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(parser): [Babel8] Align error codes between Flow and TypeScript #13294

Merged
merged 3 commits into from Aug 28, 2021
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
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);
});
});