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

master
Ika 2018-08-09 10:21:00 +08:00 committed by GitHub
parent 4fb2070244
commit 3842cbb118
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 63 additions and 13 deletions

View File

@ -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 : []

View File

@ -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",

View File

@ -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;

View File

@ -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);
}
};

View File

@ -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
"

View File

@ -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();
});

View File

@ -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"