Skip to content

Commit

Permalink
Introduce error code
Browse files Browse the repository at this point in the history
  • Loading branch information
sosukesuzuki committed Apr 3, 2021
1 parent 61e866f commit 43187c0
Show file tree
Hide file tree
Showing 13 changed files with 1,110 additions and 432 deletions.
838 changes: 627 additions & 211 deletions packages/babel-parser/src/parser/error-message.js

Large diffs are not rendered by default.

30 changes: 24 additions & 6 deletions packages/babel-parser/src/parser/error.js
Expand Up @@ -18,7 +18,21 @@ type ErrorContext = {

export type ParsingError = SyntaxError & ErrorContext;

export { ErrorMessages as Errors } from "./error-message";
export type ErrorTemplate = {
code: string,
template: string,
};

export type ErrorTemplates = {
[key: string]: ErrorTemplate,
};

export type raiseFunction = (number, ErrorTemplate, ...any) => void;

export {
ErrorMessages as Errors,
SourceTypeModuleErrorMessages as SourceTypeModuleErrors,
} from "./error-message";

export default class ParserError extends CommentsParser {
// Forward-declaration: defined in tokenizer/index.js
Expand All @@ -37,8 +51,12 @@ export default class ParserError extends CommentsParser {
return loc;
}

raise(pos: number, errorTemplate: string, ...params: any): Error | empty {
return this.raiseWithData(pos, undefined, errorTemplate, ...params);
raise(
pos: number,
{ code, template }: ErrorTemplate,
...params: any
): Error | empty {
return this.raiseWithData(pos, { code }, template, ...params);
}

/**
Expand All @@ -55,12 +73,12 @@ export default class ParserError extends CommentsParser {
*/
raiseOverwrite(
pos: number,
errorTemplate: string,
{ code, template }: ErrorTemplate,
...params: any
): Error | empty {
const loc = this.getLocationForPosition(pos);
const message =
errorTemplate.replace(/%(\d+)/g, (_, i: number) => params[i]) +
template.replace(/%(\d+)/g, (_, i: number) => params[i]) +
` (${loc.line}:${loc.column})`;
if (this.options.errorRecovery) {
const errors = this.state.errors;
Expand All @@ -73,7 +91,7 @@ export default class ParserError extends CommentsParser {
}
}
}
return this._raise({ loc, pos }, message);
return this._raise({ code, loc, pos }, message);
}

raiseWithData(
Expand Down
15 changes: 7 additions & 8 deletions packages/babel-parser/src/parser/expression.js
Expand Up @@ -54,7 +54,7 @@ import {
newAsyncArrowScope,
newExpressionScope,
} from "../util/expression-scope";
import { Errors } from "./error";
import { Errors, SourceTypeModuleErrors } from "./error";

/*::
import type { SourceType } from "../options";
Expand Down Expand Up @@ -1358,11 +1358,7 @@ export default class ExpressionParser extends LValParser {

if (this.isContextual("meta")) {
if (!this.inModule) {
this.raiseWithData(
id.start,
{ code: "BABEL_PARSER_SOURCETYPE_MODULE_REQUIRED" },
Errors.ImportMetaOutsideModule,
);
this.raise(id.start, SourceTypeModuleErrors.ImportMetaOutsideModule);
}
this.sawUnambiguousESM = true;
}
Expand Down Expand Up @@ -1524,14 +1520,17 @@ export default class ExpressionParser extends LValParser {
const metaProp = this.parseMetaProperty(node, meta, "target");

if (!this.scope.inNonArrowFunction && !this.scope.inClass) {
let error = Errors.UnexpectedNewTarget;
let error = Errors.UnexpectedNewTarget.template;

if (this.hasPlugin("classProperties")) {
error += " or class properties";
}

/* eslint-disable @babel/development-internal/dry-error-messages */
this.raise(metaProp.start, error);
this.raise(metaProp.start, {
code: Errors.UnexpectedNewTarget.code,
template: error,
});
/* eslint-enable @babel/development-internal/dry-error-messages */
}

Expand Down
21 changes: 9 additions & 12 deletions packages/babel-parser/src/parser/statement.js
Expand Up @@ -3,7 +3,7 @@
import * as N from "../types";
import { types as tt, type TokenType } from "../tokenizer/types";
import ExpressionParser from "./expression";
import { Errors } from "./error";
import { Errors, SourceTypeModuleErrors } from "./error";
import {
isIdentifierChar,
isIdentifierStart,
Expand Down Expand Up @@ -324,13 +324,7 @@ export default class StatementParser extends ExpressionParser {

assertModuleNodeAllowed(node: N.Node): void {
if (!this.options.allowImportExportEverywhere && !this.inModule) {
this.raiseWithData(
node.start,
{
code: "BABEL_PARSER_SOURCETYPE_MODULE_REQUIRED",
},
Errors.ImportOutsideModule,
);
this.raise(node.start, SourceTypeModuleErrors.ImportOutsideModule);
}
}

Expand Down Expand Up @@ -1690,7 +1684,7 @@ export default class StatementParser extends ExpressionParser {
if (optionalId || !isStatement) {
node.id = null;
} else {
this.unexpected(null, Errors.MissingClassName);
this.unexpected(null, Errors.MissingClassName.template);
}
}
}
Expand Down Expand Up @@ -1935,7 +1929,10 @@ export default class StatementParser extends ExpressionParser {
this.expectOnePlugin(["decorators", "decorators-legacy"]);
if (this.hasPlugin("decorators")) {
if (this.getPluginOption("decorators", "decoratorsBeforeExport")) {
this.unexpected(this.state.start, Errors.DecoratorBeforeExport);
this.unexpected(
this.state.start,
Errors.DecoratorBeforeExport.template,
);
} else {
return true;
}
Expand Down Expand Up @@ -2236,7 +2233,7 @@ export default class StatementParser extends ExpressionParser {
if (!this.match(tt.string)) {
throw this.unexpected(
this.state.start,
Errors.ModuleAttributeInvalidValue,
Errors.ModuleAttributeInvalidValue.template,
);
}
node.value = this.parseLiteral<N.StringLiteral>(
Expand Down Expand Up @@ -2290,7 +2287,7 @@ export default class StatementParser extends ExpressionParser {
if (!this.match(tt.string)) {
throw this.unexpected(
this.state.start,
Errors.ModuleAttributeInvalidValue,
Errors.ModuleAttributeInvalidValue.template,
);
}
node.value = this.parseLiteral(this.state.value, "StringLiteral");
Expand Down
10 changes: 8 additions & 2 deletions packages/babel-parser/src/parser/util.js
Expand Up @@ -142,7 +142,10 @@ export default class UtilParser extends Tokenizer {
assertNoSpace(message: string = "Unexpected space."): void {
if (this.state.start > this.state.lastTokEnd) {
/* eslint-disable @babel/development-internal/dry-error-messages */
this.raise(this.state.lastTokEnd, message);
this.raise(this.state.lastTokEnd, {
code: "UnexpectedSpace",
template: message,
});
/* eslint-enable @babel/development-internal/dry-error-messages */
}
}
Expand All @@ -158,7 +161,10 @@ export default class UtilParser extends Tokenizer {
messageOrType = `Unexpected token, expected "${messageOrType.label}"`;
}
/* eslint-disable @babel/development-internal/dry-error-messages */
throw this.raise(pos != null ? pos : this.state.start, messageOrType);
throw this.raise(pos != null ? pos : this.state.start, {
code: "UnexpectedToken",
template: messageOrType,
});
/* eslint-enable @babel/development-internal/dry-error-messages */
}

Expand Down

0 comments on commit 43187c0

Please sign in to comment.