Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simple expressions in template literals #3315

Closed
thorn0 opened this issue Nov 24, 2017 · 5 comments
Closed

Simple expressions in template literals #3315

thorn0 opened this issue Nov 24, 2017 · 5 comments
Labels
lang:javascript Issues affecting JS locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. type:duplicate Issues that are a duplicate of a previous issue

Comments

@thorn0
Copy link
Member

thorn0 commented Nov 24, 2017

Prettier 1.8.2
Playground link

Input:

const a = `Lorem ipsum dolor sit amet, consectetur ${bar} adipiscing ${foo}. Sed tincidunt diam tortor.`;

Output:

const a = `Lorem ipsum dolor sit amet, consectetur ${bar} adipiscing ${
  foo
}. Sed tincidunt diam tortor.`;

Expected behavior:

Is the output indeed prettier than the input? To me, it looks simply strange. Why did foo get its own line while bar didn't? I'd love to have a switch to disable formatting like this. Having a line longer than print-width seems to be a lesser evil in this case. It should be okay though, even when the switch is on, to break lines when embedded expressions are more complex than foo or data.id.

@SimenB
Copy link
Contributor

SimenB commented Nov 24, 2017

See #3299

@duailibe
Copy link
Member

Thanks for the report! We have changed the behavior for identifiers, so it should stop breaking.

It will continue to break on member expressions (even a.b) until we find a good heuristic for when to break (or someone suggests one).

@thorn0
Copy link
Member Author

thorn0 commented Nov 25, 2017

That's great news. Thank you @duailibe. I'd vote for never breaking on member expressions if the left part is an identifier and the expression

  1. uses dot (a.b), or
  2. uses brackets with the property name being a number (a[1]), or
  3. uses brackets with the property name being an identifier (a[i]).

Breaking on such simple expressions looks as unnatural as on identifiers.

@duailibe duailibe added lang:javascript Issues affecting JS status:needs discussion Issues needing discussion and a decision to be made before action can be taken labels Nov 28, 2017
@duailibe
Copy link
Member

duailibe commented Nov 28, 2017

@thorn0 Thanks for the suggestion.

The thing about template literals is without breaking between { } we were printing:

const str1 = `text text text ${foo.
  bar} text text`;

const str2 = `text text text ${foo[
  bar
]} text text`;

because foo.bar will break regardless of being in a template literal expression.

Then we could special case printing those differently when inside a template literal.

@ikatyang
Copy link
Member

ikatyang commented Dec 1, 2017

Closing as duplicate of #3280 (comment) and OP's identifier issue was fixed in #3299, let's track the member expression issue in #3368 to avoid confusing.

@ikatyang ikatyang closed this as completed Dec 1, 2017
@ikatyang ikatyang added type:bug Issues identifying ugly output, or a defect in the program and removed status:needs discussion Issues needing discussion and a decision to be made before action can be taken labels Dec 1, 2017
@ikatyang ikatyang added type:duplicate Issues that are a duplicate of a previous issue and removed type:bug Issues identifying ugly output, or a defect in the program labels Dec 1, 2017
@lock lock bot added the locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. label Jul 6, 2018
@lock lock bot locked as resolved and limited conversation to collaborators Jul 6, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
lang:javascript Issues affecting JS locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. type:duplicate Issues that are a duplicate of a previous issue
Projects
None yet
Development

No branches or pull requests

4 participants