From e0ec565cf4420a9fe094fa2b1ed86ad5f10a75d1 Mon Sep 17 00:00:00 2001 From: Ika Date: Wed, 8 Aug 2018 12:48:21 +0800 Subject: [PATCH 1/5] test: add tests --- jest.config.js | 5 ++++- package.json | 1 + tests_config/ansi-serializer.js | 13 +++++++++++++ .../__tests__/__snapshots__/format.js.snap | 11 +++++++++++ tests_integration/__tests__/format.js | 16 ++++++++++++++++ yarn.lock | 6 ++++++ 6 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 tests_config/ansi-serializer.js diff --git a/jest.config.js b/jest.config.js index 2351c2b5ab50..dc12fda87132 100644 --- a/jest.config.js +++ b/jest.config.js @@ -12,7 +12,10 @@ const isOldNode = semver.parse(process.version).major <= 4; module.exports = { setupFiles: ["/tests_config/run_spec.js"], - snapshotSerializers: ["/tests_config/raw-serializer.js"], + snapshotSerializers: [ + "/tests_config/raw-serializer.js", + "/tests_config/ansi-serializer.js" + ], testRegex: "jsfmt\\.spec\\.js$|__tests__/.*\\.js$", testPathIgnorePatterns: ["tests/new_react", "tests/more_react"].concat( isOldNode ? requiresPrettierInternals : [] diff --git a/package.json b/package.json index 367ba18d8b58..1c97d4ce57f2 100644 --- a/package.json +++ b/package.json @@ -80,6 +80,7 @@ "eslint-plugin-prettier": "2.6.0", "eslint-plugin-react": "7.7.0", "execa": "0.10.0", + "has-ansi": "3.0.0", "jest": "23.3.0", "jest-junit": "5.0.0", "jest-watch-typeahead": "0.1.0", diff --git a/tests_config/ansi-serializer.js b/tests_config/ansi-serializer.js new file mode 100644 index 000000000000..0bacfd319eb7 --- /dev/null +++ b/tests_config/ansi-serializer.js @@ -0,0 +1,13 @@ +"use strict"; + +const hasAnsi = require("has-ansi"); +const stripAnsi = require("strip-ansi"); + +module.exports = { + print(value) { + return stripAnsi(value); + }, + test(value) { + return typeof value === "string" && hasAnsi(value); + } +}; diff --git a/tests_integration/__tests__/__snapshots__/format.js.snap b/tests_integration/__tests__/__snapshots__/format.js.snap index b9f307aed4f7..7ca3e8838618 100644 --- a/tests_integration/__tests__/__snapshots__/format.js.snap +++ b/tests_integration/__tests__/__snapshots__/format.js.snap @@ -1,5 +1,16 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`typescript parser should throw the first error when both JSX and non-JSX mode failed 1`] = ` +'>' expected. (5:8) + 3 | + 4 | const App = () => ( +> 5 |
+ | ^ + 6 |
+ 7 | ); + 8 | +`; + exports[`yaml parser should handle CRLF correctly 1`] = ` "a: 123 " diff --git a/tests_integration/__tests__/format.js b/tests_integration/__tests__/format.js index b1b4eee03fb2..bf730ae99b60 100644 --- a/tests_integration/__tests__/format.js +++ b/tests_integration/__tests__/format.js @@ -6,3 +6,19 @@ test("yaml parser should handle CRLF correctly", () => { const input = "a:\r\n 123\r\n"; expect(prettier.format(input, { parser: "yaml" })).toMatchSnapshot(); }); + +test("typescript parser should throw the first error when both JSX and non-JSX mode failed", () => { + const input = ` +import React from "react"; + +const App = () => ( +
+
+); + +label: + `; + expect(() => + prettier.format(input, { parser: "typescript" }) + ).toThrowErrorMatchingSnapshot(); +}); diff --git a/yarn.lock b/yarn.lock index 1fe5f1562c49..d02bc5ff7ea4 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2603,6 +2603,12 @@ har-validator@~5.0.3: ajv "^5.1.0" har-schema "^2.0.0" +has-ansi@3.0.0: + version "3.0.0" + resolved "https://registry.yarnpkg.com/has-ansi/-/has-ansi-3.0.0.tgz#36077ef1d15f333484aa7fa77a28606f1c655b37" + dependencies: + ansi-regex "^3.0.0" + has-ansi@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/has-ansi/-/has-ansi-2.0.0.tgz#34f5049ce1ecdf2b0649af3ef24e45ed35416d91" From 02d1e45652522910b12d28130a8040d52f9a5f94 Mon Sep 17 00:00:00 2001 From: Ika Date: Wed, 8 Aug 2018 12:49:42 +0800 Subject: [PATCH 2/5] fix(typescript): use the first error when both failed --- src/language-js/parser-typescript.js | 26 ++++++++++--------- .../__tests__/__snapshots__/format.js.snap | 14 +++++----- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/language-js/parser-typescript.js b/src/language-js/parser-typescript.js index e2897411f34a..87cf6078e244 100644 --- a/src/language-js/parser-typescript.js +++ b/src/language-js/parser-typescript.js @@ -9,22 +9,24 @@ function parse(text /*, parsers, opts*/) { const jsx = isProbablyJsx(text); let ast; try { + // Try passing with our best guess first. + ast = tryParseTypeScript(text, jsx); + } catch (firstError) { try { - // Try passing with our best guess first. - ast = tryParseTypeScript(text, jsx); - } catch (e) { // But if we get it wrong, try the opposite. - /* istanbul ignore next */ ast = tryParseTypeScript(text, !jsx); - } - } catch (e) /* istanbul ignore next */ { - if (typeof e.lineNumber === "undefined") { - throw e; - } + } catch (secondError) { + // suppose our guess is correct + const e = firstError; - throw createError(e.message, { - start: { line: e.lineNumber, column: e.column + 1 } - }); + if (typeof e.lineNumber === "undefined") { + throw e; + } + + throw createError(e.message, { + start: { line: e.lineNumber, column: e.column + 1 } + }); + } } delete ast.tokens; diff --git a/tests_integration/__tests__/__snapshots__/format.js.snap b/tests_integration/__tests__/__snapshots__/format.js.snap index 7ca3e8838618..ded5117f27b7 100644 --- a/tests_integration/__tests__/__snapshots__/format.js.snap +++ b/tests_integration/__tests__/__snapshots__/format.js.snap @@ -1,14 +1,12 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`typescript parser should throw the first error when both JSX and non-JSX mode failed 1`] = ` -'>' expected. (5:8) - 3 | - 4 | const App = () => ( -> 5 |
- | ^ - 6 |
- 7 | ); - 8 | +Expression expected. (9:7) + 7 | ); + 8 | +> 9 | label: + | ^ + 10 | `; exports[`yaml parser should handle CRLF correctly 1`] = ` From cabe7d5c01fc87a8aaf63251a1cad2729cf3cca8 Mon Sep 17 00:00:00 2001 From: Ika Date: Wed, 8 Aug 2018 13:36:35 +0800 Subject: [PATCH 3/5] test: update input --- tests_integration/__tests__/format.js | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/tests_integration/__tests__/format.js b/tests_integration/__tests__/format.js index bf730ae99b60..2ddd099e655a 100644 --- a/tests_integration/__tests__/format.js +++ b/tests_integration/__tests__/format.js @@ -8,16 +8,8 @@ test("yaml parser should handle CRLF correctly", () => { }); test("typescript parser should throw the first error when both JSX and non-JSX mode failed", () => { - const input = ` -import React from "react"; - -const App = () => ( -
-
-); - -label: - `; + const input = + '\nimport React from "react";\n\nconst App = () => (\n
\n
\n);\n\nlabel:\n '; expect(() => prettier.format(input, { parser: "typescript" }) ).toThrowErrorMatchingSnapshot(); From 42c6e5ac139b31781c548151bf30483574e29273 Mon Sep 17 00:00:00 2001 From: Ika Date: Wed, 8 Aug 2018 15:31:57 +0800 Subject: [PATCH 4/5] Revert "test: update input" This reverts commit cabe7d5c01fc87a8aaf63251a1cad2729cf3cca8. --- tests_integration/__tests__/format.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/tests_integration/__tests__/format.js b/tests_integration/__tests__/format.js index 2ddd099e655a..bf730ae99b60 100644 --- a/tests_integration/__tests__/format.js +++ b/tests_integration/__tests__/format.js @@ -8,8 +8,16 @@ test("yaml parser should handle CRLF correctly", () => { }); test("typescript parser should throw the first error when both JSX and non-JSX mode failed", () => { - const input = - '\nimport React from "react";\n\nconst App = () => (\n
\n
\n);\n\nlabel:\n '; + const input = ` +import React from "react"; + +const App = () => ( +
+
+); + +label: + `; expect(() => prettier.format(input, { parser: "typescript" }) ).toThrowErrorMatchingSnapshot(); From fdf90654b10cd72e3cf750ec0204da7ed07d7767 Mon Sep 17 00:00:00 2001 From: Ika Date: Wed, 8 Aug 2018 15:53:47 +0800 Subject: [PATCH 5/5] test: fix serializer --- tests_config/ansi-serializer.js | 4 ++-- tests_integration/__tests__/__snapshots__/format.js.snap | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests_config/ansi-serializer.js b/tests_config/ansi-serializer.js index 0bacfd319eb7..e807dc5bb8f7 100644 --- a/tests_config/ansi-serializer.js +++ b/tests_config/ansi-serializer.js @@ -4,8 +4,8 @@ const hasAnsi = require("has-ansi"); const stripAnsi = require("strip-ansi"); module.exports = { - print(value) { - return stripAnsi(value); + print(value, serialize) { + return serialize(stripAnsi(value)); }, test(value) { return typeof value === "string" && hasAnsi(value); diff --git a/tests_integration/__tests__/__snapshots__/format.js.snap b/tests_integration/__tests__/__snapshots__/format.js.snap index ded5117f27b7..164d9cb180aa 100644 --- a/tests_integration/__tests__/__snapshots__/format.js.snap +++ b/tests_integration/__tests__/__snapshots__/format.js.snap @@ -1,12 +1,12 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`typescript parser should throw the first error when both JSX and non-JSX mode failed 1`] = ` -Expression expected. (9:7) +"Expression expected. (9:7) 7 | ); 8 | > 9 | label: | ^ - 10 | + 10 | " `; exports[`yaml parser should handle CRLF correctly 1`] = `