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 b09d8701a1cb..7155523059a8 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/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_config/ansi-serializer.js b/tests_config/ansi-serializer.js new file mode 100644 index 000000000000..e807dc5bb8f7 --- /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, 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 b9f307aed4f7..164d9cb180aa 100644 --- a/tests_integration/__tests__/__snapshots__/format.js.snap +++ b/tests_integration/__tests__/__snapshots__/format.js.snap @@ -1,5 +1,14 @@ // 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) + 7 | ); + 8 | +> 9 | label: + | ^ + 10 | " +`; + 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 06576df5aa39..8f63fd04236f 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"