Skip to content

Commit

Permalink
fix(parser): [Babel8] Align error codes between Flow and TypeScript (#…
Browse files Browse the repository at this point in the history
…13294)

* Align error codes between Flow and TypeScript

* Implement compat fallback in makeErrorTemplates

* Add tests
  • Loading branch information
sosukesuzuki committed Aug 28, 2021
1 parent 38d70e2 commit 64d116b
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 3 deletions.
15 changes: 14 additions & 1 deletion packages/babel-parser/src/parser/error.js
Expand Up @@ -28,17 +28,30 @@ export type ErrorTemplates = {
[key: string]: ErrorTemplate,
};

type SyntaxPlugin = "flow" | "typescript" | "jsx" | typeof undefined;

function keepReasonCodeCompat(reasonCode: string, syntaxPlugin: SyntaxPlugin) {
if (!process.env.BABEL_8_BREAKING) {
// For consistency in TypeScript and Flow error codes
if (syntaxPlugin === "flow" && reasonCode === "PatternIsOptional") {
return "OptionalBindingPattern";
}
}
return reasonCode;
}

export function makeErrorTemplates(
messages: {
[key: string]: string,
},
code: ErrorCode,
syntaxPlugin?: SyntaxPlugin,
): ErrorTemplates {
const templates: ErrorTemplates = {};
Object.keys(messages).forEach(reasonCode => {
templates[reasonCode] = Object.freeze({
code,
reasonCode,
reasonCode: keepReasonCodeCompat(reasonCode, syntaxPlugin),
template: messages[reasonCode],
});
});
Expand Down
5 changes: 3 additions & 2 deletions packages/babel-parser/src/plugins/flow/index.js
Expand Up @@ -99,7 +99,7 @@ const FlowErrors = makeErrorTemplates(
"`declare module` cannot be used inside another `declare module`.",
NestedFlowComment:
"Cannot have a flow comment inside another flow comment.",
OptionalBindingPattern:
PatternIsOptional:
"A binding pattern parameter cannot be optional in an implementation signature.",
SetterMayNotHaveThisParam: "A setter cannot have a `this` parameter.",
SpreadVariance: "Spread properties cannot have variance.",
Expand Down Expand Up @@ -137,6 +137,7 @@ const FlowErrors = makeErrorTemplates(
UnterminatedFlowComment: "Unterminated flow-comment.",
},
/* code */ ErrorCodes.SyntaxError,
/* syntaxPlugin */ "flow",
);
/* eslint-disable sort-keys */

Expand Down Expand Up @@ -2532,7 +2533,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
parseAssignableListItemTypes(param: N.Pattern): N.Pattern {
if (this.eat(tt.question)) {
if (param.type !== "Identifier") {
this.raise(param.start, FlowErrors.OptionalBindingPattern);
this.raise(param.start, FlowErrors.PatternIsOptional);
}
if (this.isThisParam(param)) {
this.raise(param.start, FlowErrors.ThisParamMayNotBeOptional);
Expand Down
1 change: 1 addition & 0 deletions packages/babel-parser/src/plugins/jsx/index.js
Expand Up @@ -40,6 +40,7 @@ const JsxErrors = makeErrorTemplates(
"Adjacent JSX elements must be wrapped in an enclosing tag. Did you want a JSX fragment <>...</>?",
},
/* code */ ErrorCodes.SyntaxError,
/* syntaxPlugin */ "jsx",
);
/* eslint-disable sort-keys */

Expand Down
1 change: 1 addition & 0 deletions packages/babel-parser/src/plugins/typescript/index.js
Expand Up @@ -156,6 +156,7 @@ const TSErrors = makeErrorTemplates(
"Name in a signature must be an Identifier, ObjectPattern or ArrayPattern, instead got %0.",
},
/* code */ ErrorCodes.SyntaxError,
/* syntaxPlugin */ "typescript",
);
/* eslint-disable sort-keys */

Expand Down
21 changes: 21 additions & 0 deletions packages/babel-parser/test/error-codes.js
Expand Up @@ -18,4 +18,25 @@ describe("error codes", function () {
expect(error.code).toBe("BABEL_PARSER_SYNTAX_ERROR");
expect(error.reasonCode).toBe("MissingSemicolon");
});
it("consistent reasonCode between Flow and TypeScript in Babel 8", () => {
const code = `function f([]?) {}`;
const {
errors: [tsError],
} = parse(code, {
errorRecovery: true,
plugins: ["typescript"],
});
const {
errors: [flowError],
} = parse(code, {
errorRecovery: true,
plugins: ["flow"],
});
expect(flowError.reasonCode).toBe(
process.env.BABEL_8_BREAKING
? tsError.reasonCode
: "OptionalBindingPattern",
);
expect(flowError.message).toBe(tsError.message);
});
});

0 comments on commit 64d116b

Please sign in to comment.