fix: guess line ending before preprocess (#4957)

master
Ika 2018-08-09 10:51:08 +08:00 committed by GitHub
parent 3842cbb118
commit 53c83f97f5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 8 additions and 6 deletions

View File

@ -71,6 +71,8 @@ function coreFormat(text, opts, addAlignmentSize) {
const parsed = parser.parse(text, opts);
const ast = parsed.ast;
const originalText = text;
text = parsed.text;
if (opts.cursorOffset >= 0) {
@ -82,7 +84,7 @@ function coreFormat(text, opts, addAlignmentSize) {
const astComments = attachComments(text, ast, opts);
const doc = printAstToDoc(ast, opts, addAlignmentSize);
opts.newLine = guessLineEnding(text);
opts.newLine = guessLineEnding(originalText);
const result = printDocToString(doc, opts);

View File

@ -9,7 +9,4 @@ exports[`typescript parser should throw the first error when both JSX and non-JS
10 | "
`;
exports[`yaml parser should handle CRLF correctly 1`] = `
"a: 123
"
`;
exports[`yaml parser should handle CRLF correctly 1`] = `"\\"a: 123\\\\r\\\\n\\""`;

View File

@ -4,7 +4,10 @@ const prettier = require("prettier/local");
test("yaml parser should handle CRLF correctly", () => {
const input = "a:\r\n 123\r\n";
expect(prettier.format(input, { parser: "yaml" })).toMatchSnapshot();
expect(
// use JSON.stringify to observe CRLF
JSON.stringify(prettier.format(input, { parser: "yaml" }))
).toMatchSnapshot();
});
test("typescript parser should throw the first error when both JSX and non-JSX mode failed", () => {