Merge pull request #9375 from danez/contextual-let
Make let a contextual keyword
This commit is contained in:
commit
93e1b5e612
@ -3,8 +3,13 @@
|
||||
import * as N from "../types";
|
||||
import { types as tt, type TokenType } from "../tokenizer/types";
|
||||
import ExpressionParser from "./expression";
|
||||
import { isIdentifierChar } from "../util/identifier";
|
||||
import {
|
||||
isIdentifierChar,
|
||||
isIdentifierStart,
|
||||
keywordRelationalOperator,
|
||||
} from "../util/identifier";
|
||||
import { lineBreak, skipWhiteSpace } from "../util/whitespace";
|
||||
import * as charCodes from "charcodes";
|
||||
|
||||
// Reused empty array added for node fields that are always empty.
|
||||
|
||||
@ -71,6 +76,39 @@ export default class StatementParser extends ExpressionParser {
|
||||
return this.finishNode(node, "InterpreterDirective");
|
||||
}
|
||||
|
||||
isLet(declaration?: boolean): boolean {
|
||||
if (!this.isContextual("let")) {
|
||||
return false;
|
||||
}
|
||||
skipWhiteSpace.lastIndex = this.state.pos;
|
||||
const skip = skipWhiteSpace.exec(this.state.input);
|
||||
// $FlowIgnore
|
||||
const next = this.state.pos + skip[0].length;
|
||||
const nextCh = this.state.input.charCodeAt(next);
|
||||
if (
|
||||
(nextCh === charCodes.leftCurlyBrace &&
|
||||
!lineBreak.test(this.state.input.slice(this.state.end, next))) ||
|
||||
nextCh === charCodes.leftSquareBracket
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
if (isIdentifierStart(nextCh)) {
|
||||
if (
|
||||
!declaration &&
|
||||
lineBreak.test(this.state.input.slice(this.state.end, next))
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
let pos = next + 1;
|
||||
while (isIdentifierChar(this.state.input.charCodeAt(pos))) {
|
||||
++pos;
|
||||
}
|
||||
const ident = this.state.input.slice(next, pos);
|
||||
if (!keywordRelationalOperator.test(ident)) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Parse a single statement.
|
||||
//
|
||||
// If expecting a statement and finding a slash operator, parse a
|
||||
@ -86,8 +124,14 @@ export default class StatementParser extends ExpressionParser {
|
||||
}
|
||||
|
||||
parseStatementContent(declaration: boolean, topLevel: ?boolean): N.Statement {
|
||||
const starttype = this.state.type;
|
||||
let starttype = this.state.type;
|
||||
const node = this.startNode();
|
||||
let kind;
|
||||
|
||||
if (this.isLet(declaration)) {
|
||||
starttype = tt._var;
|
||||
kind = "let";
|
||||
}
|
||||
|
||||
// Most types of statements are recognized by the keyword they
|
||||
// start with. Many are trivial to parse, some require a bit of
|
||||
@ -129,12 +173,11 @@ export default class StatementParser extends ExpressionParser {
|
||||
case tt._try:
|
||||
return this.parseTryStatement(node);
|
||||
|
||||
case tt._let:
|
||||
case tt._const:
|
||||
if (!declaration) this.unexpected(); // NOTE: falls through to _var
|
||||
|
||||
case tt._var:
|
||||
return this.parseVarStatement(node, starttype);
|
||||
kind = kind || this.state.value;
|
||||
if (!declaration && kind !== "var") this.unexpected();
|
||||
return this.parseVarStatement(node, kind);
|
||||
|
||||
case tt._while:
|
||||
return this.parseWhileStatement(node);
|
||||
@ -424,18 +467,19 @@ export default class StatementParser extends ExpressionParser {
|
||||
return this.parseFor(node, null);
|
||||
}
|
||||
|
||||
if (this.match(tt._var) || this.match(tt._let) || this.match(tt._const)) {
|
||||
const isLet = this.isLet();
|
||||
if (this.match(tt._var) || this.match(tt._const) || isLet) {
|
||||
const init = this.startNode();
|
||||
const varKind = this.state.type;
|
||||
const kind = isLet ? "let" : this.state.value;
|
||||
this.next();
|
||||
this.parseVar(init, true, varKind);
|
||||
this.parseVar(init, true, kind);
|
||||
this.finishNode(init, "VariableDeclaration");
|
||||
|
||||
if (this.match(tt._in) || this.isContextual("of")) {
|
||||
if (init.declarations.length === 1) {
|
||||
const declaration = init.declarations[0];
|
||||
const isForInInitializer =
|
||||
varKind === tt._var &&
|
||||
kind === "var" &&
|
||||
declaration.init &&
|
||||
declaration.id.type != "ObjectPattern" &&
|
||||
declaration.id.type != "ArrayPattern" &&
|
||||
@ -606,7 +650,7 @@ export default class StatementParser extends ExpressionParser {
|
||||
|
||||
parseVarStatement(
|
||||
node: N.VariableDeclaration,
|
||||
kind: TokenType,
|
||||
kind: "var" | "let" | "const",
|
||||
): N.VariableDeclaration {
|
||||
this.next();
|
||||
this.parseVar(node, false, kind);
|
||||
@ -862,20 +906,19 @@ export default class StatementParser extends ExpressionParser {
|
||||
parseVar(
|
||||
node: N.VariableDeclaration,
|
||||
isFor: boolean,
|
||||
kind: TokenType,
|
||||
kind: "var" | "let" | "const",
|
||||
): N.VariableDeclaration {
|
||||
const declarations = (node.declarations = []);
|
||||
const isTypescript = this.hasPlugin("typescript");
|
||||
// $FlowFixMe
|
||||
node.kind = kind.keyword;
|
||||
node.kind = kind;
|
||||
for (;;) {
|
||||
const decl = this.startNode();
|
||||
this.parseVarHead(decl);
|
||||
this.parseVarId(decl, kind);
|
||||
if (this.eat(tt.eq)) {
|
||||
decl.init = this.parseMaybeAssign(isFor);
|
||||
} else {
|
||||
if (
|
||||
kind === tt._const &&
|
||||
kind === "const" &&
|
||||
!(this.match(tt._in) || this.isContextual("of"))
|
||||
) {
|
||||
// `const` with no initializer is allowed in TypeScript.
|
||||
@ -900,7 +943,10 @@ export default class StatementParser extends ExpressionParser {
|
||||
return node;
|
||||
}
|
||||
|
||||
parseVarHead(decl: N.VariableDeclarator): void {
|
||||
parseVarId(decl: N.VariableDeclarator, kind: "var" | "let" | "const"): void {
|
||||
if ((kind === "const" || kind === "let") && this.isContextual("let")) {
|
||||
this.unexpected(null, "let is disallowed as a lexically bound name");
|
||||
}
|
||||
decl.id = this.parseBindingAtom();
|
||||
this.checkLVal(decl.id, true, undefined, "variable declaration");
|
||||
}
|
||||
@ -1008,12 +1054,12 @@ export default class StatementParser extends ExpressionParser {
|
||||
): T {
|
||||
this.next();
|
||||
this.takeDecorators(node);
|
||||
this.parseClassId(node, isStatement, optionalId);
|
||||
|
||||
// class bodies and heritages are implicitly strict
|
||||
// A class definition is always strict mode code.
|
||||
const oldStrict = this.state.strict;
|
||||
this.state.strict = true;
|
||||
|
||||
this.parseClassId(node, isStatement, optionalId);
|
||||
this.parseClassSuper(node);
|
||||
this.parseClassBody(node);
|
||||
|
||||
@ -1638,11 +1684,7 @@ export default class StatementParser extends ExpressionParser {
|
||||
}
|
||||
this.parseDecorators(false);
|
||||
return this.parseClass(expr, true, true);
|
||||
} else if (
|
||||
this.match(tt._let) ||
|
||||
this.match(tt._const) ||
|
||||
this.match(tt._var)
|
||||
) {
|
||||
} else if (this.match(tt._const) || this.match(tt._var) || this.isLet()) {
|
||||
return this.raise(
|
||||
this.state.start,
|
||||
"Only expressions, functions or classes are allowed as the `default` export.",
|
||||
@ -1661,7 +1703,7 @@ export default class StatementParser extends ExpressionParser {
|
||||
|
||||
isExportDefaultSpecifier(): boolean {
|
||||
if (this.match(tt.name)) {
|
||||
return this.state.value !== "async";
|
||||
return this.state.value !== "async" && this.state.value !== "let";
|
||||
}
|
||||
|
||||
if (!this.match(tt._default)) {
|
||||
@ -1710,9 +1752,9 @@ export default class StatementParser extends ExpressionParser {
|
||||
return (
|
||||
this.state.type.keyword === "var" ||
|
||||
this.state.type.keyword === "const" ||
|
||||
this.state.type.keyword === "let" ||
|
||||
this.state.type.keyword === "function" ||
|
||||
this.state.type.keyword === "class" ||
|
||||
this.isLet() ||
|
||||
this.isAsyncFunction()
|
||||
);
|
||||
}
|
||||
|
||||
@ -344,7 +344,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
||||
} else {
|
||||
if (
|
||||
this.match(tt._const) ||
|
||||
this.match(tt._let) ||
|
||||
this.isLet() ||
|
||||
((this.isContextual("type") || this.isContextual("interface")) &&
|
||||
!insideModule)
|
||||
) {
|
||||
@ -2344,8 +2344,11 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
||||
}
|
||||
|
||||
// parse flow type annotations on variable declarator heads - let foo: string = bar
|
||||
parseVarHead(decl: N.VariableDeclarator): void {
|
||||
super.parseVarHead(decl);
|
||||
parseVarId(
|
||||
decl: N.VariableDeclarator,
|
||||
kind: "var" | "let" | "const",
|
||||
): void {
|
||||
super.parseVarId(decl, kind);
|
||||
if (this.match(tt.colon)) {
|
||||
decl.id.typeAnnotation = this.flowParseTypeAnnotation();
|
||||
this.finishNode(decl.id, decl.id.type);
|
||||
|
||||
@ -1190,8 +1190,15 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
||||
if (this.isLineTerminator()) {
|
||||
return;
|
||||
}
|
||||
let starttype = this.state.type;
|
||||
let kind;
|
||||
|
||||
switch (this.state.type) {
|
||||
if (this.isContextual("let")) {
|
||||
starttype = tt._var;
|
||||
kind = "let";
|
||||
}
|
||||
|
||||
switch (starttype) {
|
||||
case tt._function:
|
||||
this.next();
|
||||
return this.parseFunction(nany, /* isStatement */ true);
|
||||
@ -1210,8 +1217,8 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
||||
}
|
||||
// falls through
|
||||
case tt._var:
|
||||
case tt._let:
|
||||
return this.parseVarStatement(nany, this.state.type);
|
||||
kind = kind || this.state.value;
|
||||
return this.parseVarStatement(nany, kind);
|
||||
case tt.name: {
|
||||
const value = this.state.value;
|
||||
if (value === "global") {
|
||||
@ -1941,8 +1948,11 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
||||
}
|
||||
|
||||
// `let x: number;`
|
||||
parseVarHead(decl: N.VariableDeclarator): void {
|
||||
super.parseVarHead(decl);
|
||||
parseVarId(
|
||||
decl: N.VariableDeclarator,
|
||||
kind: "var" | "let" | "const",
|
||||
): void {
|
||||
super.parseVarId(decl, kind);
|
||||
if (decl.id.type === "Identifier" && this.eat(tt.bang)) {
|
||||
decl.definite = true;
|
||||
}
|
||||
|
||||
@ -1384,8 +1384,8 @@ export default class Tokenizer extends LocationParser {
|
||||
|
||||
if (
|
||||
prevType === tt._var ||
|
||||
prevType === tt._let ||
|
||||
prevType === tt._const
|
||||
prevType === tt._const ||
|
||||
prevType === tt.name
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -180,7 +180,6 @@ export const keywords = Object.create(null, {
|
||||
throw: makeKeywordProps("throw", { beforeExpr, prefix, startsExpr }),
|
||||
try: makeKeywordProps("try"),
|
||||
var: makeKeywordProps("var"),
|
||||
let: makeKeywordProps("let"),
|
||||
const: makeKeywordProps("const"),
|
||||
while: makeKeywordProps("while", { isLoop }),
|
||||
with: makeKeywordProps("with"),
|
||||
|
||||
@ -57,7 +57,6 @@ const keywords = new Set([
|
||||
"new",
|
||||
"in",
|
||||
"this",
|
||||
"let",
|
||||
"const",
|
||||
"class",
|
||||
"extends",
|
||||
@ -71,6 +70,8 @@ export function isKeyword(word: string): boolean {
|
||||
return keywords.has(word);
|
||||
}
|
||||
|
||||
export const keywordRelationalOperator = /^in(stanceof)?$/;
|
||||
|
||||
// ## Character categories
|
||||
|
||||
// Big ugly regular expressions that matches characters in the
|
||||
|
||||
1
packages/babel-parser/test/fixtures/es2015/let/let-as-identifier-1/input.js
vendored
Normal file
1
packages/babel-parser/test/fixtures/es2015/let/let-as-identifier-1/input.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
let + 1
|
||||
103
packages/babel-parser/test/fixtures/es2015/let/let-as-identifier-1/output.json
vendored
Normal file
103
packages/babel-parser/test/fixtures/es2015/let/let-as-identifier-1/output.json
vendored
Normal file
@ -0,0 +1,103 @@
|
||||
{
|
||||
"type": "File",
|
||||
"start": 0,
|
||||
"end": 7,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 7
|
||||
}
|
||||
},
|
||||
"program": {
|
||||
"type": "Program",
|
||||
"start": 0,
|
||||
"end": 7,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 7
|
||||
}
|
||||
},
|
||||
"sourceType": "script",
|
||||
"interpreter": null,
|
||||
"body": [
|
||||
{
|
||||
"type": "ExpressionStatement",
|
||||
"start": 0,
|
||||
"end": 7,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 7
|
||||
}
|
||||
},
|
||||
"expression": {
|
||||
"type": "BinaryExpression",
|
||||
"start": 0,
|
||||
"end": 7,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 7
|
||||
}
|
||||
},
|
||||
"left": {
|
||||
"type": "Identifier",
|
||||
"start": 0,
|
||||
"end": 3,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 3
|
||||
},
|
||||
"identifierName": "let"
|
||||
},
|
||||
"name": "let"
|
||||
},
|
||||
"operator": "+",
|
||||
"right": {
|
||||
"type": "NumericLiteral",
|
||||
"start": 6,
|
||||
"end": 7,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 6
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 7
|
||||
}
|
||||
},
|
||||
"extra": {
|
||||
"rawValue": 1,
|
||||
"raw": "1"
|
||||
},
|
||||
"value": 1
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"directives": []
|
||||
}
|
||||
}
|
||||
1
packages/babel-parser/test/fixtures/es2015/let/let-as-identifier-2/input.js
vendored
Normal file
1
packages/babel-parser/test/fixtures/es2015/let/let-as-identifier-2/input.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
var let = 1
|
||||
105
packages/babel-parser/test/fixtures/es2015/let/let-as-identifier-2/output.json
vendored
Normal file
105
packages/babel-parser/test/fixtures/es2015/let/let-as-identifier-2/output.json
vendored
Normal file
@ -0,0 +1,105 @@
|
||||
{
|
||||
"type": "File",
|
||||
"start": 0,
|
||||
"end": 11,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 11
|
||||
}
|
||||
},
|
||||
"program": {
|
||||
"type": "Program",
|
||||
"start": 0,
|
||||
"end": 11,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 11
|
||||
}
|
||||
},
|
||||
"sourceType": "script",
|
||||
"interpreter": null,
|
||||
"body": [
|
||||
{
|
||||
"type": "VariableDeclaration",
|
||||
"start": 0,
|
||||
"end": 11,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 11
|
||||
}
|
||||
},
|
||||
"declarations": [
|
||||
{
|
||||
"type": "VariableDeclarator",
|
||||
"start": 4,
|
||||
"end": 11,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 4
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 11
|
||||
}
|
||||
},
|
||||
"id": {
|
||||
"type": "Identifier",
|
||||
"start": 4,
|
||||
"end": 7,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 4
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 7
|
||||
},
|
||||
"identifierName": "let"
|
||||
},
|
||||
"name": "let"
|
||||
},
|
||||
"init": {
|
||||
"type": "NumericLiteral",
|
||||
"start": 10,
|
||||
"end": 11,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 10
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 11
|
||||
}
|
||||
},
|
||||
"extra": {
|
||||
"rawValue": 1,
|
||||
"raw": "1"
|
||||
},
|
||||
"value": 1
|
||||
}
|
||||
}
|
||||
],
|
||||
"kind": "var"
|
||||
}
|
||||
],
|
||||
"directives": []
|
||||
}
|
||||
}
|
||||
1
packages/babel-parser/test/fixtures/es2015/let/let-as-identifier-3/input.js
vendored
Normal file
1
packages/babel-parser/test/fixtures/es2015/let/let-as-identifier-3/input.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
let instanceof Foo
|
||||
100
packages/babel-parser/test/fixtures/es2015/let/let-as-identifier-3/output.json
vendored
Normal file
100
packages/babel-parser/test/fixtures/es2015/let/let-as-identifier-3/output.json
vendored
Normal file
@ -0,0 +1,100 @@
|
||||
{
|
||||
"type": "File",
|
||||
"start": 0,
|
||||
"end": 18,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 18
|
||||
}
|
||||
},
|
||||
"program": {
|
||||
"type": "Program",
|
||||
"start": 0,
|
||||
"end": 18,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 18
|
||||
}
|
||||
},
|
||||
"sourceType": "script",
|
||||
"interpreter": null,
|
||||
"body": [
|
||||
{
|
||||
"type": "ExpressionStatement",
|
||||
"start": 0,
|
||||
"end": 18,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 18
|
||||
}
|
||||
},
|
||||
"expression": {
|
||||
"type": "BinaryExpression",
|
||||
"start": 0,
|
||||
"end": 18,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 18
|
||||
}
|
||||
},
|
||||
"left": {
|
||||
"type": "Identifier",
|
||||
"start": 0,
|
||||
"end": 3,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 3
|
||||
},
|
||||
"identifierName": "let"
|
||||
},
|
||||
"name": "let"
|
||||
},
|
||||
"operator": "instanceof",
|
||||
"right": {
|
||||
"type": "Identifier",
|
||||
"start": 15,
|
||||
"end": 18,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 15
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 18
|
||||
},
|
||||
"identifierName": "Foo"
|
||||
},
|
||||
"name": "Foo"
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"directives": []
|
||||
}
|
||||
}
|
||||
1
packages/babel-parser/test/fixtures/es2015/let/let-as-identifier-4/input.js
vendored
Normal file
1
packages/babel-parser/test/fixtures/es2015/let/let-as-identifier-4/input.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
let in {}
|
||||
99
packages/babel-parser/test/fixtures/es2015/let/let-as-identifier-4/output.json
vendored
Normal file
99
packages/babel-parser/test/fixtures/es2015/let/let-as-identifier-4/output.json
vendored
Normal file
@ -0,0 +1,99 @@
|
||||
{
|
||||
"type": "File",
|
||||
"start": 0,
|
||||
"end": 9,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 9
|
||||
}
|
||||
},
|
||||
"program": {
|
||||
"type": "Program",
|
||||
"start": 0,
|
||||
"end": 9,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 9
|
||||
}
|
||||
},
|
||||
"sourceType": "script",
|
||||
"interpreter": null,
|
||||
"body": [
|
||||
{
|
||||
"type": "ExpressionStatement",
|
||||
"start": 0,
|
||||
"end": 9,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 9
|
||||
}
|
||||
},
|
||||
"expression": {
|
||||
"type": "BinaryExpression",
|
||||
"start": 0,
|
||||
"end": 9,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 9
|
||||
}
|
||||
},
|
||||
"left": {
|
||||
"type": "Identifier",
|
||||
"start": 0,
|
||||
"end": 3,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 3
|
||||
},
|
||||
"identifierName": "let"
|
||||
},
|
||||
"name": "let"
|
||||
},
|
||||
"operator": "in",
|
||||
"right": {
|
||||
"type": "ObjectExpression",
|
||||
"start": 7,
|
||||
"end": 9,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 7
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 9
|
||||
}
|
||||
},
|
||||
"properties": []
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"directives": []
|
||||
}
|
||||
}
|
||||
2
packages/babel-parser/test/fixtures/es2015/let/let-as-identifier-5/input.js
vendored
Normal file
2
packages/babel-parser/test/fixtures/es2015/let/let-as-identifier-5/input.js
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
if (1) let
|
||||
{}
|
||||
120
packages/babel-parser/test/fixtures/es2015/let/let-as-identifier-5/output.json
vendored
Normal file
120
packages/babel-parser/test/fixtures/es2015/let/let-as-identifier-5/output.json
vendored
Normal file
@ -0,0 +1,120 @@
|
||||
{
|
||||
"type": "File",
|
||||
"start": 0,
|
||||
"end": 13,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 2,
|
||||
"column": 2
|
||||
}
|
||||
},
|
||||
"program": {
|
||||
"type": "Program",
|
||||
"start": 0,
|
||||
"end": 13,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 2,
|
||||
"column": 2
|
||||
}
|
||||
},
|
||||
"sourceType": "script",
|
||||
"interpreter": null,
|
||||
"body": [
|
||||
{
|
||||
"type": "IfStatement",
|
||||
"start": 0,
|
||||
"end": 10,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 10
|
||||
}
|
||||
},
|
||||
"test": {
|
||||
"type": "NumericLiteral",
|
||||
"start": 4,
|
||||
"end": 5,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 4
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 5
|
||||
}
|
||||
},
|
||||
"extra": {
|
||||
"rawValue": 1,
|
||||
"raw": "1"
|
||||
},
|
||||
"value": 1
|
||||
},
|
||||
"consequent": {
|
||||
"type": "ExpressionStatement",
|
||||
"start": 7,
|
||||
"end": 10,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 7
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 10
|
||||
}
|
||||
},
|
||||
"expression": {
|
||||
"type": "Identifier",
|
||||
"start": 7,
|
||||
"end": 10,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 7
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 10
|
||||
},
|
||||
"identifierName": "let"
|
||||
},
|
||||
"name": "let"
|
||||
}
|
||||
},
|
||||
"alternate": null
|
||||
},
|
||||
{
|
||||
"type": "BlockStatement",
|
||||
"start": 11,
|
||||
"end": 13,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 2,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 2,
|
||||
"column": 2
|
||||
}
|
||||
},
|
||||
"body": [],
|
||||
"directives": []
|
||||
}
|
||||
],
|
||||
"directives": []
|
||||
}
|
||||
}
|
||||
2
packages/babel-parser/test/fixtures/es2015/let/let-as-identifier-6/input.js
vendored
Normal file
2
packages/babel-parser/test/fixtures/es2015/let/let-as-identifier-6/input.js
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
while (0) let
|
||||
foo
|
||||
134
packages/babel-parser/test/fixtures/es2015/let/let-as-identifier-6/output.json
vendored
Normal file
134
packages/babel-parser/test/fixtures/es2015/let/let-as-identifier-6/output.json
vendored
Normal file
@ -0,0 +1,134 @@
|
||||
{
|
||||
"type": "File",
|
||||
"start": 0,
|
||||
"end": 17,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 2,
|
||||
"column": 3
|
||||
}
|
||||
},
|
||||
"program": {
|
||||
"type": "Program",
|
||||
"start": 0,
|
||||
"end": 17,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 2,
|
||||
"column": 3
|
||||
}
|
||||
},
|
||||
"sourceType": "script",
|
||||
"interpreter": null,
|
||||
"body": [
|
||||
{
|
||||
"type": "WhileStatement",
|
||||
"start": 0,
|
||||
"end": 13,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 13
|
||||
}
|
||||
},
|
||||
"test": {
|
||||
"type": "NumericLiteral",
|
||||
"start": 7,
|
||||
"end": 8,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 7
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 8
|
||||
}
|
||||
},
|
||||
"extra": {
|
||||
"rawValue": 0,
|
||||
"raw": "0"
|
||||
},
|
||||
"value": 0
|
||||
},
|
||||
"body": {
|
||||
"type": "ExpressionStatement",
|
||||
"start": 10,
|
||||
"end": 13,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 10
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 13
|
||||
}
|
||||
},
|
||||
"expression": {
|
||||
"type": "Identifier",
|
||||
"start": 10,
|
||||
"end": 13,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 10
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 13
|
||||
},
|
||||
"identifierName": "let"
|
||||
},
|
||||
"name": "let"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "ExpressionStatement",
|
||||
"start": 14,
|
||||
"end": 17,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 2,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 2,
|
||||
"column": 3
|
||||
}
|
||||
},
|
||||
"expression": {
|
||||
"type": "Identifier",
|
||||
"start": 14,
|
||||
"end": 17,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 2,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 2,
|
||||
"column": 3
|
||||
},
|
||||
"identifierName": "foo"
|
||||
},
|
||||
"name": "foo"
|
||||
}
|
||||
}
|
||||
],
|
||||
"directives": []
|
||||
}
|
||||
}
|
||||
2
packages/babel-parser/test/fixtures/es2015/let/let-as-identifier-7/input.js
vendored
Normal file
2
packages/babel-parser/test/fixtures/es2015/let/let-as-identifier-7/input.js
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
while (0) let
|
||||
instanceof Foo
|
||||
135
packages/babel-parser/test/fixtures/es2015/let/let-as-identifier-7/output.json
vendored
Normal file
135
packages/babel-parser/test/fixtures/es2015/let/let-as-identifier-7/output.json
vendored
Normal file
@ -0,0 +1,135 @@
|
||||
{
|
||||
"type": "File",
|
||||
"start": 0,
|
||||
"end": 28,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 2,
|
||||
"column": 14
|
||||
}
|
||||
},
|
||||
"program": {
|
||||
"type": "Program",
|
||||
"start": 0,
|
||||
"end": 28,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 2,
|
||||
"column": 14
|
||||
}
|
||||
},
|
||||
"sourceType": "script",
|
||||
"interpreter": null,
|
||||
"body": [
|
||||
{
|
||||
"type": "WhileStatement",
|
||||
"start": 0,
|
||||
"end": 28,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 2,
|
||||
"column": 14
|
||||
}
|
||||
},
|
||||
"test": {
|
||||
"type": "NumericLiteral",
|
||||
"start": 7,
|
||||
"end": 8,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 7
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 8
|
||||
}
|
||||
},
|
||||
"extra": {
|
||||
"rawValue": 0,
|
||||
"raw": "0"
|
||||
},
|
||||
"value": 0
|
||||
},
|
||||
"body": {
|
||||
"type": "ExpressionStatement",
|
||||
"start": 10,
|
||||
"end": 28,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 10
|
||||
},
|
||||
"end": {
|
||||
"line": 2,
|
||||
"column": 14
|
||||
}
|
||||
},
|
||||
"expression": {
|
||||
"type": "BinaryExpression",
|
||||
"start": 10,
|
||||
"end": 28,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 10
|
||||
},
|
||||
"end": {
|
||||
"line": 2,
|
||||
"column": 14
|
||||
}
|
||||
},
|
||||
"left": {
|
||||
"type": "Identifier",
|
||||
"start": 10,
|
||||
"end": 13,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 10
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 13
|
||||
},
|
||||
"identifierName": "let"
|
||||
},
|
||||
"name": "let"
|
||||
},
|
||||
"operator": "instanceof",
|
||||
"right": {
|
||||
"type": "Identifier",
|
||||
"start": 25,
|
||||
"end": 28,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 2,
|
||||
"column": 11
|
||||
},
|
||||
"end": {
|
||||
"line": 2,
|
||||
"column": 14
|
||||
},
|
||||
"identifierName": "Foo"
|
||||
},
|
||||
"name": "Foo"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"directives": []
|
||||
}
|
||||
}
|
||||
2
packages/babel-parser/test/fixtures/es2015/let/let-as-identifier-strict-fail/input.js
vendored
Normal file
2
packages/babel-parser/test/fixtures/es2015/let/let-as-identifier-strict-fail/input.js
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
"use strict";
|
||||
let + 1
|
||||
3
packages/babel-parser/test/fixtures/es2015/let/let-as-identifier-strict-fail/options.json
vendored
Normal file
3
packages/babel-parser/test/fixtures/es2015/let/let-as-identifier-strict-fail/options.json
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"throws": "let is a reserved word in strict mode (2:0)"
|
||||
}
|
||||
@ -1,3 +1,3 @@
|
||||
{
|
||||
"throws": "Unexpected token (1:37)"
|
||||
"throws": "let is a reserved word in strict mode (1:37)"
|
||||
}
|
||||
|
||||
@ -1 +0,0 @@
|
||||
var let
|
||||
@ -1,3 +0,0 @@
|
||||
{
|
||||
"throws": "Unexpected token (1:4)"
|
||||
}
|
||||
@ -1 +0,0 @@
|
||||
let
|
||||
@ -1,3 +0,0 @@
|
||||
{
|
||||
"throws": "Unexpected token (1:3)"
|
||||
}
|
||||
@ -1,18 +1,42 @@
|
||||
import { isKeyword } from "../../../src/util/identifier";
|
||||
import {
|
||||
isKeyword,
|
||||
keywordRelationalOperator,
|
||||
} 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("const is a keyword", () => {
|
||||
expect(isKeyword("const")).toBe(true);
|
||||
});
|
||||
it("super is a keyword", () => {
|
||||
expect(isKeyword("super")).toBe(true);
|
||||
});
|
||||
it("let is not a keyword", () => {
|
||||
expect(isKeyword("let")).toBe(false);
|
||||
});
|
||||
it("abc is not a keyword", () => {
|
||||
expect(isKeyword("abc")).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("keywordRelationalOperator", () => {
|
||||
it("in is true", () => {
|
||||
expect(keywordRelationalOperator.test("in")).toBe(true);
|
||||
});
|
||||
it("instanceof is true", () => {
|
||||
expect(keywordRelationalOperator.test("instanceof")).toBe(true);
|
||||
});
|
||||
it("stanceof is false", () => {
|
||||
expect(keywordRelationalOperator.test("stanceof")).toBe(false);
|
||||
});
|
||||
it("instance is false", () => {
|
||||
expect(keywordRelationalOperator.test("instance")).toBe(false);
|
||||
});
|
||||
it("abc is false", () => {
|
||||
expect(keywordRelationalOperator.test("abc")).toBe(false);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@ -672,8 +672,6 @@ language/expressions/async-generator/early-errors-expression-formals-body-duplic
|
||||
language/expressions/async-generator/early-errors-expression-formals-body-duplicate-let.js(default)
|
||||
language/expressions/async-generator/early-errors-expression-formals-body-duplicate-let.js(strict mode)
|
||||
language/expressions/async-generator/named-dflt-params-duplicates.js(default)
|
||||
language/expressions/class/class-name-ident-static-escaped.js(default)
|
||||
language/expressions/class/class-name-ident-static.js(default)
|
||||
language/expressions/class/elements/fields-duplicate-privatenames.js(default)
|
||||
language/expressions/class/elements/fields-duplicate-privatenames.js(strict mode)
|
||||
language/expressions/class/elements/fields-literal-name-static-propname-constructor.js(default)
|
||||
@ -776,8 +774,6 @@ language/expressions/class/elements/syntax/early-errors/super-private-access-inv
|
||||
language/expressions/class/elements/syntax/early-errors/super-private-access-invalid.js(strict mode)
|
||||
language/expressions/function/dflt-params-duplicates.js(default)
|
||||
language/expressions/generators/dflt-params-duplicates.js(default)
|
||||
language/expressions/object/let-non-strict-access.js(default)
|
||||
language/expressions/object/let-non-strict-syntax.js(default)
|
||||
language/expressions/object/method-definition/async-gen-meth-dflt-params-duplicates.js(default)
|
||||
language/expressions/object/method-definition/async-meth-dflt-params-duplicates.js(default)
|
||||
language/expressions/object/method-definition/early-errors-object-method-async-lineterminator.js(default)
|
||||
@ -1029,8 +1025,6 @@ language/statements/class/async-gen-meth-escaped-async.js(default)
|
||||
language/statements/class/async-gen-meth-escaped-async.js(strict mode)
|
||||
language/statements/class/async-meth-escaped-async.js(default)
|
||||
language/statements/class/async-meth-escaped-async.js(strict mode)
|
||||
language/statements/class/class-name-ident-static-escaped.js(default)
|
||||
language/statements/class/class-name-ident-static.js(default)
|
||||
language/statements/class/elements/fields-duplicate-privatenames.js(default)
|
||||
language/statements/class/elements/fields-duplicate-privatenames.js(strict mode)
|
||||
language/statements/class/elements/fields-literal-name-static-propname-constructor.js(default)
|
||||
@ -1144,8 +1138,6 @@ language/statements/class/syntax/early-errors/class-definition-evaluation-block-
|
||||
language/statements/class/syntax/early-errors/class-definition-evaluation-scriptbody-duplicate-binding.js(default)
|
||||
language/statements/class/syntax/early-errors/class-definition-evaluation-scriptbody-duplicate-binding.js(strict mode)
|
||||
language/statements/const/redeclaration-error-from-within-strict-mode-function-const.js(default)
|
||||
language/statements/for-await-of/let-block-with-newline.js(default)
|
||||
language/statements/for-await-of/let-identifier-with-newline.js(default)
|
||||
language/statements/for-in/dstr/array-rest-before-elision.js(default)
|
||||
language/statements/for-in/dstr/array-rest-before-elision.js(strict mode)
|
||||
language/statements/for-in/dstr/array-rest-elision-invalid.js(default)
|
||||
@ -1158,11 +1150,6 @@ language/statements/for-in/head-let-bound-names-dup.js(default)
|
||||
language/statements/for-in/head-let-bound-names-dup.js(strict mode)
|
||||
language/statements/for-in/head-let-bound-names-in-stmt.js(default)
|
||||
language/statements/for-in/head-let-bound-names-in-stmt.js(strict mode)
|
||||
language/statements/for-in/head-lhs-let.js(default)
|
||||
language/statements/for-in/head-var-bound-names-let.js(default)
|
||||
language/statements/for-in/identifier-let-allowed-as-lefthandside-expression-not-strict.js(default)
|
||||
language/statements/for-in/let-block-with-newline.js(default)
|
||||
language/statements/for-in/let-identifier-with-newline.js(default)
|
||||
language/statements/for-of/dstr/array-rest-before-elision.js(default)
|
||||
language/statements/for-of/dstr/array-rest-before-elision.js(strict mode)
|
||||
language/statements/for-of/dstr/array-rest-elision-invalid.js(default)
|
||||
@ -1180,26 +1167,16 @@ language/statements/for-of/head-let-bound-names-dup.js(default)
|
||||
language/statements/for-of/head-let-bound-names-dup.js(strict mode)
|
||||
language/statements/for-of/head-let-bound-names-in-stmt.js(default)
|
||||
language/statements/for-of/head-let-bound-names-in-stmt.js(strict mode)
|
||||
language/statements/for-of/head-var-bound-names-let.js(default)
|
||||
language/statements/for-of/head-var-no-expr.js(default)
|
||||
language/statements/for-of/head-var-no-expr.js(strict mode)
|
||||
language/statements/for-of/let-block-with-newline.js(default)
|
||||
language/statements/for-of/let-identifier-with-newline.js(default)
|
||||
language/statements/for/head-let-bound-names-in-stmt.js(default)
|
||||
language/statements/for/head-let-bound-names-in-stmt.js(strict mode)
|
||||
language/statements/for/head-lhs-let.js(default)
|
||||
language/statements/for/let-block-with-newline.js(default)
|
||||
language/statements/for/let-identifier-with-newline.js(default)
|
||||
language/statements/function/dflt-params-duplicates.js(default)
|
||||
language/statements/generators/dflt-params-duplicates.js(default)
|
||||
language/statements/if/let-block-with-newline.js(default)
|
||||
language/statements/if/let-identifier-with-newline.js(default)
|
||||
language/statements/labeled/let-block-with-newline.js(default)
|
||||
language/statements/labeled/let-identifier-with-newline.js(default)
|
||||
language/statements/labeled/value-yield-non-strict-escaped.js(default)
|
||||
language/statements/labeled/value-yield-non-strict.js(default)
|
||||
language/statements/let/redeclaration-error-from-within-strict-mode-function.js(default)
|
||||
language/statements/let/syntax/escaped-let.js(default)
|
||||
language/statements/switch/syntax/redeclaration/async-function-name-redeclaration-attempt-with-async-function.js(default)
|
||||
language/statements/switch/syntax/redeclaration/async-function-name-redeclaration-attempt-with-async-function.js(strict mode)
|
||||
language/statements/switch/syntax/redeclaration/async-function-name-redeclaration-attempt-with-async-generator.js(default)
|
||||
@ -1331,7 +1308,3 @@ language/statements/try/early-catch-lex.js(default)
|
||||
language/statements/try/early-catch-lex.js(strict mode)
|
||||
language/statements/try/early-catch-var.js(default)
|
||||
language/statements/try/early-catch-var.js(strict mode)
|
||||
language/statements/while/let-block-with-newline.js(default)
|
||||
language/statements/while/let-identifier-with-newline.js(default)
|
||||
language/statements/with/let-block-with-newline.js(default)
|
||||
language/statements/with/let-identifier-with-newline.js(default)
|
||||
Loading…
x
Reference in New Issue
Block a user