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

Template literals have conditional formatting when used as function arguments #13056

Closed
zsakowitz opened this issue Jun 29, 2022 · 2 comments
Closed

Comments

@zsakowitz
Copy link

When used as function arguments and in function calls, template literals conditionally insert a newline before and after themselves depending on whether there is a newline in the source code.

Prettier 2.7.1
Playground link 1, playground link 2

--parser babel

Input 1:

fn(`
abc
`);

Output 1:

fn(`
abc
`);

Input 2:

fn(
  `
abc
`);

Output 2:

fn(
  `
abc
`
);

Expected behavior:

Prettier should select one style and stick with it. The Prettier website mentions that only cases where newlines are taken into account are in object literals, object type literals, and decorators, yet the compiler produces different results in this case.

I have found that the minimal example requires that the template literal have a single newline with no other characters. If no newlines are present in the source string, Prettier correctly formats the function call as a single line in the result.

@zsakowitz
Copy link
Author

After experimenting, I noticed that this issue does not occur in computed property access, where a newline is inserted before multi line strings whether or not one existed beforehand.

@alexander-akait
Copy link
Member

Duplicate of #2068, we look at newlines (for function call) in your source code to decide to keep the newline or not for better DX, we can solve it only introduce the --pure option

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 2, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants