From 04e4ef375844b011ab586072ecc97678afe838b4 Mon Sep 17 00:00:00 2001 From: Sidharth Vinod Date: Sun, 5 Apr 2020 16:21:29 +0530 Subject: [PATCH] Uniform line endings for multi line strings/comments (#7891) --- changelog_unreleased/javascript/pr-7891.md | 42 +++++ src/document/doc-printer.js | 9 +- src/main/core.js | 13 +- .../__snapshots__/jsfmt.spec.js.snap | 177 ++++++++++++++++++ tests/end_of_line/multiline.js | 23 +++ 5 files changed, 249 insertions(+), 15 deletions(-) create mode 100644 changelog_unreleased/javascript/pr-7891.md create mode 100644 tests/end_of_line/multiline.js diff --git a/changelog_unreleased/javascript/pr-7891.md b/changelog_unreleased/javascript/pr-7891.md new file mode 100644 index 000000000000..75a7a2e1da6e --- /dev/null +++ b/changelog_unreleased/javascript/pr-7891.md @@ -0,0 +1,42 @@ +#### Fix: line endings were not always converted properly in multiline strings and comments ([#7891](https://github.com/prettier/prettier/pull/7891) by [@sidharthv96](https://github.com/sidharthv96)) + + +``` +// Input +export const IAmIncredibleLongFunctionName = IAmAnotherFunctionName( + (_0: IAmIncredibleLongParameterType) => { + setTimeout(() => { + /* + Multiline comment + Multiline comment + Multiline comment + */ + console.log( + "Multiline string\ + Multiline string\ + Multiline string" + ); + }); + } +); + +// Prettier stable +export const IAmIncredibleLongFunctionName = IAmAnotherFunctionName( + (_0: IAmIncredibleLongParameterType) => { + setTimeout(() => { + /* + Multiline comment + Multiline comment + Multiline comment + */ + console.log( + "Multiline string\ + Multiline string\ + Multiline string" + ); + }); + } +); + +// Prettier master: same as input +``` diff --git a/src/document/doc-printer.js b/src/document/doc-printer.js index 800e44e1c4a3..737532fb3af5 100644 --- a/src/document/doc-printer.js +++ b/src/document/doc-printer.js @@ -253,9 +253,12 @@ function printDocToString(doc, options) { const [ind, mode, doc] = cmds.pop(); if (typeof doc === "string") { - out.push(doc); - - pos += getStringWidth(doc); + const formatted = + newLine !== "\n" && doc.includes("\n") + ? doc.replace(/\n/g, newLine) + : doc; + out.push(formatted); + pos += getStringWidth(formatted); } else { switch (doc.type) { case "cursor": diff --git a/src/main/core.js b/src/main/core.js index 8d35eab40673..58ff4fa31a0d 100644 --- a/src/main/core.js +++ b/src/main/core.js @@ -14,7 +14,6 @@ const { const rangeUtil = require("./range-util"); const privateUtil = require("../common/util"); const { - utils: { mapDoc }, printer: { printDocToString }, debug: { printDocToDebug }, } = require("../document"); @@ -85,17 +84,7 @@ function coreFormat(text, opts, addAlignmentSize) { const astComments = attachComments(text, ast, opts); const doc = printAstToDoc(ast, opts, addAlignmentSize); - const eol = convertEndOfLineToChars(opts.endOfLine); - const result = printDocToString( - opts.endOfLine === "lf" - ? doc - : mapDoc(doc, (currentDoc) => - typeof currentDoc === "string" && currentDoc.includes("\n") - ? currentDoc.replace(/\n/g, eol) - : currentDoc - ), - opts - ); + const result = printDocToString(doc, opts); ensureAllCommentsPrinted(astComments); // Remove extra leading indentation as well as the added indentation after last newline diff --git a/tests/end_of_line/__snapshots__/jsfmt.spec.js.snap b/tests/end_of_line/__snapshots__/jsfmt.spec.js.snap index b5dcc9ee3430..6212d4daf4d8 100644 --- a/tests/end_of_line/__snapshots__/jsfmt.spec.js.snap +++ b/tests/end_of_line/__snapshots__/jsfmt.spec.js.snap @@ -56,3 +56,180 @@ function f() { ================================================================================ `; + +exports[`multiline.js 1`] = ` +====================================options===================================== +endOfLine: "lf" +parsers: ["babel"] +printWidth: 80 + | printWidth +=====================================input====================================== +type IAmIncredibleLongParameterType = {}; +const IAmAnotherFunctionName = (_0: IAmIncredibleLongParameterType) => {}; +export const IAmIncredibleLongFunctionName = IAmAnotherFunctionName( + (_0: IAmIncredibleLongParameterType) => { + setTimeout(() => { + /* + Multiline comment + Multiline comment + Multiline comment + */ + console.log( + 'Multiline string\\ + Multiline string\\ + Multiline string' + ); + console.log( + \`Multiline \\n string\\ + Multiline string\\ + Multiline string\` + ); + }); + } +); + +=====================================output===================================== +type IAmIncredibleLongParameterType = {}; +const IAmAnotherFunctionName = (_0: IAmIncredibleLongParameterType) => {}; +export const IAmIncredibleLongFunctionName = IAmAnotherFunctionName( + (_0: IAmIncredibleLongParameterType) => { + setTimeout(() => { + /* + Multiline comment + Multiline comment + Multiline comment + */ + console.log( + "Multiline string\\ + Multiline string\\ + Multiline string" + ); + console.log( + \`Multiline \\n string\\ + Multiline string\\ + Multiline string\` + ); + }); + } +); + +================================================================================ +`; + +exports[`multiline.js 2`] = ` +====================================options===================================== +endOfLine: "cr" +parsers: ["babel"] +printWidth: 80 + | printWidth +=====================================input====================================== +type IAmIncredibleLongParameterType = {}; +const IAmAnotherFunctionName = (_0: IAmIncredibleLongParameterType) => {}; +export const IAmIncredibleLongFunctionName = IAmAnotherFunctionName( + (_0: IAmIncredibleLongParameterType) => { + setTimeout(() => { + /* + Multiline comment + Multiline comment + Multiline comment + */ + console.log( + 'Multiline string\\ + Multiline string\\ + Multiline string' + ); + console.log( + \`Multiline \\n string\\ + Multiline string\\ + Multiline string\` + ); + }); + } +); + +=====================================output===================================== +type IAmIncredibleLongParameterType = {}; +const IAmAnotherFunctionName = (_0: IAmIncredibleLongParameterType) => {}; +export const IAmIncredibleLongFunctionName = IAmAnotherFunctionName( + (_0: IAmIncredibleLongParameterType) => { + setTimeout(() => { + /* + Multiline comment + Multiline comment + Multiline comment + */ + console.log( + "Multiline string\\ + Multiline string\\ + Multiline string" + ); + console.log( + \`Multiline \\n string\\ + Multiline string\\ + Multiline string\` + ); + }); + } +); + +================================================================================ +`; + +exports[`multiline.js 3`] = ` +====================================options===================================== +endOfLine: "crlf" +parsers: ["babel"] +printWidth: 80 + | printWidth +=====================================input====================================== +type IAmIncredibleLongParameterType = {}; +const IAmAnotherFunctionName = (_0: IAmIncredibleLongParameterType) => {}; +export const IAmIncredibleLongFunctionName = IAmAnotherFunctionName( + (_0: IAmIncredibleLongParameterType) => { + setTimeout(() => { + /* + Multiline comment + Multiline comment + Multiline comment + */ + console.log( + 'Multiline string\\ + Multiline string\\ + Multiline string' + ); + console.log( + \`Multiline \\n string\\ + Multiline string\\ + Multiline string\` + ); + }); + } +); + +=====================================output===================================== +type IAmIncredibleLongParameterType = {}; +const IAmAnotherFunctionName = (_0: IAmIncredibleLongParameterType) => {}; +export const IAmIncredibleLongFunctionName = IAmAnotherFunctionName( + (_0: IAmIncredibleLongParameterType) => { + setTimeout(() => { + /* + Multiline comment + Multiline comment + Multiline comment + */ + console.log( + "Multiline string\\ + Multiline string\\ + Multiline string" + ); + console.log( + \`Multiline \\n string\\ + Multiline string\\ + Multiline string\` + ); + }); + } +); + +================================================================================ +`; diff --git a/tests/end_of_line/multiline.js b/tests/end_of_line/multiline.js new file mode 100644 index 000000000000..b51866579807 --- /dev/null +++ b/tests/end_of_line/multiline.js @@ -0,0 +1,23 @@ +type IAmIncredibleLongParameterType = {}; +const IAmAnotherFunctionName = (_0: IAmIncredibleLongParameterType) => {}; +export const IAmIncredibleLongFunctionName = IAmAnotherFunctionName( + (_0: IAmIncredibleLongParameterType) => { + setTimeout(() => { + /* + Multiline comment + Multiline comment + Multiline comment + */ + console.log( + 'Multiline string\ + Multiline string\ + Multiline string' + ); + console.log( + `Multiline \n string\ + Multiline string\ + Multiline string` + ); + }); + } +);