From 1b3a5d3572da41af5388cd787e8ccc14f9b2c461 Mon Sep 17 00:00:00 2001 From: Alexander Kachkaev Date: Tue, 28 Jan 2020 21:35:47 +0000 Subject: [PATCH] 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 --- changelog_unreleased/api/pr-7435.md | 17 ++++++++ docs/options.md | 34 ++++++++-------- src/main/core-options.js | 19 +++++---- .../__snapshots__/jsfmt.spec.js.snap | 40 +++++++++---------- tests/end_of_line/jsfmt.spec.js | 2 +- .../__snapshots__/config-resolution.js.snap | 4 +- .../__snapshots__/early-exit.js.snap | 8 ++-- .../__snapshots__/help-options.js.snap | 8 ++-- .../plugin-options-string.js.snap | 4 +- .../__snapshots__/plugin-options.js.snap | 4 +- .../__tests__/__snapshots__/schema.js.snap | 16 ++++---- .../__snapshots__/support-info.js.snap | 25 +++++++++--- .../with-config-precedence.js.snap | 8 ++-- tests_integration/__tests__/format.js | 12 ++++-- tests_integration/cli/config/.prettierrc | 2 +- .../cli/config/js/prettier.config.js | 2 +- 16 files changed, 121 insertions(+), 84 deletions(-) create mode 100644 changelog_unreleased/api/pr-7435.md diff --git a/changelog_unreleased/api/pr-7435.md b/changelog_unreleased/api/pr-7435.md new file mode 100644 index 000000000..a9032b052 --- /dev/null +++ b/changelog_unreleased/api/pr-7435.md @@ -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" }`. diff --git a/docs/options.md b/docs/options.md index 0f80e402c..04400138a 100644 --- a/docs/options.md +++ b/docs/options.md @@ -326,38 +326,36 @@ Valid options: ## 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. 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. -It also converts mixed line endings within one file to what it finds at the end of the first line. +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 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. -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 entire git repository only contains Linux-style line endings in files covered by Prettier: -If you want to make sure that your git repository only contains Linux-style line endings in files covered by Prettier: - -1. Set `endOfLine` option to `lf` +1. Ensure Prettier’s `endOfLine` option is set to `lf` (this is a default value since v2.0.0) 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. 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. - Alternatively, you can add `* text=auto eol=lf` to the repo's `.gitattributes` file to achieve this. +1. Add `* text=auto eol=lf` to the repo's `.gitattributes` file. + 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. -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: -- `"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 - `"crlf"` - Carriage Return + Line Feed characters (`\r\n`), common on Windows - `"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 | -| -------- | ----------------------------------------------------------- | ---------------------------------------------------------- | -| `"auto"` | --end-of-line | endOfLine: "" | +| Default | CLI Override | API Override | +| ------- | ----------------------------------------------------------- | ---------------------------------------------------------- | +| `"lf"` | --end-of-line | endOfLine: "" | diff --git a/src/main/core-options.js b/src/main/core-options.js index d52447681..426f7502d 100644 --- a/src/main/core-options.js +++ b/src/main/core-options.js @@ -65,16 +65,12 @@ const options = { since: "1.15.0", category: CATEGORY_GLOBAL, 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.", 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", description: @@ -88,6 +84,13 @@ const options = { { value: "cr", 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) + ` } ] }, diff --git a/tests/end_of_line/__snapshots__/jsfmt.spec.js.snap b/tests/end_of_line/__snapshots__/jsfmt.spec.js.snap index d26abd579..b5dcc9ee3 100644 --- a/tests/end_of_line/__snapshots__/jsfmt.spec.js.snap +++ b/tests/end_of_line/__snapshots__/jsfmt.spec.js.snap @@ -2,6 +2,25 @@ exports[`example.js 1`] = ` ====================================options===================================== +endOfLine: "lf" +parsers: ["babel"] +printWidth: 80 + | printWidth +=====================================input====================================== +function f() { + console.log("testing line endings"); +} + +=====================================output===================================== +function f() { + console.log("testing line endings"); +} + +================================================================================ +`; + +exports[`example.js 2`] = ` +====================================options===================================== endOfLine: "cr" parsers: ["babel"] printWidth: 80 @@ -19,7 +38,7 @@ function f() { ================================================================================ `; -exports[`example.js 2`] = ` +exports[`example.js 3`] = ` ====================================options===================================== endOfLine: "crlf" parsers: ["babel"] @@ -37,22 +56,3 @@ function f() { ================================================================================ `; - -exports[`example.js 3`] = ` -====================================options===================================== -endOfLine: "lf" -parsers: ["babel"] -printWidth: 80 - | printWidth -=====================================input====================================== -function f() { - console.log("testing line endings"); -} - -=====================================output===================================== -function f() { - console.log("testing line endings"); -} - -================================================================================ -`; diff --git a/tests/end_of_line/jsfmt.spec.js b/tests/end_of_line/jsfmt.spec.js index 7aa8fd2f1..ee8f5b520 100644 --- a/tests/end_of_line/jsfmt.spec.js +++ b/tests/end_of_line/jsfmt.spec.js @@ -1,3 +1,3 @@ +run_spec(__dirname, ["babel"], { endOfLine: "lf" }); run_spec(__dirname, ["babel"], { endOfLine: "cr" }); run_spec(__dirname, ["babel"], { endOfLine: "crlf" }); -run_spec(__dirname, ["babel"], { endOfLine: "lf" }); diff --git a/tests_integration/__tests__/__snapshots__/config-resolution.js.snap b/tests_integration/__tests__/__snapshots__/config-resolution.js.snap index 47180fffc..fd68b478c 100644 --- a/tests_integration/__tests__/__snapshots__/config-resolution.js.snap +++ b/tests_integration/__tests__/__snapshots__/config-resolution.js.snap @@ -44,7 +44,7 @@ function js() { module.exports = { endOfLine: - \\"lf\\", + \\"auto\\", tabWidth: 8, }; function noConfigJs() { @@ -143,7 +143,7 @@ function js() { \\"use strict\\"; module.exports = { - endOfLine: \\"lf\\", + endOfLine: \\"auto\\", tabWidth: 8, }; function noConfigJs() { diff --git a/tests_integration/__tests__/__snapshots__/early-exit.js.snap b/tests_integration/__tests__/__snapshots__/early-exit.js.snap index 3749692c3..db5bcfb8b 100644 --- a/tests_integration/__tests__/__snapshots__/early-exit.js.snap +++ b/tests_integration/__tests__/__snapshots__/early-exit.js.snap @@ -62,9 +62,9 @@ Format options: Include parentheses around a sole arrow function parameter. Defaults to avoid. --no-bracket-spacing Do not print spaces between brackets. - --end-of-line + --end-of-line Which end of line characters to apply. - Defaults to auto. + Defaults to lf. --html-whitespace-sensitivity How to handle whitespaces in HTML. Defaults to css. @@ -218,9 +218,9 @@ Format options: Include parentheses around a sole arrow function parameter. Defaults to avoid. --no-bracket-spacing Do not print spaces between brackets. - --end-of-line + --end-of-line Which end of line characters to apply. - Defaults to auto. + Defaults to lf. --html-whitespace-sensitivity How to handle whitespaces in HTML. Defaults to css. diff --git a/tests_integration/__tests__/__snapshots__/help-options.js.snap b/tests_integration/__tests__/__snapshots__/help-options.js.snap index fe588d066..5757513eb 100644 --- a/tests_integration/__tests__/__snapshots__/help-options.js.snap +++ b/tests_integration/__tests__/__snapshots__/help-options.js.snap @@ -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 (stdout) 1`] = ` -"--end-of-line +"--end-of-line Which end of line characters to apply. 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 crlf Carriage Return + Line Feed characters (\\\\r\\\\n), common on Windows 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 " `; diff --git a/tests_integration/__tests__/__snapshots__/plugin-options-string.js.snap b/tests_integration/__tests__/__snapshots__/plugin-options-string.js.snap index 77d3ae01f..9ffaf73be 100644 --- a/tests_integration/__tests__/__snapshots__/plugin-options-string.js.snap +++ b/tests_integration/__tests__/__snapshots__/plugin-options-string.js.snap @@ -8,9 +8,9 @@ exports[` 1`] = ` @@ -17,10 +17,12 @@ Defaults to avoid. --no-bracket-spacing Do not print spaces between brackets. - --end-of-line + --end-of-line Which end of line characters to apply. - Defaults to auto. + Defaults to lf. + --foo-string foo description + Defaults to bar. --html-whitespace-sensitivity diff --git a/tests_integration/__tests__/__snapshots__/plugin-options.js.snap b/tests_integration/__tests__/__snapshots__/plugin-options.js.snap index 28016078e..fda2e64c4 100644 --- a/tests_integration/__tests__/__snapshots__/plugin-options.js.snap +++ b/tests_integration/__tests__/__snapshots__/plugin-options.js.snap @@ -8,9 +8,9 @@ exports[` 1`] = ` @@ -17,10 +17,12 @@ Defaults to avoid. --no-bracket-spacing Do not print spaces between brackets. - --end-of-line + --end-of-line Which end of line characters to apply. - Defaults to auto. + Defaults to lf. + --foo-option foo description + Defaults to bar. --html-whitespace-sensitivity diff --git a/tests_integration/__tests__/__snapshots__/schema.js.snap b/tests_integration/__tests__/__snapshots__/schema.js.snap index e3cf53b44..bc89850e3 100644 --- a/tests_integration/__tests__/__snapshots__/schema.js.snap +++ b/tests_integration/__tests__/__snapshots__/schema.js.snap @@ -36,16 +36,9 @@ This option cannot be used with --range-start and --range-end.", "type": "integer", }, "endOfLine": Object { - "default": "auto", + "default": "lf", "description": "Which end of line characters to apply.", "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 { "description": "Line Feed only (\\\\n), common on Linux and macOS as well as inside git repos", "enum": Array [ @@ -64,6 +57,13 @@ This option cannot be used with --range-start and --range-end.", "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 { diff --git a/tests_integration/__tests__/__snapshots__/support-info.js.snap b/tests_integration/__tests__/__snapshots__/support-info.js.snap index 1470fc6da..fcf21fbae 100644 --- a/tests_integration/__tests__/__snapshots__/support-info.js.snap +++ b/tests_integration/__tests__/__snapshots__/support-info.js.snap @@ -499,10 +499,10 @@ exports[`API getSupportInfo() with version 1.8.2 -> 1.16.0 1`] = ` }, + \\"endOfLine\\": Object { + \\"choices\\": Array [ -+ \\"auto\\", + \\"lf\\", + \\"crlf\\", + \\"cr\\", ++ \\"auto\\", + ], + \\"default\\": \\"auto\\", + \\"type\\": \\"choice\\", @@ -609,6 +609,19 @@ exports[`API getSupportInfo() with version 1.16.0 -> undefined 1`] = ` ], \\"Markdown\\": Array [ \\"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 @@ \\"mdx\\", \\"vue\\", @@ -1131,10 +1144,6 @@ exports[`CLI --support-info (stdout) 1`] = ` { \\"category\\": \\"Global\\", \\"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\\", \\"value\\": \\"lf\\" @@ -1146,9 +1155,13 @@ exports[`CLI --support-info (stdout) 1`] = ` { \\"description\\": \\"Carriage Return character only (\\\\\\\\r), used very rarely\\", \\"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.\\", \\"name\\": \\"endOfLine\\", \\"pluginDefaults\\": {}, diff --git a/tests_integration/__tests__/__snapshots__/with-config-precedence.js.snap b/tests_integration/__tests__/__snapshots__/with-config-precedence.js.snap index db14ab935..17dcd9711 100644 --- a/tests_integration/__tests__/__snapshots__/with-config-precedence.js.snap +++ b/tests_integration/__tests__/__snapshots__/with-config-precedence.js.snap @@ -54,7 +54,7 @@ exports[`CLI overrides gets ignored when config exists with --config-precedence \\"use strict\\"; module.exports = { - endOfLine: \\"lf\\", + endOfLine: \\"auto\\", tabWidth: 8, }; " @@ -71,7 +71,7 @@ exports[`CLI overrides take lower precedence with --config-precedence file-overr \\"use strict\\"; module.exports = { - endOfLine: \\"lf\\", + endOfLine: \\"auto\\", tabWidth: 8, }; " @@ -123,7 +123,7 @@ function js() { module.exports = { endOfLine: - \\"lf\\", + \\"auto\\", tabWidth: 8, }; function noConfigJs() { @@ -215,7 +215,7 @@ function js() { module.exports = { endOfLine: - \\"lf\\", + \\"auto\\", tabWidth: 8, }; function noConfigJs() { diff --git a/tests_integration/__tests__/format.js b/tests_integration/__tests__/format.js index 8303cd487..70b8b9147 100644 --- a/tests_integration/__tests__/format.js +++ b/tests_integration/__tests__/format.js @@ -7,7 +7,9 @@ test("yaml parser should handle CRLF correctly", () => { const input = "a:\r\n 123\r\n"; expect( // use JSON.stringify to observe CRLF - JSON.stringify(prettier.format(input, { parser: "yaml" })) + JSON.stringify( + prettier.format(input, { parser: "yaml", endOfLine: "auto" }) + ) ).toMatchSnapshot(); }); @@ -31,7 +33,9 @@ test("html parser should handle CRLF correctly", () => { const input = ""; expect( // use JSON.stringify to observe CRLF - JSON.stringify(prettier.format(input, { parser: "html" })) + JSON.stringify( + prettier.format(input, { parser: "html", endOfLine: "auto" }) + ) ).toMatchSnapshot(); }); @@ -39,7 +43,9 @@ test("markdown parser should handle CRLF correctly", () => { const input = "```\r\n\r\n\r\n```"; expect( // use JSON.stringify to observe CRLF - JSON.stringify(prettier.format(input, { parser: "markdown" })) + JSON.stringify( + prettier.format(input, { parser: "markdown", endOfLine: "auto" }) + ) ).toMatchSnapshot(); }); diff --git a/tests_integration/cli/config/.prettierrc b/tests_integration/cli/config/.prettierrc index 7c221bcc1..43de9b5e6 100644 --- a/tests_integration/cli/config/.prettierrc +++ b/tests_integration/cli/config/.prettierrc @@ -1,4 +1,4 @@ -endOfLine: 'lf' +endOfLine: 'auto' overrides: - files: "*.js" options: diff --git a/tests_integration/cli/config/js/prettier.config.js b/tests_integration/cli/config/js/prettier.config.js index da1c5408e..7676cdef0 100644 --- a/tests_integration/cli/config/js/prettier.config.js +++ b/tests_integration/cli/config/js/prettier.config.js @@ -1,6 +1,6 @@ "use strict"; module.exports = { - endOfLine: 'lf', + endOfLine: 'auto', tabWidth: 8 };