Change endOfLine to lf by default for 2.0 (#7435)

* Towards changing default value for endOfLine

* Update tests and snapshots

* Add placeholder for changelog_unreleased

* Add CHANGELOG

* Undo adding snapshot tests for auto as it does not play well on Windows in CI

* Tweak changelog

* Update jest snapshot
This commit is contained in:
Alexander Kachkaev authored and Georgii Dolzhykov committed 2020-01-28 23:35:47 +02:00
1 parent d0f0bac976
commit 1b3a5d3572
16 files changed
+121 -84

No files matched your search

+17
View File
@@ -0,0 +1,17 @@
#### Change default value for `endOfLine` to `lf` ([#7435](https://github.com/prettier/prettier/pull/7435) by [@kachkaev](https://github.com/kachkaev))
Early versions of Prettier were formatting all files with the \*nix flavor of [line endings](https://en.wikipedia.org/wiki/Newline) (`\n`, also known as `LF`).
This behavior was changed in [#472](https://github.com/prettier/prettier/pull/472), which allowed preserving Windows line endings (`\r\n`, also known as `CRLF`).
[Since Prettier version 1.15](https://prettier.io/blog/2018/11/07/1.15.0.html#add-an-option-to-enforce-line-endings-5327-by-kachkaev), the flavor of line endings is configurable via the `endOfLine` option.
The default value was set to `auto` for backwards compatibility, which meant that Prettier preserved a flavor of line endings already present in a given file.
With `endOfLine` equal to `auto`, Prettier converted mixed line endings within one file to what was found at the end of the first line.
However, line endings in neighboring files could still remain inconsistent.
Besides, contributors on different operating systems could accidentally change line endings in previously committed files and this would be fine with Prettier.
Doing so would produce a large `git diff` and thus make the line-by-line history for a file (`git blame`) harder to explore.
You are encouraged to use the new default value for `endOfLine`, which is now `lf`.
It may be also worth [checking the option docs](https://prettier.io/docs/en/options.html#end-of-line) to ensure your project repository is configured correctly.
This will help you avoid a mix of line endings in the repo and a broken `git blame`.
If the old behavior is still preferred, please configure Prettier with `{ "endOfLine": "auto" }`.
+16 -18
View File
@@ -326,38 +326,36 @@ Valid options:
## End of Line ## End of Line
_First available in 1.15.0_ _First available in v1.15.0, default value changed from `auto` to `lf` in v2.0.0_
For historical reasons, there exist two commonly used flavors of line endings in text files. That is `\n` (or `LF` for _Line Feed_) and `\r\n` (or `CRLF` for _Carriage Return + Line Feed_). For historical reasons, there exist two common flavors of line endings in text files.
That is `\n` (or `LF` for _Line Feed_) and `\r\n` (or `CRLF` for _Carriage Return + Line Feed_).
The former is common on Linux and macOS, while the latter is prevalent on Windows. The former is common on Linux and macOS, while the latter is prevalent on Windows.
Some details explaining why it is so [can be found on Wikipedia](https://en.wikipedia.org/wiki/Newline). Some details explaining why it is so [can be found on Wikipedia](https://en.wikipedia.org/wiki/Newline).
By default, Prettier preserves a flavor of line endings a given file has already used. When people collaborate on a project from different operating systems, it becomes easy to end up with mixed line endings in a shared git repository.
It also converts mixed line endings within one file to what it finds at the end of the first line. It is also possible for Windows users to accidentally change line endings in a previously committed file from `LF` to `CRLF`.
Doing so produces a large `git diff` and thus makes the line-by-line history for a file (`git blame`) harder to explore.
When people collaborate on a project from different operating systems, it becomes easy to end up with mixed line endings in the central git repository. If you want to make sure that your entire git repository only contains Linux-style line endings in files covered by Prettier:
It is also possible for Windows users to accidentally change line endings in an already committed file from `LF` to `CRLF`.
Doing so produces a large `git diff`, and if it goes unnoticed during code review, all line-by-line history for the file (`git blame`) gets lost.
If you want to make sure that your git repository only contains Linux-style line endings in files covered by Prettier: 1. Ensure Prettier’s `endOfLine` option is set to `lf` (this is a default value since v2.0.0)
1. Set `endOfLine` option to `lf`
1. Configure [a pre-commit hook](precommit.md) that will run Prettier 1. Configure [a pre-commit hook](precommit.md) that will run Prettier
1. Configure Prettier to run in your CI pipeline using [`--check` flag](cli.md#check) 1. Configure Prettier to run in your CI pipeline using [`--check` flag](cli.md#check)
1. Ask Windows users to run `git config core.autocrlf false` before working on your repo so that git did not convert `LF` to `CRLF` on checkout. 1. Add `* text=auto eol=lf` to the repo's `.gitattributes` file.
Alternatively, you can add `* text=auto eol=lf` to the repo's `.gitattributes` file to achieve this. You may need to ask Windows users to re-clone your repo after this change to ensure git has not converted `LF` to `CRLF` on checkout.
All modern text editors in all operating systems are able to correctly display line endings when `\n` (`LF`) is used. All modern text editors in all operating systems are able to correctly display line endings when `\n` (`LF`) is used.
However, old versions of Notepad for Windows will visually squash such lines into one. However, old versions of Notepad for Windows will visually squash such lines into one as they can only deal with `\r\n` (`CRLF`).
Valid options: Valid options:
- `"auto"` - Maintain existing line endings
(mixed values within one file are normalised by looking at what's used after the first line)
- `"lf"` – Line Feed only (`\n`), common on Linux and macOS as well as inside git repos - `"lf"` – Line Feed only (`\n`), common on Linux and macOS as well as inside git repos
- `"crlf"` - Carriage Return + Line Feed characters (`\r\n`), common on Windows - `"crlf"` - Carriage Return + Line Feed characters (`\r\n`), common on Windows
- `"cr"` - Carriage Return character only (`\r`), used very rarely - `"cr"` - Carriage Return character only (`\r`), used very rarely
- `"auto"` - Maintain existing line endings
(mixed values within one file are normalised by looking at what's used after the first line)
| Default | CLI Override | API Override | | Default | CLI Override | API Override |
| -------- | ----------------------------------------------------------- | ---------------------------------------------------------- | | ------- | ----------------------------------------------------------- | ---------------------------------------------------------- |
| `"auto"` | <code>--end-of-line <auto&#124;lf&#124;crlf&#124;cr></code> | <code>endOfLine: "<auto&#124;lf&#124;crlf&#124;cr>"</code> | | `"lf"` | <code>--end-of-line <lf&#124;crlf&#124;cr&#124;auto></code> | <code>endOfLine: "<lf&#124;crlf&#124;cr&#124;auto>"</code> |
+11 -8
View File
@@ -65,16 +65,12 @@ const options = {
since: "1.15.0", since: "1.15.0",
category: CATEGORY_GLOBAL, category: CATEGORY_GLOBAL,
type: "choice", type: "choice",
default: "auto", default: [
{ since: "1.15.0", value: "auto" },
{ since: "2.0.0", value: "lf" }
],
description: "Which end of line characters to apply.", description: "Which end of line characters to apply.",
choices: [ choices: [
{
value: "auto",
description: dedent`
Maintain existing
(mixed values within one file are normalised by looking at what's used after the first line)
`
},
{ {
value: "lf", value: "lf",
description: description:
@@ -88,6 +84,13 @@ const options = {
{ {
value: "cr", value: "cr",
description: "Carriage Return character only (\\r), used very rarely" description: "Carriage Return character only (\\r), used very rarely"
},
{
value: "auto",
description: dedent`
Maintain existing
(mixed values within one file are normalised by looking at what's used after the first line)
`
} }
] ]
}, },
@@ -2,6 +2,25 @@
exports[`example.js 1`] = ` exports[`example.js 1`] = `
====================================options===================================== ====================================options=====================================
endOfLine: "lf"
parsers: ["babel"]
printWidth: 80
| printWidth
=====================================input======================================
function f() {<LF>
console.log("testing line endings");<LF>
}<LF>
=====================================output=====================================
function f() {<LF>
console.log("testing line endings");<LF>
}<LF>
================================================================================
`;
exports[`example.js 2`] = `
====================================options=====================================
endOfLine: "cr" endOfLine: "cr"
parsers: ["babel"] parsers: ["babel"]
printWidth: 80 printWidth: 80
@@ -19,7 +38,7 @@ function f() {<CR>
================================================================================ ================================================================================
`; `;
exports[`example.js 2`] = ` exports[`example.js 3`] = `
====================================options===================================== ====================================options=====================================
endOfLine: "crlf" endOfLine: "crlf"
parsers: ["babel"] parsers: ["babel"]
@@ -37,22 +56,3 @@ function f() {<CRLF>
================================================================================ ================================================================================
`; `;
exports[`example.js 3`] = `
====================================options=====================================
endOfLine: "lf"
parsers: ["babel"]
printWidth: 80
| printWidth
=====================================input======================================
function f() {<LF>
console.log("testing line endings");<LF>
}<LF>
=====================================output=====================================
function f() {<LF>
console.log("testing line endings");<LF>
}<LF>
================================================================================
`;
+1 -1
View File
@@ -1,3 +1,3 @@
run_spec(__dirname, ["babel"], { endOfLine: "lf" });
run_spec(__dirname, ["babel"], { endOfLine: "cr" }); run_spec(__dirname, ["babel"], { endOfLine: "cr" });
run_spec(__dirname, ["babel"], { endOfLine: "crlf" }); run_spec(__dirname, ["babel"], { endOfLine: "crlf" });
run_spec(__dirname, ["babel"], { endOfLine: "lf" });
@@ -44,7 +44,7 @@ function js() {
module.exports = { module.exports = {
endOfLine: endOfLine:
\\"lf\\", \\"auto\\",
tabWidth: 8, tabWidth: 8,
}; };
function noConfigJs() { function noConfigJs() {
@@ -143,7 +143,7 @@ function js() {
\\"use strict\\"; \\"use strict\\";
module.exports = { module.exports = {
endOfLine: \\"lf\\", endOfLine: \\"auto\\",
tabWidth: 8, tabWidth: 8,
}; };
function noConfigJs() { function noConfigJs() {
@@ -62,9 +62,9 @@ Format options:
Include parentheses around a sole arrow function parameter. Include parentheses around a sole arrow function parameter.
Defaults to avoid. Defaults to avoid.
--no-bracket-spacing Do not print spaces between brackets. --no-bracket-spacing Do not print spaces between brackets.
--end-of-line <auto|lf|crlf|cr> --end-of-line <lf|crlf|cr|auto>
Which end of line characters to apply. Which end of line characters to apply.
Defaults to auto. Defaults to lf.
--html-whitespace-sensitivity <css|strict|ignore> --html-whitespace-sensitivity <css|strict|ignore>
How to handle whitespaces in HTML. How to handle whitespaces in HTML.
Defaults to css. Defaults to css.
@@ -218,9 +218,9 @@ Format options:
Include parentheses around a sole arrow function parameter. Include parentheses around a sole arrow function parameter.
Defaults to avoid. Defaults to avoid.
--no-bracket-spacing Do not print spaces between brackets. --no-bracket-spacing Do not print spaces between brackets.
--end-of-line <auto|lf|crlf|cr> --end-of-line <lf|crlf|cr|auto>
Which end of line characters to apply. Which end of line characters to apply.
Defaults to auto. Defaults to lf.
--html-whitespace-sensitivity <css|strict|ignore> --html-whitespace-sensitivity <css|strict|ignore>
How to handle whitespaces in HTML. How to handle whitespaces in HTML.
Defaults to css. Defaults to css.
@@ -117,19 +117,19 @@ exports[`show detailed usage with --help editorconfig (write) 1`] = `Array []`;
exports[`show detailed usage with --help end-of-line (stderr) 1`] = `""`; exports[`show detailed usage with --help end-of-line (stderr) 1`] = `""`;
exports[`show detailed usage with --help end-of-line (stdout) 1`] = ` exports[`show detailed usage with --help end-of-line (stdout) 1`] = `
"--end-of-line <auto|lf|crlf|cr> "--end-of-line <lf|crlf|cr|auto>
Which end of line characters to apply. Which end of line characters to apply.
Valid options: Valid options:
auto Maintain existing
(mixed values within one file are normalised by looking at what's used after the first line)
lf Line Feed only (\\\\n), common on Linux and macOS as well as inside git repos lf Line Feed only (\\\\n), common on Linux and macOS as well as inside git repos
crlf Carriage Return + Line Feed characters (\\\\r\\\\n), common on Windows crlf Carriage Return + Line Feed characters (\\\\r\\\\n), common on Windows
cr Carriage Return character only (\\\\r), used very rarely cr Carriage Return character only (\\\\r), used very rarely
auto Maintain existing
(mixed values within one file are normalised by looking at what's used after the first line)
Default: auto Default: lf
" "
`; `;
@@ -8,9 +8,9 @@ exports[` 1`] = `
@@ -17,10 +17,12 @@ @@ -17,10 +17,12 @@
Defaults to avoid. Defaults to avoid.
--no-bracket-spacing Do not print spaces between brackets. --no-bracket-spacing Do not print spaces between brackets.
--end-of-line <auto|lf|crlf|cr> --end-of-line <lf|crlf|cr|auto>
Which end of line characters to apply. Which end of line characters to apply.
Defaults to auto. Defaults to lf.
+ --foo-string <string> foo description + --foo-string <string> foo description
+ Defaults to bar. + Defaults to bar.
--html-whitespace-sensitivity <css|strict|ignore> --html-whitespace-sensitivity <css|strict|ignore>
@@ -8,9 +8,9 @@ exports[` 1`] = `
@@ -17,10 +17,12 @@ @@ -17,10 +17,12 @@
Defaults to avoid. Defaults to avoid.
--no-bracket-spacing Do not print spaces between brackets. --no-bracket-spacing Do not print spaces between brackets.
--end-of-line <auto|lf|crlf|cr> --end-of-line <lf|crlf|cr|auto>
Which end of line characters to apply. Which end of line characters to apply.
Defaults to auto. Defaults to lf.
+ --foo-option <bar|baz> foo description + --foo-option <bar|baz> foo description
+ Defaults to bar. + Defaults to bar.
--html-whitespace-sensitivity <css|strict|ignore> --html-whitespace-sensitivity <css|strict|ignore>
@@ -36,16 +36,9 @@ This option cannot be used with --range-start and --range-end.",
"type": "integer", "type": "integer",
}, },
"endOfLine": Object { "endOfLine": Object {
"default": "auto", "default": "lf",
"description": "Which end of line characters to apply.", "description": "Which end of line characters to apply.",
"oneOf": Array [ "oneOf": Array [
Object {
"description": "Maintain existing
(mixed values within one file are normalised by looking at what's used after the first line)",
"enum": Array [
"auto",
],
},
Object { Object {
"description": "Line Feed only (\\\\n), common on Linux and macOS as well as inside git repos", "description": "Line Feed only (\\\\n), common on Linux and macOS as well as inside git repos",
"enum": Array [ "enum": Array [
@@ -64,6 +57,13 @@ This option cannot be used with --range-start and --range-end.",
"cr", "cr",
], ],
}, },
Object {
"description": "Maintain existing
(mixed values within one file are normalised by looking at what's used after the first line)",
"enum": Array [
"auto",
],
},
], ],
}, },
"filepath": Object { "filepath": Object {
@@ -499,10 +499,10 @@ exports[`API getSupportInfo() with version 1.8.2 -> 1.16.0 1`] = `
}, },
+ \\"endOfLine\\": Object { + \\"endOfLine\\": Object {
+ \\"choices\\": Array [ + \\"choices\\": Array [
+ \\"auto\\",
+ \\"lf\\", + \\"lf\\",
+ \\"crlf\\", + \\"crlf\\",
+ \\"cr\\", + \\"cr\\",
+ \\"auto\\",
+ ], + ],
+ \\"default\\": \\"auto\\", + \\"default\\": \\"auto\\",
+ \\"type\\": \\"choice\\", + \\"type\\": \\"choice\\",
@@ -609,6 +609,19 @@ exports[`API getSupportInfo() with version 1.16.0 -> undefined 1`] = `
], ],
\\"Markdown\\": Array [ \\"Markdown\\": Array [
\\"markdown\\", \\"markdown\\",
@@ -91,11 +94,11 @@
\\"lf\\",
\\"crlf\\",
\\"cr\\",
\\"auto\\",
],
- \\"default\\": \\"auto\\",
+ \\"default\\": \\"lf\\",
\\"type\\": \\"choice\\",
},
\\"filepath\\": Object {
\\"default\\": undefined,
\\"type\\": \\"path\\",
@@ -138,10 +141,11 @@ @@ -138,10 +141,11 @@
\\"mdx\\", \\"mdx\\",
\\"vue\\", \\"vue\\",
@@ -1131,10 +1144,6 @@ exports[`CLI --support-info (stdout) 1`] = `
{ {
\\"category\\": \\"Global\\", \\"category\\": \\"Global\\",
\\"choices\\": [ \\"choices\\": [
{
\\"description\\": \\"Maintain existing\\\\n(mixed values within one file are normalised by looking at what's used after the first line)\\",
\\"value\\": \\"auto\\"
},
{ {
\\"description\\": \\"Line Feed only (\\\\\\\\n), common on Linux and macOS as well as inside git repos\\", \\"description\\": \\"Line Feed only (\\\\\\\\n), common on Linux and macOS as well as inside git repos\\",
\\"value\\": \\"lf\\" \\"value\\": \\"lf\\"
@@ -1146,9 +1155,13 @@ exports[`CLI --support-info (stdout) 1`] = `
{ {
\\"description\\": \\"Carriage Return character only (\\\\\\\\r), used very rarely\\", \\"description\\": \\"Carriage Return character only (\\\\\\\\r), used very rarely\\",
\\"value\\": \\"cr\\" \\"value\\": \\"cr\\"
},
{
\\"description\\": \\"Maintain existing\\\\n(mixed values within one file are normalised by looking at what's used after the first line)\\",
\\"value\\": \\"auto\\"
} }
], ],
\\"default\\": \\"auto\\", \\"default\\": \\"lf\\",
\\"description\\": \\"Which end of line characters to apply.\\", \\"description\\": \\"Which end of line characters to apply.\\",
\\"name\\": \\"endOfLine\\", \\"name\\": \\"endOfLine\\",
\\"pluginDefaults\\": {}, \\"pluginDefaults\\": {},
@@ -54,7 +54,7 @@ exports[`CLI overrides gets ignored when config exists with --config-precedence
\\"use strict\\"; \\"use strict\\";
module.exports = { module.exports = {
endOfLine: \\"lf\\", endOfLine: \\"auto\\",
tabWidth: 8, tabWidth: 8,
}; };
" "
@@ -71,7 +71,7 @@ exports[`CLI overrides take lower precedence with --config-precedence file-overr
\\"use strict\\"; \\"use strict\\";
module.exports = { module.exports = {
endOfLine: \\"lf\\", endOfLine: \\"auto\\",
tabWidth: 8, tabWidth: 8,
}; };
" "
@@ -123,7 +123,7 @@ function js() {
module.exports = { module.exports = {
endOfLine: endOfLine:
\\"lf\\", \\"auto\\",
tabWidth: 8, tabWidth: 8,
}; };
function noConfigJs() { function noConfigJs() {
@@ -215,7 +215,7 @@ function js() {
module.exports = { module.exports = {
endOfLine: endOfLine:
\\"lf\\", \\"auto\\",
tabWidth: 8, tabWidth: 8,
}; };
function noConfigJs() { function noConfigJs() {
+9 -3
View File
@@ -7,7 +7,9 @@ test("yaml parser should handle CRLF correctly", () => {
const input = "a:\r\n 123\r\n"; const input = "a:\r\n 123\r\n";
expect( expect(
// use JSON.stringify to observe CRLF // use JSON.stringify to observe CRLF
JSON.stringify(prettier.format(input, { parser: "yaml" })) JSON.stringify(
prettier.format(input, { parser: "yaml", endOfLine: "auto" })
)
).toMatchSnapshot(); ).toMatchSnapshot();
}); });
@@ -31,7 +33,9 @@ test("html parser should handle CRLF correctly", () => {
const input = "<!--\r\n test\r\n test\r\n-->"; const input = "<!--\r\n test\r\n test\r\n-->";
expect( expect(
// use JSON.stringify to observe CRLF // use JSON.stringify to observe CRLF
JSON.stringify(prettier.format(input, { parser: "html" })) JSON.stringify(
prettier.format(input, { parser: "html", endOfLine: "auto" })
)
).toMatchSnapshot(); ).toMatchSnapshot();
}); });
@@ -39,7 +43,9 @@ test("markdown parser should handle CRLF correctly", () => {
const input = "```\r\n\r\n\r\n```"; const input = "```\r\n\r\n\r\n```";
expect( expect(
// use JSON.stringify to observe CRLF // use JSON.stringify to observe CRLF
JSON.stringify(prettier.format(input, { parser: "markdown" })) JSON.stringify(
prettier.format(input, { parser: "markdown", endOfLine: "auto" })
)
).toMatchSnapshot(); ).toMatchSnapshot();
}); });
+1 -1
View File
@@ -1,4 +1,4 @@
endOfLine: 'lf' endOfLine: 'auto'
overrides: overrides:
- files: "*.js" - files: "*.js"
options: options:
@@ -1,6 +1,6 @@
"use strict"; "use strict";
module.exports = { module.exports = {
endOfLine: 'lf', endOfLine: 'auto',
tabWidth: 8 tabWidth: 8
}; };