Avoid introducing linebreaks in template interpolations (#15209)

This commit is contained in:
Kevin Gibbons authored and GitHub committed 2023-12-17 18:10:20 +08:00
1 parent d541f0d699
commit cb6fea81fb
10 files changed
+151 -102

No files matched your search

+15
View File
@@ -143,6 +143,21 @@ One final thing: TC39 has [not yet decided if decorators come before or after `e
export @decorator class Foo {}
```
### Template literals
Template literals can contain interpolations. Deciding whether it's appropriate to insert a linebreak within an interpolation unfortunately depends on the semantic content of the template - for example, introducing a linebreak in the middle of a natural-language sentence is usually undesirable. Since Prettier doesn't have enough information to make this decision itself, it uses a heuristic similar to that used for objects: it will only split an interpolation expression across multiple lines if there was already a linebreak within that interpolation.
This means that a literal like the following will not be broken onto multiple lines, even if it exceeds the print width:
<!-- prettier-ignore -->
```js
`this is a long message which contains an interpolation: ${format(data)} <- like this`;
```
If you want Prettier to split up an interpolation, you'll need to ensure there's a linebreak somewhere within the `${...}`. Otherwise it will keep everything on a single line, no matter how long it is.
The team would prefer not to depend on the original formatting in this way, but it's the best heuristic we have at the moment.
### Semicolons
This is about using the [noSemi](options.md#semicolons) option.