From 4f69699b710b8842538002241b341748e760bed6 Mon Sep 17 00:00:00 2001 From: Daniel Tschinder Date: Tue, 22 Jan 2019 10:10:28 -0800 Subject: [PATCH] Fix parsing in non-declaration places --- packages/babel-parser/src/parser/statement.js | 10 +- .../es2015/let/let-as-identifier-6/input.js | 2 + .../let/let-as-identifier-6/output.json | 134 +++++++++++++++++ .../es2015/let/let-as-identifier-7/input.js | 2 + .../let/let-as-identifier-7/output.json | 135 ++++++++++++++++++ scripts/tests/test262/test262_whitelist.txt | 9 +- 6 files changed, 282 insertions(+), 10 deletions(-) create mode 100644 packages/babel-parser/test/fixtures/es2015/let/let-as-identifier-6/input.js create mode 100644 packages/babel-parser/test/fixtures/es2015/let/let-as-identifier-6/output.json create mode 100644 packages/babel-parser/test/fixtures/es2015/let/let-as-identifier-7/input.js create mode 100644 packages/babel-parser/test/fixtures/es2015/let/let-as-identifier-7/output.json diff --git a/packages/babel-parser/src/parser/statement.js b/packages/babel-parser/src/parser/statement.js index 7a49ba3fce..1ef98a1f87 100644 --- a/packages/babel-parser/src/parser/statement.js +++ b/packages/babel-parser/src/parser/statement.js @@ -76,7 +76,7 @@ export default class StatementParser extends ExpressionParser { return this.finishNode(node, "InterpreterDirective"); } - isLet() { + isLet(declaration?: boolean): boolean { if (!this.isContextual("let")) { return false; } @@ -93,6 +93,12 @@ export default class StatementParser extends ExpressionParser { 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; @@ -122,7 +128,7 @@ export default class StatementParser extends ExpressionParser { const node = this.startNode(); let kind; - if (this.isLet()) { + if (this.isLet(declaration)) { starttype = tt._var; kind = "let"; } diff --git a/packages/babel-parser/test/fixtures/es2015/let/let-as-identifier-6/input.js b/packages/babel-parser/test/fixtures/es2015/let/let-as-identifier-6/input.js new file mode 100644 index 0000000000..2ebfa83ecf --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2015/let/let-as-identifier-6/input.js @@ -0,0 +1,2 @@ +while (0) let +foo diff --git a/packages/babel-parser/test/fixtures/es2015/let/let-as-identifier-6/output.json b/packages/babel-parser/test/fixtures/es2015/let/let-as-identifier-6/output.json new file mode 100644 index 0000000000..c51c3538f1 --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2015/let/let-as-identifier-6/output.json @@ -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": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/es2015/let/let-as-identifier-7/input.js b/packages/babel-parser/test/fixtures/es2015/let/let-as-identifier-7/input.js new file mode 100644 index 0000000000..7036d1f8e4 --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2015/let/let-as-identifier-7/input.js @@ -0,0 +1,2 @@ +while (0) let +instanceof Foo diff --git a/packages/babel-parser/test/fixtures/es2015/let/let-as-identifier-7/output.json b/packages/babel-parser/test/fixtures/es2015/let/let-as-identifier-7/output.json new file mode 100644 index 0000000000..721e29212e --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2015/let/let-as-identifier-7/output.json @@ -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": [] + } +} \ No newline at end of file diff --git a/scripts/tests/test262/test262_whitelist.txt b/scripts/tests/test262/test262_whitelist.txt index 535b174422..e65ad9097d 100644 --- a/scripts/tests/test262/test262_whitelist.txt +++ b/scripts/tests/test262/test262_whitelist.txt @@ -1138,7 +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-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) @@ -1151,7 +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/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) @@ -1171,13 +1169,10 @@ 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-no-expr.js(default) language/statements/for-of/head-var-no-expr.js(strict mode) -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/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-identifier-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) @@ -1312,6 +1307,4 @@ language/statements/try/early-catch-function.js(strict mode) 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-identifier-with-newline.js(default) -language/statements/with/let-identifier-with-newline.js(default) \ No newline at end of file +language/statements/try/early-catch-var.js(strict mode) \ No newline at end of file