fix(parser): [Babel8] Align error codes between Flow and TypeScript (#13294)
* Align error codes between Flow and TypeScript * Implement compat fallback in makeErrorTemplates * Add tests
This commit is contained in:
@@ -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],
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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.",
|
||||
@@ -137,6 +137,7 @@ const FlowErrors = makeErrorTemplates(
|
||||
UnterminatedFlowComment: "Unterminated flow-comment.",
|
||||
},
|
||||
/* code */ ErrorCodes.SyntaxError,
|
||||
/* syntaxPlugin */ "flow",
|
||||
);
|
||||
/* eslint-disable sort-keys */
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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 */
|
||||
|
||||
|
||||
@@ -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 */
|
||||
|
||||
|
||||
Reference in New Issue
Block a user