question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[bug] Prettier formats contents of template string

See original GitHub issue

Prettier 1.14.3

playground link

Input:

export const FlexBox = styled.div`
	display: flex;
	align-items: center;
	justify-content: center;
	flex-wrap: wrap;
	flex-direction: ${p => p.direction};

	${p => p.stackOnMobile ? `
		${media('<desktop')} {
			flex-direction: column;
		}
	` : ''}
	
	> * {
		width: ${p => (p.howManyPerRow ? `${100 / p.howManyPerRow}%` : 'auto')};
	}
`

output:

export const FlexBox = styled.div`
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
  flex-direction: ${p => p.direction};

  ${p =>
    p.stackOnMobile
      ? `
		${media("<desktop")} {
			flex-direction: column;
		}
	`
      : ""} > * {
    width: ${p => (p.howManyPerRow ? `${100 / p.howManyPerRow}%` : "auto")};
  }
`;

Expected behavior:

Prettier should never format any part of a template string (at least not without some in-code option like some sort of comment).

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:3
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
lydellcommented, Oct 18, 2018

Is the output formatting in the OP example not ugly to you?

There’s a lot of symbols, so my brain quickly zones out and I’m not sure what I’m looking at. Is there a specific part of it you don’t like?

Honestly I just want to disable this template string formatting, and have a way to opt-in only when I want to.

Could you say why you want this? Is it breaking your code? Or is it “just” a bit ugly? Wouldn’t you be more interested in fixing things rather than disabling things?

1reaction
trusktrcommented, Oct 18, 2018

Arguably Prettier is trying to do too much, and should thus make some things opt-in (or opt-out). The line between formatting code and formatting template strings isn’t fully clear.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Prettier 2.2: new JavaScript parsers, TS 4.1 and ESM ...
This release supports new JavaScript parsers espree and meriyah, supports TypeScript 4.1, ships ESM standalone bundles for modern browsers, ...
Read more >
Rationale - Prettier
Prettier is an opinionated code formatter. This document explains some of its choices. What Prettier is concerned about. Correctness.
Read more >
Prettier 1.12: Fixes, Features, and Formatting, Oh My!
A bug in Prettier 1.11.1 caused expressions nested in template literals to get moved around when using prettier-ignore comments within the ...
Read more >
Prettier 2.3. In which assignments are consistent, short keys ...
This means Prettier won't format Handlebars files that can't be parsed into such a tree, either because the underlying syntax isn't HTML or ......
Read more >
Prettier 2.8: improve --cache CLI option and TypeScript 4.9 ...
A new `--cache-location` option has been added, and a bug that saved the ... Fix inconsistent formatting for multiline strings (#13274 by ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found