From 09bb9bc6be3a4722d9684c12492513ea0320c89c Mon Sep 17 00:00:00 2001 From: Brian Ng Date: Tue, 28 Feb 2017 11:43:34 -0600 Subject: [PATCH] Fix parsing yield with dynamicImport (#383) --- src/parser/expression.js | 8 +- src/tokenizer/types.js | 2 +- .../dynamic-import/generator/actual.js | 3 + .../dynamic-import/generator/expected.json | 171 ++++++++++++++++++ 4 files changed, 181 insertions(+), 3 deletions(-) create mode 100644 test/fixtures/experimental/dynamic-import/generator/actual.js create mode 100644 test/fixtures/experimental/dynamic-import/generator/expected.json diff --git a/src/parser/expression.js b/src/parser/expression.js index bf5dda237d..f336e896b7 100644 --- a/src/parser/expression.js +++ b/src/parser/expression.js @@ -61,7 +61,7 @@ pp.getExpression = function() { // the AST node that the inner parser gave them in another node. // Parse a full expression. The optional arguments are used to -// forbid the `in` operator (in for loops initalization expressions) +// forbid the `in` operator (in for loops initialization expressions) // and provide reference for storing '=' operator inside shorthand // property assignment in contexts where both object expression // and object pattern might appear (so it's possible to raise @@ -1095,7 +1095,11 @@ pp.parseAwait = function (node) { pp.parseYield = function () { const node = this.startNode(); this.next(); - if (this.match(tt.semi) || this.canInsertSemicolon() || (!this.match(tt.star) && !this.state.type.startsExpr)) { + if ( + this.match(tt.semi) || + this.canInsertSemicolon() || + (!this.match(tt.star) && !this.state.type.startsExpr) + ) { node.delegate = false; node.argument = null; } else { diff --git a/src/tokenizer/types.js b/src/tokenizer/types.js index b82dfe0902..1d78aabb40 100644 --- a/src/tokenizer/types.js +++ b/src/tokenizer/types.js @@ -143,7 +143,7 @@ export const keywords = { "class": new KeywordTokenType("class"), "extends": new KeywordTokenType("extends", { beforeExpr }), "export": new KeywordTokenType("export"), - "import": new KeywordTokenType("import"), + "import": new KeywordTokenType("import", { startsExpr }), "yield": new KeywordTokenType("yield", { beforeExpr, startsExpr }), "null": new KeywordTokenType("null", { startsExpr }), "true": new KeywordTokenType("true", { startsExpr }), diff --git a/test/fixtures/experimental/dynamic-import/generator/actual.js b/test/fixtures/experimental/dynamic-import/generator/actual.js new file mode 100644 index 0000000000..5b09c31e6f --- /dev/null +++ b/test/fixtures/experimental/dynamic-import/generator/actual.js @@ -0,0 +1,3 @@ +function* a() { + yield import('http'); +} diff --git a/test/fixtures/experimental/dynamic-import/generator/expected.json b/test/fixtures/experimental/dynamic-import/generator/expected.json new file mode 100644 index 0000000000..e2aff25e73 --- /dev/null +++ b/test/fixtures/experimental/dynamic-import/generator/expected.json @@ -0,0 +1,171 @@ +{ + "type": "File", + "start": 0, + "end": 41, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 41, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "sourceType": "script", + "body": [ + { + "type": "FunctionDeclaration", + "start": 0, + "end": 41, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 10, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 11 + }, + "identifierName": "a" + }, + "name": "a" + }, + "generator": true, + "expression": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start": 14, + "end": 41, + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "body": [ + { + "type": "ExpressionStatement", + "start": 18, + "end": 39, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 23 + } + }, + "expression": { + "type": "YieldExpression", + "start": 18, + "end": 38, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 22 + } + }, + "delegate": false, + "argument": { + "type": "CallExpression", + "start": 24, + "end": 38, + "loc": { + "start": { + "line": 2, + "column": 8 + }, + "end": { + "line": 2, + "column": 22 + } + }, + "callee": { + "type": "Import", + "start": 24, + "end": 30, + "loc": { + "start": { + "line": 2, + "column": 8 + }, + "end": { + "line": 2, + "column": 14 + } + } + }, + "arguments": [ + { + "type": "StringLiteral", + "start": 31, + "end": 37, + "loc": { + "start": { + "line": 2, + "column": 15 + }, + "end": { + "line": 2, + "column": 21 + } + }, + "extra": { + "rawValue": "http", + "raw": "'http'" + }, + "value": "http" + } + ] + } + } + } + ], + "directives": [] + } + } + ], + "directives": [] + } +} \ No newline at end of file