Skip to content
This repository has been archived by the owner on May 19, 2018. It is now read-only.

Commit

Permalink
Throw error for reserved words enum and await (#195)
Browse files Browse the repository at this point in the history
* Throw error for reserved words enum and await when source type is module

* Extract reserved word check into method

* Fix tests
  • Loading branch information
kaicataldo authored and danez committed Nov 9, 2016
1 parent 6431247 commit e260381
Show file tree
Hide file tree
Showing 189 changed files with 1,460 additions and 231 deletions.
23 changes: 14 additions & 9 deletions src/parser/expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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");
}
Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand Down
9 changes: 8 additions & 1 deletion src/parser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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]);
}
Expand Down
5 changes: 1 addition & 4 deletions src/parser/lval.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import { types as tt } from "../tokenizer/types";
import Parser from "./index";
import { reservedWords } from "../util/identifier";

const pp = Parser.prototype;

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/core/uncategorised/428/options.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Unexpected token (1:8)"
}
}
4 changes: 2 additions & 2 deletions test/fixtures/core/uncategorised/468/options.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Binding eval in strict mode (1:36)"
}
"throws": "eval is a reserved word in strict mode (1:36)"
}
4 changes: 2 additions & 2 deletions test/fixtures/core/uncategorised/469/options.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Binding arguments in strict mode (1:36)"
}
"throws": "arguments is a reserved word in strict mode (1:36)"
}
4 changes: 2 additions & 2 deletions test/fixtures/core/uncategorised/470/options.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Binding eval in strict mode (1:47)"
}
"throws": "eval is a reserved word in strict mode (1:47)"
}
4 changes: 2 additions & 2 deletions test/fixtures/core/uncategorised/471/options.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Binding arguments in strict mode (1:47)"
}
"throws": "arguments is a reserved word in strict mode (1:47)"
}
4 changes: 2 additions & 2 deletions test/fixtures/core/uncategorised/472/options.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Assigning to eval in strict mode (1:32)"
}
"throws": "eval is a reserved word in strict mode (1:32)"
}
4 changes: 2 additions & 2 deletions test/fixtures/core/uncategorised/473/options.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Assigning to arguments in strict mode (1:32)"
}
"throws": "arguments is a reserved word in strict mode (1:32)"
}
4 changes: 2 additions & 2 deletions test/fixtures/core/uncategorised/474/options.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Assigning to eval in strict mode (1:34)"
}
"throws": "eval is a reserved word in strict mode (1:34)"
}
4 changes: 2 additions & 2 deletions test/fixtures/core/uncategorised/475/options.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Assigning to eval in strict mode (1:34)"
}
"throws": "eval is a reserved word in strict mode (1:34)"
}
4 changes: 2 additions & 2 deletions test/fixtures/core/uncategorised/476/options.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Assigning to arguments in strict mode (1:34)"
}
"throws": "arguments is a reserved word in strict mode (1:34)"
}
4 changes: 2 additions & 2 deletions test/fixtures/core/uncategorised/477/options.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Assigning to arguments in strict mode (1:34)"
}
"throws": "arguments is a reserved word in strict mode (1:34)"
}
4 changes: 2 additions & 2 deletions test/fixtures/core/uncategorised/478/options.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Assigning to eval in strict mode (1:32)"
}
"throws": "eval is a reserved word in strict mode (1:32)"
}
4 changes: 2 additions & 2 deletions test/fixtures/core/uncategorised/479/options.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Assigning to eval in strict mode (1:32)"
}
"throws": "eval is a reserved word in strict mode (1:32)"
}
4 changes: 2 additions & 2 deletions test/fixtures/core/uncategorised/480/options.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Assigning to arguments in strict mode (1:32)"
}
"throws": "arguments is a reserved word in strict mode (1:32)"
}
4 changes: 2 additions & 2 deletions test/fixtures/core/uncategorised/481/options.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Assigning to arguments in strict mode (1:32)"
}
"throws": "arguments is a reserved word in strict mode (1:32)"
}
4 changes: 2 additions & 2 deletions test/fixtures/core/uncategorised/482/options.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Binding eval in strict mode (1:41)"
}
"throws": "eval is a reserved word in strict mode (1:41)"
}
4 changes: 2 additions & 2 deletions test/fixtures/core/uncategorised/483/options.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Binding arguments in strict mode (1:41)"
}
"throws": "arguments is a reserved word in strict mode (1:41)"
}
4 changes: 2 additions & 2 deletions test/fixtures/core/uncategorised/484/options.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Binding eval in strict mode (1:9)"
}
"throws": "eval is a reserved word in strict mode (1:9)"
}
4 changes: 2 additions & 2 deletions test/fixtures/core/uncategorised/485/options.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Binding arguments in strict mode (1:9)"
}
"throws": "arguments is a reserved word in strict mode (1:9)"
}
4 changes: 2 additions & 2 deletions test/fixtures/core/uncategorised/486/options.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Binding eval in strict mode (1:42)"
}
"throws": "eval is a reserved word in strict mode (1:42)"
}
4 changes: 2 additions & 2 deletions test/fixtures/core/uncategorised/487/options.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Binding arguments in strict mode (1:42)"
}
"throws": "arguments is a reserved word in strict mode (1:42)"
}
4 changes: 2 additions & 2 deletions test/fixtures/core/uncategorised/488/options.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Binding eval in strict mode (1:10)"
}
"throws": "eval is a reserved word in strict mode (1:10)"
}
4 changes: 2 additions & 2 deletions test/fixtures/core/uncategorised/489/options.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Binding arguments in strict mode (1:10)"
}
"throws": "arguments is a reserved word in strict mode (1:10)"
}
4 changes: 2 additions & 2 deletions test/fixtures/core/uncategorised/490/options.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Binding eval in strict mode (1:47)"
}
"throws": "eval is a reserved word in strict mode (1:47)"
}
4 changes: 2 additions & 2 deletions test/fixtures/core/uncategorised/491/options.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Binding package in strict mode (1:10)"
}
"throws": "package is a reserved word in strict mode (1:10)"
}
4 changes: 2 additions & 2 deletions test/fixtures/core/uncategorised/492/options.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Binding eval in strict mode (1:48)"
}
"throws": "eval is a reserved word in strict mode (1:48)"
}
4 changes: 2 additions & 2 deletions test/fixtures/core/uncategorised/493/options.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Binding eval in strict mode (1:41)"
}
"throws": "eval is a reserved word in strict mode (1:41)"
}
4 changes: 2 additions & 2 deletions test/fixtures/core/uncategorised/494/options.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Binding eval in strict mode (1:49)"
}
"throws": "eval is a reserved word in strict mode (1:49)"
}
4 changes: 2 additions & 2 deletions test/fixtures/core/uncategorised/495/options.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Binding eval in strict mode (1:15)"
}
"throws": "eval is a reserved word in strict mode (1:15)"
}
4 changes: 2 additions & 2 deletions test/fixtures/core/uncategorised/496/options.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Binding arguments in strict mode (1:15)"
}
"throws": "arguments is a reserved word in strict mode (1:15)"
}
4 changes: 2 additions & 2 deletions test/fixtures/core/uncategorised/497/options.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Binding eval in strict mode (1:48)"
}
"throws": "eval is a reserved word in strict mode (1:48)"
}
4 changes: 2 additions & 2 deletions test/fixtures/core/uncategorised/498/options.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Binding arguments in strict mode (1:48)"
}
"throws": "arguments is a reserved word in strict mode (1:48)"
}
2 changes: 1 addition & 1 deletion test/fixtures/core/uncategorised/504/options.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Binding implements in strict mode (1:37)"
"throws": "implements is a reserved word in strict mode (1:37)"
}
2 changes: 1 addition & 1 deletion test/fixtures/core/uncategorised/505/options.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Binding interface in strict mode (1:37)"
"throws": "interface is a reserved word in strict mode (1:37)"
}
2 changes: 1 addition & 1 deletion test/fixtures/core/uncategorised/506/options.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Binding package in strict mode (1:37)"
"throws": "package is a reserved word in strict mode (1:37)"
}
2 changes: 1 addition & 1 deletion test/fixtures/core/uncategorised/507/options.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Binding private in strict mode (1:37)"
"throws": "private is a reserved word in strict mode (1:37)"
}
2 changes: 1 addition & 1 deletion test/fixtures/core/uncategorised/508/options.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Binding protected in strict mode (1:37)"
"throws": "protected is a reserved word in strict mode (1:37)"
}
2 changes: 1 addition & 1 deletion test/fixtures/core/uncategorised/509/options.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Binding public in strict mode (1:37)"
"throws": "public is a reserved word in strict mode (1:37)"
}
2 changes: 1 addition & 1 deletion test/fixtures/core/uncategorised/510/options.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Binding static in strict mode (1:37)"
"throws": "static is a reserved word in strict mode (1:37)"
}
4 changes: 2 additions & 2 deletions test/fixtures/core/uncategorised/511/options.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Binding static in strict mode (1:15)"
}
"throws": "static is a reserved word in strict mode (1:15)"
}
4 changes: 2 additions & 2 deletions test/fixtures/core/uncategorised/512/options.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Binding static in strict mode (1:9)"
}
"throws": "static is a reserved word in strict mode (1:9)"
}
2 changes: 1 addition & 1 deletion test/fixtures/core/uncategorised/513/options.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "The keyword 'static' is reserved (1:23)"
"throws": "static is a reserved word in strict mode (1:23)"
}
4 changes: 2 additions & 2 deletions test/fixtures/core/uncategorised/515/options.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Binding eval in strict mode (1:11)"
}
"throws": "eval is a reserved word in strict mode (1:11)"
}
4 changes: 2 additions & 2 deletions test/fixtures/core/uncategorised/516/options.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Binding package in strict mode (1:11)"
}
"throws": "package is a reserved word in strict mode (1:11)"
}
4 changes: 2 additions & 2 deletions test/fixtures/core/uncategorised/520/options.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Binding eval in strict mode (1:12)"
}
"throws": "eval is a reserved word in strict mode (1:12)"
}
4 changes: 2 additions & 2 deletions test/fixtures/core/uncategorised/521/options.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Binding package in strict mode (1:12)"
}
"throws": "package is a reserved word in strict mode (1:12)"
}
Loading

0 comments on commit e260381

Please sign in to comment.