Uniform line endings for multi line strings/comments (#7891)
This commit is contained in:
1 parent
46fc3def7b
commit
04e4ef3758
5 files changed
+249
-15
No files matched your search
@@ -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))
|
||||
|
||||
<!-- prettier-ignore -->
|
||||
```
|
||||
// Input
|
||||
export const IAmIncredibleLongFunctionName = IAmAnotherFunctionName(<CRLF>
|
||||
(_0: IAmIncredibleLongParameterType) => {<CRLF>
|
||||
setTimeout(() => {<CRLF>
|
||||
/*<CRLF>
|
||||
Multiline comment<CRLF>
|
||||
Multiline comment<CRLF>
|
||||
Multiline comment<CRLF>
|
||||
*/<CRLF>
|
||||
console.log(<CRLF>
|
||||
"Multiline string\<CRLF>
|
||||
Multiline string\<CRLF>
|
||||
Multiline string"<CRLF>
|
||||
);<CRLF>
|
||||
});<CRLF>
|
||||
}<CRLF>
|
||||
);<CRLF>
|
||||
|
||||
// Prettier stable
|
||||
export const IAmIncredibleLongFunctionName = IAmAnotherFunctionName(<CRLF>
|
||||
(_0: IAmIncredibleLongParameterType) => {<CRLF>
|
||||
setTimeout(() => {<CRLF>
|
||||
/*<LF>
|
||||
Multiline comment<LF>
|
||||
Multiline comment<LF>
|
||||
Multiline comment<LF>
|
||||
*/<CRLF>
|
||||
console.log(<CRLF>
|
||||
"Multiline string\<LF>
|
||||
Multiline string\<LF>
|
||||
Multiline string"<CRLF>
|
||||
);<CRLF>
|
||||
});<CRLF>
|
||||
}<CRLF>
|
||||
);<CRLF>
|
||||
|
||||
// Prettier master: same as input
|
||||
```
|
||||
@@ -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":
|
||||
|
||||
+1
-12
@@ -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
|
||||
|
||||
@@ -56,3 +56,180 @@ function f() {<CRLF>
|
||||
|
||||
================================================================================
|
||||
`;
|
||||
|
||||
exports[`multiline.js 1`] = `
|
||||
====================================options=====================================
|
||||
endOfLine: "lf"
|
||||
parsers: ["babel"]
|
||||
printWidth: 80
|
||||
| printWidth
|
||||
=====================================input======================================
|
||||
type IAmIncredibleLongParameterType = {};<LF>
|
||||
const IAmAnotherFunctionName = (_0: IAmIncredibleLongParameterType) => {};<LF>
|
||||
export const IAmIncredibleLongFunctionName = IAmAnotherFunctionName(<LF>
|
||||
(_0: IAmIncredibleLongParameterType) => {<LF>
|
||||
setTimeout(() => {<LF>
|
||||
/*<LF>
|
||||
Multiline comment<LF>
|
||||
Multiline comment<LF>
|
||||
Multiline comment<LF>
|
||||
*/<LF>
|
||||
console.log(<LF>
|
||||
'Multiline string\\<LF>
|
||||
Multiline string\\<LF>
|
||||
Multiline string'<LF>
|
||||
);<LF>
|
||||
console.log(<LF>
|
||||
\`Multiline \\n string\\<LF>
|
||||
Multiline string\\<LF>
|
||||
Multiline string\`<LF>
|
||||
);<LF>
|
||||
});<LF>
|
||||
}<LF>
|
||||
);<LF>
|
||||
|
||||
=====================================output=====================================
|
||||
type IAmIncredibleLongParameterType = {};<LF>
|
||||
const IAmAnotherFunctionName = (_0: IAmIncredibleLongParameterType) => {};<LF>
|
||||
export const IAmIncredibleLongFunctionName = IAmAnotherFunctionName(<LF>
|
||||
(_0: IAmIncredibleLongParameterType) => {<LF>
|
||||
setTimeout(() => {<LF>
|
||||
/*<LF>
|
||||
Multiline comment<LF>
|
||||
Multiline comment<LF>
|
||||
Multiline comment<LF>
|
||||
*/<LF>
|
||||
console.log(<LF>
|
||||
"Multiline string\\<LF>
|
||||
Multiline string\\<LF>
|
||||
Multiline string"<LF>
|
||||
);<LF>
|
||||
console.log(<LF>
|
||||
\`Multiline \\n string\\<LF>
|
||||
Multiline string\\<LF>
|
||||
Multiline string\`<LF>
|
||||
);<LF>
|
||||
});<LF>
|
||||
}<LF>
|
||||
);<LF>
|
||||
|
||||
================================================================================
|
||||
`;
|
||||
|
||||
exports[`multiline.js 2`] = `
|
||||
====================================options=====================================
|
||||
endOfLine: "cr"
|
||||
parsers: ["babel"]
|
||||
printWidth: 80
|
||||
| printWidth
|
||||
=====================================input======================================
|
||||
type IAmIncredibleLongParameterType = {};<LF>
|
||||
const IAmAnotherFunctionName = (_0: IAmIncredibleLongParameterType) => {};<LF>
|
||||
export const IAmIncredibleLongFunctionName = IAmAnotherFunctionName(<LF>
|
||||
(_0: IAmIncredibleLongParameterType) => {<LF>
|
||||
setTimeout(() => {<LF>
|
||||
/*<LF>
|
||||
Multiline comment<LF>
|
||||
Multiline comment<LF>
|
||||
Multiline comment<LF>
|
||||
*/<LF>
|
||||
console.log(<LF>
|
||||
'Multiline string\\<LF>
|
||||
Multiline string\\<LF>
|
||||
Multiline string'<LF>
|
||||
);<LF>
|
||||
console.log(<LF>
|
||||
\`Multiline \\n string\\<LF>
|
||||
Multiline string\\<LF>
|
||||
Multiline string\`<LF>
|
||||
);<LF>
|
||||
});<LF>
|
||||
}<LF>
|
||||
);<LF>
|
||||
|
||||
=====================================output=====================================
|
||||
type IAmIncredibleLongParameterType = {};<CR>
|
||||
const IAmAnotherFunctionName = (_0: IAmIncredibleLongParameterType) => {};<CR>
|
||||
export const IAmIncredibleLongFunctionName = IAmAnotherFunctionName(<CR>
|
||||
(_0: IAmIncredibleLongParameterType) => {<CR>
|
||||
setTimeout(() => {<CR>
|
||||
/*<CR>
|
||||
Multiline comment<CR>
|
||||
Multiline comment<CR>
|
||||
Multiline comment<CR>
|
||||
*/<CR>
|
||||
console.log(<CR>
|
||||
"Multiline string\\<CR>
|
||||
Multiline string\\<CR>
|
||||
Multiline string"<CR>
|
||||
);<CR>
|
||||
console.log(<CR>
|
||||
\`Multiline \\n string\\<CR>
|
||||
Multiline string\\<CR>
|
||||
Multiline string\`<CR>
|
||||
);<CR>
|
||||
});<CR>
|
||||
}<CR>
|
||||
);<CR>
|
||||
|
||||
================================================================================
|
||||
`;
|
||||
|
||||
exports[`multiline.js 3`] = `
|
||||
====================================options=====================================
|
||||
endOfLine: "crlf"
|
||||
parsers: ["babel"]
|
||||
printWidth: 80
|
||||
| printWidth
|
||||
=====================================input======================================
|
||||
type IAmIncredibleLongParameterType = {};<LF>
|
||||
const IAmAnotherFunctionName = (_0: IAmIncredibleLongParameterType) => {};<LF>
|
||||
export const IAmIncredibleLongFunctionName = IAmAnotherFunctionName(<LF>
|
||||
(_0: IAmIncredibleLongParameterType) => {<LF>
|
||||
setTimeout(() => {<LF>
|
||||
/*<LF>
|
||||
Multiline comment<LF>
|
||||
Multiline comment<LF>
|
||||
Multiline comment<LF>
|
||||
*/<LF>
|
||||
console.log(<LF>
|
||||
'Multiline string\\<LF>
|
||||
Multiline string\\<LF>
|
||||
Multiline string'<LF>
|
||||
);<LF>
|
||||
console.log(<LF>
|
||||
\`Multiline \\n string\\<LF>
|
||||
Multiline string\\<LF>
|
||||
Multiline string\`<LF>
|
||||
);<LF>
|
||||
});<LF>
|
||||
}<LF>
|
||||
);<LF>
|
||||
|
||||
=====================================output=====================================
|
||||
type IAmIncredibleLongParameterType = {};<CRLF>
|
||||
const IAmAnotherFunctionName = (_0: IAmIncredibleLongParameterType) => {};<CRLF>
|
||||
export const IAmIncredibleLongFunctionName = IAmAnotherFunctionName(<CRLF>
|
||||
(_0: IAmIncredibleLongParameterType) => {<CRLF>
|
||||
setTimeout(() => {<CRLF>
|
||||
/*<CRLF>
|
||||
Multiline comment<CRLF>
|
||||
Multiline comment<CRLF>
|
||||
Multiline comment<CRLF>
|
||||
*/<CRLF>
|
||||
console.log(<CRLF>
|
||||
"Multiline string\\<CRLF>
|
||||
Multiline string\\<CRLF>
|
||||
Multiline string"<CRLF>
|
||||
);<CRLF>
|
||||
console.log(<CRLF>
|
||||
\`Multiline \\n string\\<CRLF>
|
||||
Multiline string\\<CRLF>
|
||||
Multiline string\`<CRLF>
|
||||
);<CRLF>
|
||||
});<CRLF>
|
||||
}<CRLF>
|
||||
);<CRLF>
|
||||
|
||||
================================================================================
|
||||
`;
|
||||
@@ -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`
|
||||
);
|
||||
});
|
||||
}
|
||||
);
|
||||
Reference in new issue
Block a user