diff --git a/packages/babel-parser/src/parser/base.js b/packages/babel-parser/src/parser/base.js index 796abb0df2..f147cc88e7 100644 --- a/packages/babel-parser/src/parser/base.js +++ b/packages/babel-parser/src/parser/base.js @@ -1,7 +1,7 @@ // @flow import type { Options } from "../options"; -import { reservedWords } from "../util/identifier"; +import { isES2015ReservedWord } from "../util/identifier"; import type State from "../tokenizer/state"; import type { PluginsMap } from "./index"; @@ -21,7 +21,7 @@ export default class BaseParser { if (word === "await") { return this.inModule; } else { - return reservedWords[6](word); + return isES2015ReservedWord(word); } } diff --git a/packages/babel-parser/src/parser/expression.js b/packages/babel-parser/src/parser/expression.js index b65d057470..8f51c9b80e 100644 --- a/packages/babel-parser/src/parser/expression.js +++ b/packages/babel-parser/src/parser/expression.js @@ -21,7 +21,10 @@ import { types as tt, type TokenType } from "../tokenizer/types"; import * as N from "../types"; import LValParser from "./lval"; -import { reservedWords } from "../util/identifier"; +import { + isStrictReservedWord, + isStrictBindReservedWord, +} from "../util/identifier"; import type { Pos, Position } from "../util/location"; import * as charCodes from "charcodes"; @@ -1997,8 +2000,8 @@ export default class ExpressionParser extends LValParser { ): void { if ( this.state.strict && - (reservedWords.strict(word) || - (isBinding && reservedWords.strictBind(word))) + (isStrictReservedWord(word) || + (isBinding && isStrictBindReservedWord(word))) ) { this.raise(startLoc, word + " is a reserved word in strict mode"); } diff --git a/packages/babel-parser/src/util/identifier.js b/packages/babel-parser/src/util/identifier.js index a312c9ac8d..847a61f878 100644 --- a/packages/babel-parser/src/util/identifier.js +++ b/packages/babel-parser/src/util/identifier.js @@ -4,28 +4,72 @@ import * as charCodes from "charcodes"; -function makePredicate(words: string): (str: string) => boolean { - const wordsArr = words.split(" "); - return function(str) { - return wordsArr.indexOf(str) >= 0; - }; -} - -// Reserved word lists for various dialects of the language - -export const reservedWords = { - "6": makePredicate("enum await"), - strict: makePredicate( - "implements interface let package private protected public static yield", - ), - strictBind: makePredicate("eval arguments"), +export const isES2015ReservedWord = (word: string): boolean => { + return word === "enum" || word === "await"; }; -// And the keywords +const reservedWordsStrict = new Set([ + "implements", + "interface", + "let", + "package", + "private", + "protected", + "public", + "static", + "yield", +]); +export function isStrictReservedWord(word: string): boolean { + return reservedWordsStrict.has(word); +} -export const isKeyword = makePredicate( - "break case catch continue debugger default do else finally for function if return switch throw try var while with null true false instanceof typeof void delete new in this let const class extends export import yield super", -); +export function isStrictBindReservedWord(word: string): boolean { + return word === "eval" || word === "arguments"; +} + +const keywords = new Set([ + "break", + "case", + "catch", + "continue", + "debugger", + "default", + "do", + "else", + "finally", + "for", + "function", + "if", + "return", + "switch", + "throw", + "try", + "var", + "while", + "with", + "null", + "true", + "false", + "instanceof", + "typeof", + "void", + "delete", + "new", + "in", + "this", + "let", + "const", + "class", + "extends", + "export", + "import", + "yield", + "super", +]); + +export function isKeyword(word: string): boolean { + return keywords.has(word); +} // ## Character categories diff --git a/packages/babel-parser/test/unit/util/identifier.js b/packages/babel-parser/test/unit/util/identifier.js new file mode 100644 index 0000000000..a0d6bb16d2 --- /dev/null +++ b/packages/babel-parser/test/unit/util/identifier.js @@ -0,0 +1,18 @@ +import { isKeyword } from "../../../src/util/identifier"; + +describe("identifier", () => { + describe("isKeyword", () => { + it("break is a keyword", () => { + expect(isKeyword("break")).toBe(true); + }); + it("let is a keyword", () => { + expect(isKeyword("let")).toBe(true); + }); + it("super is a keyword", () => { + expect(isKeyword("super")).toBe(true); + }); + it("abc is not a keyword", () => { + expect(isKeyword("abc")).toBe(false); + }); + }); +});