Unify reserved word checking and update error messages (#9402)
* Unify reserved word checking and update error messages * Fix test
This commit is contained in:
@@ -1,8 +1,6 @@
|
||||
// @flow
|
||||
|
||||
import type { Options } from "../options";
|
||||
import { isES2015ReservedWord } from "../util/identifier";
|
||||
|
||||
import type State from "../tokenizer/state";
|
||||
import type { PluginsMap } from "./index";
|
||||
|
||||
@@ -17,14 +15,6 @@ export default class BaseParser {
|
||||
// Initialized by Tokenizer
|
||||
state: State;
|
||||
|
||||
isReservedWord(word: string): boolean {
|
||||
if (word === "await") {
|
||||
return this.inModule;
|
||||
} else {
|
||||
return isES2015ReservedWord(word);
|
||||
}
|
||||
}
|
||||
|
||||
hasPlugin(name: string): boolean {
|
||||
return this.plugins.has(name);
|
||||
}
|
||||
|
||||
@@ -22,9 +22,10 @@ import { types as tt, type TokenType } from "../tokenizer/types";
|
||||
import * as N from "../types";
|
||||
import LValParser from "./lval";
|
||||
import {
|
||||
isKeyword,
|
||||
isReservedWord,
|
||||
isStrictReservedWord,
|
||||
isStrictBindReservedWord,
|
||||
isKeyword,
|
||||
} from "../util/identifier";
|
||||
import type { Pos, Position } from "../util/location";
|
||||
import * as charCodes from "charcodes";
|
||||
@@ -1988,43 +1989,45 @@ export default class ExpressionParser extends LValParser {
|
||||
checkKeywords: boolean,
|
||||
isBinding: boolean,
|
||||
): void {
|
||||
if (this.state.inGenerator && word === "yield") {
|
||||
const state = this.state;
|
||||
if (state.inGenerator && word === "yield") {
|
||||
this.raise(
|
||||
startLoc,
|
||||
"Can not use 'yield' as identifier inside a generator",
|
||||
);
|
||||
}
|
||||
|
||||
if (this.state.inAsync && word === "await") {
|
||||
if (state.inAsync && word === "await") {
|
||||
this.raise(
|
||||
startLoc,
|
||||
"Can not use 'await' as identifier inside an async function",
|
||||
);
|
||||
}
|
||||
|
||||
if (this.state.inClassProperty && word === "arguments") {
|
||||
if (state.inClassProperty && word === "arguments") {
|
||||
this.raise(
|
||||
startLoc,
|
||||
"'arguments' is not allowed in class field initializer",
|
||||
);
|
||||
}
|
||||
if (checkKeywords && isKeyword(word)) {
|
||||
this.raise(startLoc, `Unexpected keyword '${word}'`);
|
||||
}
|
||||
|
||||
if (this.isReservedWord(word) || (checkKeywords && isKeyword(word))) {
|
||||
if (!this.state.inAsync && word === "await") {
|
||||
const reservedTest = !state.strict
|
||||
? isReservedWord
|
||||
: isBinding
|
||||
? isStrictBindReservedWord
|
||||
: isStrictReservedWord;
|
||||
|
||||
if (reservedTest(word, this.inModule)) {
|
||||
if (!state.inAsync && word === "await") {
|
||||
this.raise(
|
||||
startLoc,
|
||||
"Can not use keyword 'await' outside an async function",
|
||||
);
|
||||
}
|
||||
this.raise(startLoc, word + " is a reserved word");
|
||||
}
|
||||
|
||||
if (
|
||||
this.state.strict &&
|
||||
(isStrictReservedWord(word) ||
|
||||
(isBinding && isStrictBindReservedWord(word)))
|
||||
) {
|
||||
this.raise(startLoc, word + " is a reserved word in strict mode");
|
||||
this.raise(startLoc, `Unexpected reserved word '${word}'`);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -14,10 +14,7 @@ import type {
|
||||
SpreadElement,
|
||||
} from "../types";
|
||||
import type { Pos, Position } from "../util/location";
|
||||
import {
|
||||
isStrictReservedWord,
|
||||
isStrictBindReservedWord,
|
||||
} from "../util/identifier";
|
||||
import { isStrictBindReservedWord } from "../util/identifier";
|
||||
import { NodeUtils } from "./node";
|
||||
|
||||
export default class LValParser extends NodeUtils {
|
||||
@@ -336,12 +333,13 @@ export default class LValParser extends NodeUtils {
|
||||
case "Identifier":
|
||||
if (
|
||||
this.state.strict &&
|
||||
(isStrictReservedWord(expr.name) ||
|
||||
isStrictBindReservedWord(expr.name))
|
||||
isStrictBindReservedWord(expr.name, this.inModule)
|
||||
) {
|
||||
this.raise(
|
||||
expr.start,
|
||||
expr.name + " is a reserved word in strict mode",
|
||||
`${isBinding ? "Binding" : "Assigning to"} '${
|
||||
expr.name
|
||||
}' in strict mode`,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user