From 51f3b02e50cd5508876b34cf7c79199a60bc21b1 Mon Sep 17 00:00:00 2001 From: Ika Date: Thu, 9 Aug 2018 00:58:05 +0800 Subject: [PATCH 1/2] test: update test case --- tests_integration/__tests__/__snapshots__/format.js.snap | 5 +---- tests_integration/__tests__/format.js | 5 ++++- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/tests_integration/__tests__/__snapshots__/format.js.snap b/tests_integration/__tests__/__snapshots__/format.js.snap index b9f307aed4f7..19732d7bcfe9 100644 --- a/tests_integration/__tests__/__snapshots__/format.js.snap +++ b/tests_integration/__tests__/__snapshots__/format.js.snap @@ -1,6 +1,3 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`yaml parser should handle CRLF correctly 1`] = ` -"a: 123 -" -`; +exports[`yaml parser should handle CRLF correctly 1`] = `"\\"a: 123\\\\n\\""`; diff --git a/tests_integration/__tests__/format.js b/tests_integration/__tests__/format.js index b1b4eee03fb2..08fb1991c99f 100644 --- a/tests_integration/__tests__/format.js +++ b/tests_integration/__tests__/format.js @@ -4,5 +4,8 @@ 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(); }); From 26349d5e8dd2ecd9a37a18fcc08368877bf36156 Mon Sep 17 00:00:00 2001 From: Ika Date: Thu, 9 Aug 2018 00:58:53 +0800 Subject: [PATCH 2/2] fix: guess line ending before preprocess --- src/main/core.js | 4 +++- tests_integration/__tests__/__snapshots__/format.js.snap | 2 +- 2 files changed, 4 insertions(+), 2 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 19732d7bcfe9..965eb240f3fb 100644 --- a/tests_integration/__tests__/__snapshots__/format.js.snap +++ b/tests_integration/__tests__/__snapshots__/format.js.snap @@ -1,3 +1,3 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`yaml parser should handle CRLF correctly 1`] = `"\\"a: 123\\\\n\\""`; +exports[`yaml parser should handle CRLF correctly 1`] = `"\\"a: 123\\\\r\\\\n\\""`;