Skip to content

Commit

Permalink
Fix unexpected signature
Browse files Browse the repository at this point in the history
  • Loading branch information
sosukesuzuki committed Mar 24, 2021
1 parent 904c9d0 commit 0a70716
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 22 deletions.
11 changes: 4 additions & 7 deletions packages/babel-parser/src/parser/statement.js
Expand Up @@ -1668,7 +1668,7 @@ export default class StatementParser extends ExpressionParser {
if (optionalId || !isStatement) {
node.id = null;
} else {
this.unexpected(null, Errors.MissingClassName.template);
this.unexpected(null, Errors.MissingClassName);
}
}
}
Expand Down Expand Up @@ -1913,10 +1913,7 @@ 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.template,
);
this.unexpected(this.state.start, Errors.DecoratorBeforeExport);
} else {
return true;
}
Expand Down Expand Up @@ -2217,7 +2214,7 @@ export default class StatementParser extends ExpressionParser {
if (!this.match(tt.string)) {
throw this.unexpected(
this.state.start,
Errors.ModuleAttributeInvalidValue.template,
Errors.ModuleAttributeInvalidValue,
);
}
node.value = this.parseLiteral<N.StringLiteral>(
Expand Down Expand Up @@ -2271,7 +2268,7 @@ export default class StatementParser extends ExpressionParser {
if (!this.match(tt.string)) {
throw this.unexpected(
this.state.start,
Errors.ModuleAttributeInvalidValue.template,
Errors.ModuleAttributeInvalidValue,
);
}
node.value = this.parseLiteral(this.state.value, "StringLiteral");
Expand Down
25 changes: 14 additions & 11 deletions packages/babel-parser/src/parser/util.js
@@ -1,6 +1,6 @@
// @flow

import { types as tt, type TokenType } from "../tokenizer/types";
import { types as tt, TokenType } from "../tokenizer/types";
import Tokenizer from "../tokenizer";
import State from "../tokenizer/state";
import type { Node } from "../types";
Expand All @@ -13,7 +13,7 @@ import ProductionParameterHandler, {
PARAM_AWAIT,
PARAM,
} from "../util/production-parameter";
import { Errors } from "./error";
import { Errors, type ErrorTemplate } from "./error";
/*::
import type ScopeHandler from "../util/scope";
*/
Expand Down Expand Up @@ -91,8 +91,8 @@ export default class UtilParser extends Tokenizer {

// Asserts that following token is given contextual keyword.

expectContextual(name: string, message?: string): void {
if (!this.eatContextual(name)) this.unexpected(null, message);
expectContextual(name: string, template?: ErrorTemplate): void {
if (!this.eatContextual(name)) this.unexpected(null, template);
}

// Test whether a semicolon can be inserted at the current position.
Expand Down Expand Up @@ -155,16 +155,19 @@ export default class UtilParser extends Tokenizer {

unexpected(
pos: ?number,
messageOrType: string | TokenType = "Unexpected token",
messageOrType: ErrorTemplate | TokenType = {
code: "UnexpectedToken",
template: "Unexpected token",
},
): empty {
if (typeof messageOrType !== "string") {
messageOrType = `Unexpected token, expected "${messageOrType.label}"`;
if (messageOrType instanceof TokenType) {
messageOrType = {
code: "UnexpectedToken",
template: `Unexpected token, expected "${messageOrType.label}"`,
};
}
/* eslint-disable @babel/development-internal/dry-error-messages */
throw this.raise(pos != null ? pos : this.state.start, {
code: "UnexpectedToken",
template: messageOrType,
});
throw this.raise(pos != null ? pos : this.state.start, messageOrType);
/* eslint-enable @babel/development-internal/dry-error-messages */
}

Expand Down
4 changes: 2 additions & 2 deletions packages/babel-parser/src/plugins/flow/index.js
Expand Up @@ -414,7 +414,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
} else {
this.expectContextual(
"declare",
FlowErrors.UnsupportedStatementInDeclareModule.template,
FlowErrors.UnsupportedStatementInDeclareModule,
);

bodyNode = this.flowParseDeclare(bodyNode, true);
Expand Down Expand Up @@ -3185,7 +3185,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
skipBlockComment(): void {
if (this.hasPlugin("flowComments") && this.skipFlowComment()) {
if (this.state.hasFlowComment) {
this.unexpected(null, FlowErrors.NestedFlowComment.template);
this.unexpected(null, FlowErrors.NestedFlowComment);
}
this.hasFlowCommentCompletion();
this.state.pos += this.skipFlowComment();
Expand Down
7 changes: 6 additions & 1 deletion packages/babel-parser/src/plugins/placeholders.js
Expand Up @@ -5,6 +5,7 @@ import * as charCodes from "charcodes";
import { types as tt, TokenType } from "../tokenizer/types";
import type Parser from "../parser";
import * as N from "../types";
import { makeErrorTemplates } from "../parser/error";

tt.placeholder = new TokenType("%%", { startsExpr: true });

Expand Down Expand Up @@ -47,6 +48,10 @@ type NodeOf<T: PlaceholderTypes> = $Switch<
// the substituted nodes.
type MaybePlaceholder<T: PlaceholderTypes> = NodeOf<T>; // | Placeholder<T>

const PlaceHolderErrors = makeErrorTemplates({
ClassNameIsRequired: "A class name is required",
});

export default (superClass: Class<Parser>): Class<Parser> =>
class extends superClass {
parsePlaceholder<T: PlaceholderTypes>(
Expand Down Expand Up @@ -240,7 +245,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
node.body = this.finishPlaceholder(placeholder, "ClassBody");
return this.finishNode(node, type);
} else {
this.unexpected(null, "A class name is required");
this.unexpected(null, PlaceHolderErrors.ClassNameIsRequired);
}
} else {
this.parseClassId(node, isStatement, optionalId);
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-parser/src/tokenizer/index.js
Expand Up @@ -115,7 +115,7 @@ export default class Tokenizer extends ParserErrors {
// parser/util.js
/*::
+hasPrecedingLineBreak: () => boolean;
+unexpected: (pos?: ?number, messageOrType?: string | TokenType) => empty;
+unexpected: (pos?: ?number, messageOrType?: ErrorTemplate | TokenType) => empty;
+expectPlugin: (name: string, pos?: ?number) => true;
*/

Expand Down

0 comments on commit 0a70716

Please sign in to comment.