--- author: Ika (@ikatyang) authorURL: https://github.com/ikatyang title: "Prettier 1.16: HTML improvements and better CRLF handling" --- This release improves HTML formatting and contains better CRLF handling, new syntax features, and fixes several bugs. ## Highlights ### HTML #### Respect surrounding linebreaks ([#5596] by [@ikatyang]) Previously, Prettier always put elements in a single line if they didn’t go past the `printWidth`, but this doesn’t work for elements that are used as `if`-`else` blocks or are intended to contain several items. To solve this problem, we’ll respect surrounding linebreaks for all elements since there is no reliable way to determine if they’re being used in these ways. ```html
# Prettier 1.15
$ prettier \*.js --list-different
unformatted.js
# Prettier 1.16
$ prettier \*.js --check
Checking formatting...
unformatted.js
Code style issues found in the above file(s). Forgot to run Prettier?
## Other changes
### General
#### Fix unexpected formatting caused by range format ([#5632] by [@lhchavez])
```js
// Input
something("something", () => {
const something = {
something: [
{
long1: "longlonglonglonglonglonglonglonglonglonglonglong",
long2: "longlonglonglonglonglonglonglonglonglonglonglong",
}
]
};
});
// Output (Prettier 1.15, --range-start 100 --range-end 120)
something("something", () => {
const something = { something: [{ long1: "longlonglonglonglonglonglonglonglonglonglonglong", long2: "longlonglonglonglonglonglonglonglonglonglonglong" }] };
});
// Output (Prettier 1.16, --range-start 100 --range-end 120)
something("something", () => {
const something = {
something: [
{
long1: "longlonglonglonglonglonglonglonglonglonglonglong",
long2: "longlonglonglonglonglonglonglonglonglonglonglong",
}
]
};
});
```
### API
#### Allow plugin instances to be passed in the `plugins` field of format options ([#5763] by [@Kingwl])
Previously, plugin instances were somehow disallowed to be passed in the
`plugins` field of format options but it should be allowed. We've fixed this
issue in Prettier 1.16.
```js
const prettier = require("prettier");
const fooPlugin = require("./path/to/foo-plugin");
const formatted = prettier.format("foo-code", {
plugins: [fooPlugin],
parser: "foo-parser"
});
// Prettier 1.15: Error
// Prettier 1.16: No error
```
### Standalone
#### Remove the dynamic `require()` call in the standalone bundle ([#5612] by [@j-f1])
Previously, a dynamic `require()` call was present in the `standalone.js` file designed
for usage in browsers. It was used in a way where it wasn’t actually called, but
tools like webpack to throw a warning. We adjusted the build script to remove
this `require()` call in the standalone bundle in Prettier 1.16.
### TypeScript
#### Correctly handle `//` in TSX ([#5728] by [@JamesHenry])
Previously, putting `//` as a child of a JSX element in TypeScript led to an error
because it was interpreted as a comment. Prettier 1.16 fixes this issue.
```js
// Input
const link = http://example.com
// Output (Prettier 1.15)
// Error: Comment location overlaps with node location
// Output (Prettier 1.16)
const link = http://example.com;
```
#### Remove redundant parentheses around type annotations ([#5724] by [@flurmbo])
```js
// Input
class Foo {
bar: (() => boolean);
}
// Output (Prettier 1.15)
class Foo {
bar: (() => boolean);
}
// Output (Prettier 1.16)
class Foo {
bar: () => boolean;
}
```
### JavaScript
#### Do not treat `something.connect()` as functional composition ([#5739] by [@makepost])
A special format for `connect()` calls was introduced to improve formatting of Redux
several versions ago, but there are many other uses for functions named `connect` that
shouldn’t be formatted that way. Since most of these cases involved a `connect` method,
we no longer treat `foo.connect()` calls as functional composition.
```js
// Input
app.connect("activate", async () => {
await data.load();
win.show_all();
});
const ConnectedComponent = connect(
bar,
baz
)(foo);
// Output (Prettier 1.15)
app.connect(
"activate",
async () => {
await data.load();
win.show_all();
}
);
const ConnectedComponent = connect(
bar,
baz
)(foo);
// Output (Prettier 1.16)
app.connect("activate", async () => {
await data.load();
win.show_all();
});
const ConnectedComponent = connect(
bar,
baz
)(foo);
```
#### Add support for class private methods ([#5637] by [@existentialism])
```js
// Input
class Hello {
#world() {}
}
// Output (Prettier 1.15)
// SyntaxError
// Output (Prettier 1.16)
class Hello {
#world() {}
}
```
#### Correct indentation for expressions in root template ([#5607] by [@ikatyang])
```js
// Input
if (a) {
return `
hello
${foo({
bar,
baz
})}
world
`;
}
// Output (Prettier 1.15)
if (a) {
return `
hello
${foo({
bar,
baz
})}
world
`;
}
// Output (Prettier 1.16)
if (a) {
return `
hello
${foo({
bar,
baz
})}
world
`;
}
```
#### Remove unnecessary linebreaks from HTML template literals ([#5771] by [@ikatyang])
```js
// Input
function HelloWorld() {
return html`
${bar}
`)} `; } // Output (Prettier 1.15) function HelloWorld() { return html`${bar}
` ) } `; } // Output (Prettier 1.16) function HelloWorld() { return html`${bar}
` )} `; } ``` ### TypeScript/Flow #### Correctly recognize `/* HTML */` templates ([#5658] by [@ikatyang]) We [added support] for HTML template literal formatting using the `/* HTML */` pseudo-tag in Prettier 1.15, but due to a bug, it only worked for JavaScript code. We’ve fixed this issue in Prettier 1.16. [added support]: https://prettier.io/blog/2018/11/07/1.15.0.html#html-template-literal-in-javascript ### CSS #### Fix broken output for lists caused by comments ([#5710] by [@jsnajdr]) ```scss // Input $my-list2: a // a b c; // Output (Prettier 1.15) $my-list2: a// a bc; // Output (Prettier 1.16) $my-list2: a // a b c; ``` #### Correctly handle backslashes ([#5597] by [@sh7dm]) ```less // Input .figcaption { .margin-top-1\/2; .large\:none; } // Output (Prettier 1.15) .figcaption { .margin-top-1\ / 2; .large\: none; } // Output (Prettier 1.16) .figcaption { .margin-top-1\/2; .large\: none; } ``` ### MDX #### Correctly handle inline html ([#5704] by [@ikatyang]) ```md | Column 1 | Column 2 | |---|---| | Text |{{ foo({ bar: {} }) }}
{{ foo({bar: {}}) }}
{{ foo({bar: ({})}) }}
``` [@existentialism]: https://github.com/existentialism [@flurmbo]: https://github.com/flurmbo [@ikatyang]: https://github.com/ikatyang [@j-f1]: https://github.com/j-f1 [@jameshenry]: https://github.com/JamesHenry [@jsnajdr]: https://github.com/jsnajdr [@kachkaev]: https://github.com/kachkaev [@kingwl]: https://github.com/Kingwl [@lhchavez]: https://github.com/lhchavez [@makepost]: https://github.com/makepost [@n1ru4l]: https://github.com/n1ru4l [@sh7dm]: https://github.com/sh7dm [@wuweiweiwu]: https://github.com/wuweiweiwu [#5494]: https://github.com/prettier/prettier/pull/5494 [#5505]: https://github.com/prettier/prettier/pull/5505 [#5590]: https://github.com/prettier/prettier/pull/5590 [#5596]: https://github.com/prettier/prettier/pull/5596 [#5597]: https://github.com/prettier/prettier/pull/5597 [#5606]: https://github.com/prettier/prettier/pull/5606 [#5607]: https://github.com/prettier/prettier/pull/5607 [#5608]: https://github.com/prettier/prettier/pull/5608 [#5612]: https://github.com/prettier/prettier/pull/5612 [#5629]: https://github.com/prettier/prettier/pull/5629 [#5632]: https://github.com/prettier/prettier/pull/5632 [#5637]: https://github.com/prettier/prettier/pull/5637 [#5642]: https://github.com/prettier/prettier/pull/5642 [#5647]: https://github.com/prettier/prettier/pull/5647 [#5657]: https://github.com/prettier/prettier/pull/5657 [#5658]: https://github.com/prettier/prettier/pull/5658 [#5685]: https://github.com/prettier/prettier/pull/5685 [#5704]: https://github.com/prettier/prettier/pull/5704 [#5710]: https://github.com/prettier/prettier/pull/5710 [#5724]: https://github.com/prettier/prettier/pull/5724 [#5728]: https://github.com/prettier/prettier/pull/5728 [#5739]: https://github.com/prettier/prettier/pull/5739 [#5763]: https://github.com/prettier/prettier/pull/5763 [#5771]: https://github.com/prettier/prettier/pull/5771