From 53c83f97f58163040bcfc7a9b05425498353b407 Mon Sep 17 00:00:00 2001 From: Ika Date: Thu, 9 Aug 2018 10:51:08 +0800 Subject: [PATCH] fix: guess line ending before preprocess (#4957) --- src/main/core.js | 4 +++- tests_integration/__tests__/__snapshots__/format.js.snap | 5 +---- tests_integration/__tests__/format.js | 5 ++++- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/main/core.js b/src/main/core.js index 47ef115f9cc5..b92e97cb6252 100644 --- a/src/main/core.js +++ b/src/main/core.js @@ -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); diff --git a/tests_integration/__tests__/__snapshots__/format.js.snap b/tests_integration/__tests__/__snapshots__/format.js.snap index 164d9cb180aa..ce4f8aea5dca 100644 --- a/tests_integration/__tests__/__snapshots__/format.js.snap +++ b/tests_integration/__tests__/__snapshots__/format.js.snap @@ -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\\""`; diff --git a/tests_integration/__tests__/format.js b/tests_integration/__tests__/format.js index bf730ae99b60..1320e5ba7ae3 100644 --- a/tests_integration/__tests__/format.js +++ b/tests_integration/__tests__/format.js @@ -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", () => {