Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(typescript): use the first error when both failed #4947

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion jest.config.js
Expand Up @@ -12,7 +12,10 @@ const isOldNode = semver.parse(process.version).major <= 4;

module.exports = {
setupFiles: ["<rootDir>/tests_config/run_spec.js"],
snapshotSerializers: ["<rootDir>/tests_config/raw-serializer.js"],
snapshotSerializers: [
"<rootDir>/tests_config/raw-serializer.js",
"<rootDir>/tests_config/ansi-serializer.js"
],
testRegex: "jsfmt\\.spec\\.js$|__tests__/.*\\.js$",
testPathIgnorePatterns: ["tests/new_react", "tests/more_react"].concat(
isOldNode ? requiresPrettierInternals : []
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -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",
Expand Down
26 changes: 14 additions & 12 deletions src/language-js/parser-typescript.js
Expand Up @@ -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;
Expand Down
13 changes: 13 additions & 0 deletions 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);
}
};
9 changes: 9 additions & 0 deletions 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
"
Expand Down
16 changes: 16 additions & 0 deletions tests_integration/__tests__/format.js
Expand Up @@ -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 = () => (
<div className="App">
</div>
);

label:
`;
expect(() =>
prettier.format(input, { parser: "typescript" })
).toThrowErrorMatchingSnapshot();
});
6 changes: 6 additions & 0 deletions yarn.lock
Expand Up @@ -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"
Expand Down