Throw error for reserved words enum and await (#195)
* Throw error for reserved words enum and await when source type is module * Extract reserved word check into method * Fix tests
This commit is contained in:
committed by
Daniel Tschinder
parent
643124744f
commit
e260381e06
@@ -827,13 +827,7 @@ pp.parseObjPropValue = function (prop, startPos, startLoc, isGenerator, isAsync,
|
||||
|
||||
if (!prop.computed && prop.key.type === "Identifier") {
|
||||
if (isPattern) {
|
||||
let illegalBinding = this.isKeyword(prop.key.name);
|
||||
if (!illegalBinding && this.state.strict) {
|
||||
illegalBinding = reservedWords.strictBind(prop.key.name) || reservedWords.strict(prop.key.name);
|
||||
}
|
||||
if (illegalBinding) {
|
||||
this.raise(prop.key.start, "Binding " + prop.key.name);
|
||||
}
|
||||
this.checkReservedWord(prop.key.name, prop.key.start, true, true);
|
||||
prop.value = this.parseMaybeDefault(startPos, startLoc, prop.key.__clone());
|
||||
} else if (this.match(tt.eq) && refShorthandDefaultPos) {
|
||||
if (!refShorthandDefaultPos.start) {
|
||||
@@ -843,6 +837,7 @@ pp.parseObjPropValue = function (prop, startPos, startLoc, isGenerator, isAsync,
|
||||
} else {
|
||||
prop.value = prop.key.__clone();
|
||||
}
|
||||
|
||||
prop.shorthand = true;
|
||||
return this.finishNode(prop, "ObjectProperty");
|
||||
}
|
||||
@@ -998,8 +993,8 @@ pp.parseIdentifier = function (liberal) {
|
||||
let node = this.startNode();
|
||||
|
||||
if (this.match(tt.name)) {
|
||||
if (!liberal && this.state.strict && reservedWords.strict(this.state.value)) {
|
||||
this.raise(this.state.start, "The keyword '" + this.state.value + "' is reserved");
|
||||
if (!liberal) {
|
||||
this.checkReservedWord(this.state.value, this.state.start, false, false);
|
||||
}
|
||||
|
||||
node.name = this.state.value;
|
||||
@@ -1019,6 +1014,16 @@ pp.parseIdentifier = function (liberal) {
|
||||
return this.finishNode(node, "Identifier");
|
||||
};
|
||||
|
||||
pp.checkReservedWord = function (word, startLoc, checkKeywords, isBinding) {
|
||||
if (this.isReservedWord(word) || (checkKeywords && this.isKeyword(word))) {
|
||||
this.raise(startLoc, word + " is a reserved word");
|
||||
}
|
||||
|
||||
if (this.state.strict && (reservedWords.strict(word) || (isBinding && reservedWords.strictBind(word)))) {
|
||||
this.raise(startLoc, word + " is a reserved word in strict mode");
|
||||
}
|
||||
};
|
||||
|
||||
// Parses await expression inside async function.
|
||||
|
||||
pp.parseAwait = function (node) {
|
||||
|
||||
@@ -11,7 +11,6 @@ export default class Parser extends Tokenizer {
|
||||
|
||||
this.options = options;
|
||||
this.inModule = this.options.sourceType === "module";
|
||||
this.isReservedWord = reservedWords[6];
|
||||
this.input = input;
|
||||
this.plugins = this.loadPlugins(this.options.plugins);
|
||||
this.filename = options.sourceFilename;
|
||||
@@ -22,6 +21,14 @@ export default class Parser extends Tokenizer {
|
||||
}
|
||||
}
|
||||
|
||||
isReservedWord(word: string): boolean {
|
||||
if (word === "await") {
|
||||
return this.inModule;
|
||||
} else {
|
||||
return reservedWords[6](word);
|
||||
}
|
||||
}
|
||||
|
||||
hasPlugin(name: string): boolean {
|
||||
return !!(this.plugins["*"] || this.plugins[name]);
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
|
||||
import { types as tt } from "../tokenizer/types";
|
||||
import Parser from "./index";
|
||||
import { reservedWords } from "../util/identifier";
|
||||
|
||||
const pp = Parser.prototype;
|
||||
|
||||
@@ -204,9 +203,7 @@ pp.parseMaybeDefault = function (startPos, startLoc, left) {
|
||||
pp.checkLVal = function (expr, isBinding, checkClashes, contextDescription) {
|
||||
switch (expr.type) {
|
||||
case "Identifier":
|
||||
if (this.state.strict && (reservedWords.strictBind(expr.name) || reservedWords.strict(expr.name))) {
|
||||
this.raise(expr.start, (isBinding ? "Binding " : "Assigning to ") + expr.name + " in strict mode");
|
||||
}
|
||||
this.checkReservedWord(expr.name, expr.start, false, true);
|
||||
|
||||
if (checkClashes) {
|
||||
// we need to prefix this with an underscore for the cases where we have a key of
|
||||
|
||||
Reference in New Issue
Block a user