Make let a contextual keyword
This commit is contained in:
parent
65febdd13a
commit
178f2d7949
@ -3,8 +3,13 @@
|
|||||||
import * as N from "../types";
|
import * as N from "../types";
|
||||||
import { types as tt, type TokenType } from "../tokenizer/types";
|
import { types as tt, type TokenType } from "../tokenizer/types";
|
||||||
import ExpressionParser from "./expression";
|
import ExpressionParser from "./expression";
|
||||||
import { isIdentifierChar } from "../util/identifier";
|
import {
|
||||||
|
isIdentifierChar,
|
||||||
|
isIdentifierStart,
|
||||||
|
keywordRelationalOperator,
|
||||||
|
} from "../util/identifier";
|
||||||
import { lineBreak, skipWhiteSpace } from "../util/whitespace";
|
import { lineBreak, skipWhiteSpace } from "../util/whitespace";
|
||||||
|
import * as charCodes from "charcodes";
|
||||||
|
|
||||||
// Reused empty array added for node fields that are always empty.
|
// Reused empty array added for node fields that are always empty.
|
||||||
|
|
||||||
@ -71,6 +76,33 @@ export default class StatementParser extends ExpressionParser {
|
|||||||
return this.finishNode(node, "InterpreterDirective");
|
return this.finishNode(node, "InterpreterDirective");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
isLet() {
|
||||||
|
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)) {
|
||||||
|
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.
|
// Parse a single statement.
|
||||||
//
|
//
|
||||||
// If expecting a statement and finding a slash operator, parse a
|
// If expecting a statement and finding a slash operator, parse a
|
||||||
@ -86,8 +118,14 @@ export default class StatementParser extends ExpressionParser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
parseStatementContent(declaration: boolean, topLevel: ?boolean): N.Statement {
|
parseStatementContent(declaration: boolean, topLevel: ?boolean): N.Statement {
|
||||||
const starttype = this.state.type;
|
let starttype = this.state.type;
|
||||||
const node = this.startNode();
|
const node = this.startNode();
|
||||||
|
let kind;
|
||||||
|
|
||||||
|
if (this.isLet()) {
|
||||||
|
starttype = tt._var;
|
||||||
|
kind = "let";
|
||||||
|
}
|
||||||
|
|
||||||
// Most types of statements are recognized by the keyword they
|
// Most types of statements are recognized by the keyword they
|
||||||
// start with. Many are trivial to parse, some require a bit of
|
// start with. Many are trivial to parse, some require a bit of
|
||||||
@ -129,12 +167,11 @@ export default class StatementParser extends ExpressionParser {
|
|||||||
case tt._try:
|
case tt._try:
|
||||||
return this.parseTryStatement(node);
|
return this.parseTryStatement(node);
|
||||||
|
|
||||||
case tt._let:
|
|
||||||
case tt._const:
|
case tt._const:
|
||||||
if (!declaration) this.unexpected(); // NOTE: falls through to _var
|
|
||||||
|
|
||||||
case tt._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:
|
case tt._while:
|
||||||
return this.parseWhileStatement(node);
|
return this.parseWhileStatement(node);
|
||||||
@ -424,18 +461,19 @@ export default class StatementParser extends ExpressionParser {
|
|||||||
return this.parseFor(node, null);
|
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 init = this.startNode();
|
||||||
const varKind = this.state.type;
|
const kind = isLet ? "let" : this.state.value;
|
||||||
this.next();
|
this.next();
|
||||||
this.parseVar(init, true, varKind);
|
this.parseVar(init, true, kind);
|
||||||
this.finishNode(init, "VariableDeclaration");
|
this.finishNode(init, "VariableDeclaration");
|
||||||
|
|
||||||
if (this.match(tt._in) || this.isContextual("of")) {
|
if (this.match(tt._in) || this.isContextual("of")) {
|
||||||
if (init.declarations.length === 1) {
|
if (init.declarations.length === 1) {
|
||||||
const declaration = init.declarations[0];
|
const declaration = init.declarations[0];
|
||||||
const isForInInitializer =
|
const isForInInitializer =
|
||||||
varKind === tt._var &&
|
kind === "var" &&
|
||||||
declaration.init &&
|
declaration.init &&
|
||||||
declaration.id.type != "ObjectPattern" &&
|
declaration.id.type != "ObjectPattern" &&
|
||||||
declaration.id.type != "ArrayPattern" &&
|
declaration.id.type != "ArrayPattern" &&
|
||||||
@ -606,7 +644,7 @@ export default class StatementParser extends ExpressionParser {
|
|||||||
|
|
||||||
parseVarStatement(
|
parseVarStatement(
|
||||||
node: N.VariableDeclaration,
|
node: N.VariableDeclaration,
|
||||||
kind: TokenType,
|
kind: "var" | "let" | "const",
|
||||||
): N.VariableDeclaration {
|
): N.VariableDeclaration {
|
||||||
this.next();
|
this.next();
|
||||||
this.parseVar(node, false, kind);
|
this.parseVar(node, false, kind);
|
||||||
@ -862,12 +900,11 @@ export default class StatementParser extends ExpressionParser {
|
|||||||
parseVar(
|
parseVar(
|
||||||
node: N.VariableDeclaration,
|
node: N.VariableDeclaration,
|
||||||
isFor: boolean,
|
isFor: boolean,
|
||||||
kind: TokenType,
|
kind: "var" | "let" | "const",
|
||||||
): N.VariableDeclaration {
|
): N.VariableDeclaration {
|
||||||
const declarations = (node.declarations = []);
|
const declarations = (node.declarations = []);
|
||||||
const isTypescript = this.hasPlugin("typescript");
|
const isTypescript = this.hasPlugin("typescript");
|
||||||
// $FlowFixMe
|
node.kind = kind;
|
||||||
node.kind = kind.keyword;
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
const decl = this.startNode();
|
const decl = this.startNode();
|
||||||
this.parseVarHead(decl);
|
this.parseVarHead(decl);
|
||||||
@ -875,7 +912,7 @@ export default class StatementParser extends ExpressionParser {
|
|||||||
decl.init = this.parseMaybeAssign(isFor);
|
decl.init = this.parseMaybeAssign(isFor);
|
||||||
} else {
|
} else {
|
||||||
if (
|
if (
|
||||||
kind === tt._const &&
|
kind === "const" &&
|
||||||
!(this.match(tt._in) || this.isContextual("of"))
|
!(this.match(tt._in) || this.isContextual("of"))
|
||||||
) {
|
) {
|
||||||
// `const` with no initializer is allowed in TypeScript.
|
// `const` with no initializer is allowed in TypeScript.
|
||||||
@ -1638,11 +1675,7 @@ export default class StatementParser extends ExpressionParser {
|
|||||||
}
|
}
|
||||||
this.parseDecorators(false);
|
this.parseDecorators(false);
|
||||||
return this.parseClass(expr, true, true);
|
return this.parseClass(expr, true, true);
|
||||||
} else if (
|
} else if (this.match(tt._const) || this.match(tt._var) || this.isLet()) {
|
||||||
this.match(tt._let) ||
|
|
||||||
this.match(tt._const) ||
|
|
||||||
this.match(tt._var)
|
|
||||||
) {
|
|
||||||
return this.raise(
|
return this.raise(
|
||||||
this.state.start,
|
this.state.start,
|
||||||
"Only expressions, functions or classes are allowed as the `default` export.",
|
"Only expressions, functions or classes are allowed as the `default` export.",
|
||||||
@ -1661,7 +1694,7 @@ export default class StatementParser extends ExpressionParser {
|
|||||||
|
|
||||||
isExportDefaultSpecifier(): boolean {
|
isExportDefaultSpecifier(): boolean {
|
||||||
if (this.match(tt.name)) {
|
if (this.match(tt.name)) {
|
||||||
return this.state.value !== "async";
|
return this.state.value !== "async" && this.state.value !== "let";
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!this.match(tt._default)) {
|
if (!this.match(tt._default)) {
|
||||||
@ -1710,9 +1743,9 @@ export default class StatementParser extends ExpressionParser {
|
|||||||
return (
|
return (
|
||||||
this.state.type.keyword === "var" ||
|
this.state.type.keyword === "var" ||
|
||||||
this.state.type.keyword === "const" ||
|
this.state.type.keyword === "const" ||
|
||||||
this.state.type.keyword === "let" ||
|
|
||||||
this.state.type.keyword === "function" ||
|
this.state.type.keyword === "function" ||
|
||||||
this.state.type.keyword === "class" ||
|
this.state.type.keyword === "class" ||
|
||||||
|
this.isLet() ||
|
||||||
this.isAsyncFunction()
|
this.isAsyncFunction()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -344,7 +344,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
|||||||
} else {
|
} else {
|
||||||
if (
|
if (
|
||||||
this.match(tt._const) ||
|
this.match(tt._const) ||
|
||||||
this.match(tt._let) ||
|
this.isLet() ||
|
||||||
((this.isContextual("type") || this.isContextual("interface")) &&
|
((this.isContextual("type") || this.isContextual("interface")) &&
|
||||||
!insideModule)
|
!insideModule)
|
||||||
) {
|
) {
|
||||||
|
|||||||
@ -1190,8 +1190,15 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
|||||||
if (this.isLineTerminator()) {
|
if (this.isLineTerminator()) {
|
||||||
return;
|
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:
|
case tt._function:
|
||||||
this.next();
|
this.next();
|
||||||
return this.parseFunction(nany, /* isStatement */ true);
|
return this.parseFunction(nany, /* isStatement */ true);
|
||||||
@ -1210,8 +1217,8 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
|||||||
}
|
}
|
||||||
// falls through
|
// falls through
|
||||||
case tt._var:
|
case tt._var:
|
||||||
case tt._let:
|
kind = kind || this.state.value;
|
||||||
return this.parseVarStatement(nany, this.state.type);
|
return this.parseVarStatement(nany, kind);
|
||||||
case tt.name: {
|
case tt.name: {
|
||||||
const value = this.state.value;
|
const value = this.state.value;
|
||||||
if (value === "global") {
|
if (value === "global") {
|
||||||
|
|||||||
@ -1384,8 +1384,8 @@ export default class Tokenizer extends LocationParser {
|
|||||||
|
|
||||||
if (
|
if (
|
||||||
prevType === tt._var ||
|
prevType === tt._var ||
|
||||||
prevType === tt._let ||
|
prevType === tt._const ||
|
||||||
prevType === tt._const
|
prevType === tt.name
|
||||||
) {
|
) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -180,7 +180,6 @@ export const keywords = Object.create(null, {
|
|||||||
throw: makeKeywordProps("throw", { beforeExpr, prefix, startsExpr }),
|
throw: makeKeywordProps("throw", { beforeExpr, prefix, startsExpr }),
|
||||||
try: makeKeywordProps("try"),
|
try: makeKeywordProps("try"),
|
||||||
var: makeKeywordProps("var"),
|
var: makeKeywordProps("var"),
|
||||||
let: makeKeywordProps("let"),
|
|
||||||
const: makeKeywordProps("const"),
|
const: makeKeywordProps("const"),
|
||||||
while: makeKeywordProps("while", { isLoop }),
|
while: makeKeywordProps("while", { isLoop }),
|
||||||
with: makeKeywordProps("with"),
|
with: makeKeywordProps("with"),
|
||||||
|
|||||||
@ -57,7 +57,6 @@ const keywords = new Set([
|
|||||||
"new",
|
"new",
|
||||||
"in",
|
"in",
|
||||||
"this",
|
"this",
|
||||||
"let",
|
|
||||||
"const",
|
"const",
|
||||||
"class",
|
"class",
|
||||||
"extends",
|
"extends",
|
||||||
@ -71,6 +70,8 @@ export function isKeyword(word: string): boolean {
|
|||||||
return keywords.has(word);
|
return keywords.has(word);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const keywordRelationalOperator = /^in(stanceof)?$/;
|
||||||
|
|
||||||
// ## Character categories
|
// ## Character categories
|
||||||
|
|
||||||
// Big ugly regular expressions that matches characters in the
|
// 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-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("identifier", () => {
|
||||||
describe("isKeyword", () => {
|
describe("isKeyword", () => {
|
||||||
it("break is a keyword", () => {
|
it("break is a keyword", () => {
|
||||||
expect(isKeyword("break")).toBe(true);
|
expect(isKeyword("break")).toBe(true);
|
||||||
});
|
});
|
||||||
it("let is a keyword", () => {
|
it("const is a keyword", () => {
|
||||||
expect(isKeyword("let")).toBe(true);
|
expect(isKeyword("const")).toBe(true);
|
||||||
});
|
});
|
||||||
it("super is a keyword", () => {
|
it("super is a keyword", () => {
|
||||||
expect(isKeyword("super")).toBe(true);
|
expect(isKeyword("super")).toBe(true);
|
||||||
});
|
});
|
||||||
|
it("let is not a keyword", () => {
|
||||||
|
expect(isKeyword("let")).toBe(false);
|
||||||
|
});
|
||||||
it("abc is not a keyword", () => {
|
it("abc is not a keyword", () => {
|
||||||
expect(isKeyword("abc")).toBe(false);
|
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);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user