From c63c1bc72824ab5724d6db479e4e8585be1bbb5f Mon Sep 17 00:00:00 2001 From: Jordan Gensler Date: Fri, 14 Oct 2016 14:54:21 -0400 Subject: [PATCH] [dynamic-import] Implementing import() syntax (#163) --- README.md | 1 + ast/spec.md | 13 +- src/parser/expression.js | 13 + src/parser/statement.js | 2 + .../direct-calls-only/actual.js | 3 + .../direct-calls-only/options.json | 3 + .../dynamic-import/inside-function/actual.js | 3 + .../inside-function/expected.json | 229 +++++++++ .../dynamic-import/multiple-args/actual.js | 1 + .../dynamic-import/multiple-args/options.json | 3 + .../dynamic-import/no-args/actual.js | 1 + .../dynamic-import/no-args/options.json | 3 + .../dynamic-import/no-plugin/actual.js | 1 + .../dynamic-import/no-plugin/expected.json | 101 ++++ .../dynamic-import/no-plugin/options.json | 3 + .../experimental/dynamic-import/options.json | 3 + .../dynamic-import/parses-module/actual.js | 1 + .../parses-module/expected.json | 101 ++++ .../dynamic-import/parses-module/options.json | 3 + .../dynamic-import/parses-strict/actual.js | 3 + .../parses-strict/expected.json | 137 ++++++ .../dynamic-import/return-value/actual.js | 1 + .../dynamic-import/return-value/expected.json | 136 ++++++ .../dynamic-import/top-level/actual.js | 6 + .../dynamic-import/top-level/expected.json | 448 ++++++++++++++++++ .../variable-arguments/actual.js | 2 + .../variable-arguments/expected.json | 255 ++++++++++ 27 files changed, 1475 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/experimental/dynamic-import/direct-calls-only/actual.js create mode 100644 test/fixtures/experimental/dynamic-import/direct-calls-only/options.json create mode 100644 test/fixtures/experimental/dynamic-import/inside-function/actual.js create mode 100644 test/fixtures/experimental/dynamic-import/inside-function/expected.json create mode 100644 test/fixtures/experimental/dynamic-import/multiple-args/actual.js create mode 100644 test/fixtures/experimental/dynamic-import/multiple-args/options.json create mode 100644 test/fixtures/experimental/dynamic-import/no-args/actual.js create mode 100644 test/fixtures/experimental/dynamic-import/no-args/options.json create mode 100644 test/fixtures/experimental/dynamic-import/no-plugin/actual.js create mode 100644 test/fixtures/experimental/dynamic-import/no-plugin/expected.json create mode 100644 test/fixtures/experimental/dynamic-import/no-plugin/options.json create mode 100644 test/fixtures/experimental/dynamic-import/options.json create mode 100644 test/fixtures/experimental/dynamic-import/parses-module/actual.js create mode 100644 test/fixtures/experimental/dynamic-import/parses-module/expected.json create mode 100644 test/fixtures/experimental/dynamic-import/parses-module/options.json create mode 100644 test/fixtures/experimental/dynamic-import/parses-strict/actual.js create mode 100644 test/fixtures/experimental/dynamic-import/parses-strict/expected.json create mode 100644 test/fixtures/experimental/dynamic-import/return-value/actual.js create mode 100644 test/fixtures/experimental/dynamic-import/return-value/expected.json create mode 100644 test/fixtures/experimental/dynamic-import/top-level/actual.js create mode 100644 test/fixtures/experimental/dynamic-import/top-level/expected.json create mode 100644 test/fixtures/experimental/dynamic-import/variable-arguments/actual.js create mode 100644 test/fixtures/experimental/dynamic-import/variable-arguments/expected.json diff --git a/README.md b/README.md index 0985574d36..3c6d730f04 100644 --- a/README.md +++ b/README.md @@ -117,3 +117,4 @@ require("babylon").parse("code", { - `asyncGenerators` - `functionBind` - `functionSent` + - `dynamicImport` diff --git a/ast/spec.md b/ast/spec.md index 4435b41d4b..9d187eb4a7 100644 --- a/ast/spec.md +++ b/ast/spec.md @@ -45,6 +45,7 @@ These are the core Babylon AST node types. - [DirectiveLiteral](#directiveliteral) - [Expressions](#expressions) - [Super](#super) + - [Import](#import) - [ThisExpression](#thisexpression) - [ArrowFunctionExpression](#arrowfunctionexpression) - [YieldExpression](#yieldexpression) @@ -568,6 +569,16 @@ interface Super <: Node { A `super` pseudo-expression. +## Import + +```js +interface Import <: Node { + type: "Import"; +} +``` + +A `import` pseudo-expression. + ## ThisExpression ```js @@ -870,7 +881,7 @@ A conditional expression, i.e., a ternary `?`/`:` expression. ```js interface CallExpression <: Expression { type: "CallExpression"; - callee: Expression | Super; + callee: Expression | Super | Import; arguments: [ Expression | SpreadElement ]; } ``` diff --git a/src/parser/expression.js b/src/parser/expression.js index 62b9143194..eacb276665 100644 --- a/src/parser/expression.js +++ b/src/parser/expression.js @@ -302,6 +302,9 @@ pp.parseSubscripts = function (base, startPos, startLoc, noCalls) { let node = this.startNodeAt(startPos, startLoc); node.callee = base; node.arguments = this.parseCallExpressionArguments(tt.parenR, possibleAsync); + if (node.callee.type === "Import" && node.arguments.length !== 1) { + this.raise(node.start, "import() requires exactly one argument"); + } base = this.finishNode(node, "CallExpression"); if (possibleAsync && this.shouldParseAsyncArrow()) { @@ -387,6 +390,16 @@ pp.parseExprAtom = function (refShorthandDefaultPos) { } return this.finishNode(node, "Super"); + case tt._import: + if (!this.hasPlugin("dynamicImport")) this.unexpected(); + + node = this.startNode(); + this.next(); + if (!this.match(tt.parenL)) { + this.unexpected(); + } + return this.finishNode(node, "Import"); + case tt._this: node = this.startNode(); this.next(); diff --git a/src/parser/statement.js b/src/parser/statement.js index 16b4dfab0c..0e89b5b557 100644 --- a/src/parser/statement.js +++ b/src/parser/statement.js @@ -98,6 +98,8 @@ pp.parseStatement = function (declaration, topLevel) { case tt.semi: return this.parseEmptyStatement(node); case tt._export: case tt._import: + if (this.hasPlugin("dynamicImport") && this.lookahead().type === tt.parenL) break; + if (!this.options.allowImportExportEverywhere) { if (!topLevel) { this.raise(this.state.start, "'import' and 'export' may only appear at the top level"); diff --git a/test/fixtures/experimental/dynamic-import/direct-calls-only/actual.js b/test/fixtures/experimental/dynamic-import/direct-calls-only/actual.js new file mode 100644 index 0000000000..7be98d3feb --- /dev/null +++ b/test/fixtures/experimental/dynamic-import/direct-calls-only/actual.js @@ -0,0 +1,3 @@ +function failsParse() { + return import.then(); +} diff --git a/test/fixtures/experimental/dynamic-import/direct-calls-only/options.json b/test/fixtures/experimental/dynamic-import/direct-calls-only/options.json new file mode 100644 index 0000000000..7592dd7f0e --- /dev/null +++ b/test/fixtures/experimental/dynamic-import/direct-calls-only/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Unexpected token (2:15)" +} diff --git a/test/fixtures/experimental/dynamic-import/inside-function/actual.js b/test/fixtures/experimental/dynamic-import/inside-function/actual.js new file mode 100644 index 0000000000..0dc972c972 --- /dev/null +++ b/test/fixtures/experimental/dynamic-import/inside-function/actual.js @@ -0,0 +1,3 @@ +function loadImport(file) { + return import(`test/${file}.js`); +} diff --git a/test/fixtures/experimental/dynamic-import/inside-function/expected.json b/test/fixtures/experimental/dynamic-import/inside-function/expected.json new file mode 100644 index 0000000000..e677f47a4e --- /dev/null +++ b/test/fixtures/experimental/dynamic-import/inside-function/expected.json @@ -0,0 +1,229 @@ +{ + "type": "File", + "start": 0, + "end": 65, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 65, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "sourceType": "script", + "body": [ + { + "type": "FunctionDeclaration", + "start": 0, + "end": 65, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 9, + "end": 19, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 19 + }, + "identifierName": "loadImport" + }, + "name": "loadImport" + }, + "generator": false, + "expression": false, + "async": false, + "params": [ + { + "type": "Identifier", + "start": 20, + "end": 24, + "loc": { + "start": { + "line": 1, + "column": 20 + }, + "end": { + "line": 1, + "column": 24 + }, + "identifierName": "file" + }, + "name": "file" + } + ], + "body": { + "type": "BlockStatement", + "start": 26, + "end": 65, + "loc": { + "start": { + "line": 1, + "column": 26 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "body": [ + { + "type": "ReturnStatement", + "start": 30, + "end": 63, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 35 + } + }, + "argument": { + "type": "CallExpression", + "start": 37, + "end": 62, + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 34 + } + }, + "callee": { + "type": "Import", + "start": 37, + "end": 43, + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 15 + } + } + }, + "arguments": [ + { + "type": "TemplateLiteral", + "start": 44, + "end": 61, + "loc": { + "start": { + "line": 2, + "column": 16 + }, + "end": { + "line": 2, + "column": 33 + } + }, + "expressions": [ + { + "type": "Identifier", + "start": 52, + "end": 56, + "loc": { + "start": { + "line": 2, + "column": 24 + }, + "end": { + "line": 2, + "column": 28 + }, + "identifierName": "file" + }, + "name": "file" + } + ], + "quasis": [ + { + "type": "TemplateElement", + "start": 45, + "end": 50, + "loc": { + "start": { + "line": 2, + "column": 17 + }, + "end": { + "line": 2, + "column": 22 + } + }, + "value": { + "raw": "test/", + "cooked": "test/" + }, + "tail": false + }, + { + "type": "TemplateElement", + "start": 57, + "end": 60, + "loc": { + "start": { + "line": 2, + "column": 29 + }, + "end": { + "line": 2, + "column": 32 + } + }, + "value": { + "raw": ".js", + "cooked": ".js" + }, + "tail": true + } + ] + } + ] + } + } + ], + "directives": [] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/experimental/dynamic-import/multiple-args/actual.js b/test/fixtures/experimental/dynamic-import/multiple-args/actual.js new file mode 100644 index 0000000000..0f1447dbe6 --- /dev/null +++ b/test/fixtures/experimental/dynamic-import/multiple-args/actual.js @@ -0,0 +1 @@ +import('hello', 'world'); diff --git a/test/fixtures/experimental/dynamic-import/multiple-args/options.json b/test/fixtures/experimental/dynamic-import/multiple-args/options.json new file mode 100644 index 0000000000..95a99c52ae --- /dev/null +++ b/test/fixtures/experimental/dynamic-import/multiple-args/options.json @@ -0,0 +1,3 @@ +{ + "throws": "import() requires exactly one argument (1:0)" +} diff --git a/test/fixtures/experimental/dynamic-import/no-args/actual.js b/test/fixtures/experimental/dynamic-import/no-args/actual.js new file mode 100644 index 0000000000..6f375fea96 --- /dev/null +++ b/test/fixtures/experimental/dynamic-import/no-args/actual.js @@ -0,0 +1 @@ +import(); diff --git a/test/fixtures/experimental/dynamic-import/no-args/options.json b/test/fixtures/experimental/dynamic-import/no-args/options.json new file mode 100644 index 0000000000..95a99c52ae --- /dev/null +++ b/test/fixtures/experimental/dynamic-import/no-args/options.json @@ -0,0 +1,3 @@ +{ + "throws": "import() requires exactly one argument (1:0)" +} diff --git a/test/fixtures/experimental/dynamic-import/no-plugin/actual.js b/test/fixtures/experimental/dynamic-import/no-plugin/actual.js new file mode 100644 index 0000000000..b49ee994b1 --- /dev/null +++ b/test/fixtures/experimental/dynamic-import/no-plugin/actual.js @@ -0,0 +1 @@ +import('test.js'); diff --git a/test/fixtures/experimental/dynamic-import/no-plugin/expected.json b/test/fixtures/experimental/dynamic-import/no-plugin/expected.json new file mode 100644 index 0000000000..92fac694a0 --- /dev/null +++ b/test/fixtures/experimental/dynamic-import/no-plugin/expected.json @@ -0,0 +1,101 @@ +{ + "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", + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 18, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 18 + } + }, + "expression": { + "type": "CallExpression", + "start": 0, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 17 + } + }, + "callee": { + "type": "Import", + "start": 0, + "end": 6, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 6 + } + } + }, + "arguments": [ + { + "type": "StringLiteral", + "start": 7, + "end": 16, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 16 + } + }, + "extra": { + "rawValue": "test.js", + "raw": "'test.js'" + }, + "value": "test.js" + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/experimental/dynamic-import/no-plugin/options.json b/test/fixtures/experimental/dynamic-import/no-plugin/options.json new file mode 100644 index 0000000000..a5b818f530 --- /dev/null +++ b/test/fixtures/experimental/dynamic-import/no-plugin/options.json @@ -0,0 +1,3 @@ +{ + "plugins": [] +} diff --git a/test/fixtures/experimental/dynamic-import/options.json b/test/fixtures/experimental/dynamic-import/options.json new file mode 100644 index 0000000000..1629df1f36 --- /dev/null +++ b/test/fixtures/experimental/dynamic-import/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["dynamicImport"] +} diff --git a/test/fixtures/experimental/dynamic-import/parses-module/actual.js b/test/fixtures/experimental/dynamic-import/parses-module/actual.js new file mode 100644 index 0000000000..b49ee994b1 --- /dev/null +++ b/test/fixtures/experimental/dynamic-import/parses-module/actual.js @@ -0,0 +1 @@ +import('test.js'); diff --git a/test/fixtures/experimental/dynamic-import/parses-module/expected.json b/test/fixtures/experimental/dynamic-import/parses-module/expected.json new file mode 100644 index 0000000000..921e11a045 --- /dev/null +++ b/test/fixtures/experimental/dynamic-import/parses-module/expected.json @@ -0,0 +1,101 @@ +{ + "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": "module", + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 18, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 18 + } + }, + "expression": { + "type": "CallExpression", + "start": 0, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 17 + } + }, + "callee": { + "type": "Import", + "start": 0, + "end": 6, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 6 + } + } + }, + "arguments": [ + { + "type": "StringLiteral", + "start": 7, + "end": 16, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 16 + } + }, + "extra": { + "rawValue": "test.js", + "raw": "'test.js'" + }, + "value": "test.js" + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/experimental/dynamic-import/parses-module/options.json b/test/fixtures/experimental/dynamic-import/parses-module/options.json new file mode 100644 index 0000000000..2104ca4328 --- /dev/null +++ b/test/fixtures/experimental/dynamic-import/parses-module/options.json @@ -0,0 +1,3 @@ +{ + "sourceType": "module" +} diff --git a/test/fixtures/experimental/dynamic-import/parses-strict/actual.js b/test/fixtures/experimental/dynamic-import/parses-strict/actual.js new file mode 100644 index 0000000000..b77708a14f --- /dev/null +++ b/test/fixtures/experimental/dynamic-import/parses-strict/actual.js @@ -0,0 +1,3 @@ +"use strict"; + +import('test.js'); diff --git a/test/fixtures/experimental/dynamic-import/parses-strict/expected.json b/test/fixtures/experimental/dynamic-import/parses-strict/expected.json new file mode 100644 index 0000000000..87a3d532ba --- /dev/null +++ b/test/fixtures/experimental/dynamic-import/parses-strict/expected.json @@ -0,0 +1,137 @@ +{ + "type": "File", + "start": 0, + "end": 33, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 18 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 33, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 18 + } + }, + "sourceType": "script", + "body": [ + { + "type": "ExpressionStatement", + "start": 15, + "end": 33, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 18 + } + }, + "expression": { + "type": "CallExpression", + "start": 15, + "end": 32, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 17 + } + }, + "callee": { + "type": "Import", + "start": 15, + "end": 21, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 6 + } + } + }, + "arguments": [ + { + "type": "StringLiteral", + "start": 22, + "end": 31, + "loc": { + "start": { + "line": 3, + "column": 7 + }, + "end": { + "line": 3, + "column": 16 + } + }, + "extra": { + "rawValue": "test.js", + "raw": "'test.js'" + }, + "value": "test.js" + } + ] + } + } + ], + "directives": [ + { + "type": "Directive", + "start": 0, + "end": 13, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 13 + } + }, + "value": { + "type": "DirectiveLiteral", + "start": 0, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 12 + } + }, + "value": "use strict", + "extra": { + "raw": "\"use strict\"", + "rawValue": "use strict" + } + } + } + ] + } +} \ No newline at end of file diff --git a/test/fixtures/experimental/dynamic-import/return-value/actual.js b/test/fixtures/experimental/dynamic-import/return-value/actual.js new file mode 100644 index 0000000000..603eb2f77b --- /dev/null +++ b/test/fixtures/experimental/dynamic-import/return-value/actual.js @@ -0,0 +1 @@ +const importResult = import('test.js'); diff --git a/test/fixtures/experimental/dynamic-import/return-value/expected.json b/test/fixtures/experimental/dynamic-import/return-value/expected.json new file mode 100644 index 0000000000..42597e55b9 --- /dev/null +++ b/test/fixtures/experimental/dynamic-import/return-value/expected.json @@ -0,0 +1,136 @@ +{ + "type": "File", + "start": 0, + "end": 39, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 39 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 39, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 39 + } + }, + "sourceType": "script", + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 39, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 39 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 6, + "end": 38, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 38 + } + }, + "id": { + "type": "Identifier", + "start": 6, + "end": 18, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 18 + }, + "identifierName": "importResult" + }, + "name": "importResult" + }, + "init": { + "type": "CallExpression", + "start": 21, + "end": 38, + "loc": { + "start": { + "line": 1, + "column": 21 + }, + "end": { + "line": 1, + "column": 38 + } + }, + "callee": { + "type": "Import", + "start": 21, + "end": 27, + "loc": { + "start": { + "line": 1, + "column": 21 + }, + "end": { + "line": 1, + "column": 27 + } + } + }, + "arguments": [ + { + "type": "StringLiteral", + "start": 28, + "end": 37, + "loc": { + "start": { + "line": 1, + "column": 28 + }, + "end": { + "line": 1, + "column": 37 + } + }, + "extra": { + "rawValue": "test.js", + "raw": "'test.js'" + }, + "value": "test.js" + } + ] + } + } + ], + "kind": "const" + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/experimental/dynamic-import/top-level/actual.js b/test/fixtures/experimental/dynamic-import/top-level/actual.js new file mode 100644 index 0000000000..7517decff1 --- /dev/null +++ b/test/fixtures/experimental/dynamic-import/top-level/actual.js @@ -0,0 +1,6 @@ +import('testing.js'); + +const test = 'hello'; +import(`testing/${test}.js`); + +import('testing.js').then(() => {}); diff --git a/test/fixtures/experimental/dynamic-import/top-level/expected.json b/test/fixtures/experimental/dynamic-import/top-level/expected.json new file mode 100644 index 0000000000..3fe68aabcc --- /dev/null +++ b/test/fixtures/experimental/dynamic-import/top-level/expected.json @@ -0,0 +1,448 @@ +{ + "type": "File", + "start": 0, + "end": 112, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 6, + "column": 36 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 112, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 6, + "column": 36 + } + }, + "sourceType": "script", + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 21 + } + }, + "expression": { + "type": "CallExpression", + "start": 0, + "end": 20, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 20 + } + }, + "callee": { + "type": "Import", + "start": 0, + "end": 6, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 6 + } + } + }, + "arguments": [ + { + "type": "StringLiteral", + "start": 7, + "end": 19, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 19 + } + }, + "extra": { + "rawValue": "testing.js", + "raw": "'testing.js'" + }, + "value": "testing.js" + } + ] + } + }, + { + "type": "VariableDeclaration", + "start": 23, + "end": 44, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 21 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 29, + "end": 43, + "loc": { + "start": { + "line": 3, + "column": 6 + }, + "end": { + "line": 3, + "column": 20 + } + }, + "id": { + "type": "Identifier", + "start": 29, + "end": 33, + "loc": { + "start": { + "line": 3, + "column": 6 + }, + "end": { + "line": 3, + "column": 10 + }, + "identifierName": "test" + }, + "name": "test" + }, + "init": { + "type": "StringLiteral", + "start": 36, + "end": 43, + "loc": { + "start": { + "line": 3, + "column": 13 + }, + "end": { + "line": 3, + "column": 20 + } + }, + "extra": { + "rawValue": "hello", + "raw": "'hello'" + }, + "value": "hello" + } + } + ], + "kind": "const" + }, + { + "type": "ExpressionStatement", + "start": 45, + "end": 74, + "loc": { + "start": { + "line": 4, + "column": 0 + }, + "end": { + "line": 4, + "column": 29 + } + }, + "expression": { + "type": "CallExpression", + "start": 45, + "end": 73, + "loc": { + "start": { + "line": 4, + "column": 0 + }, + "end": { + "line": 4, + "column": 28 + } + }, + "callee": { + "type": "Import", + "start": 45, + "end": 51, + "loc": { + "start": { + "line": 4, + "column": 0 + }, + "end": { + "line": 4, + "column": 6 + } + } + }, + "arguments": [ + { + "type": "TemplateLiteral", + "start": 52, + "end": 72, + "loc": { + "start": { + "line": 4, + "column": 7 + }, + "end": { + "line": 4, + "column": 27 + } + }, + "expressions": [ + { + "type": "Identifier", + "start": 63, + "end": 67, + "loc": { + "start": { + "line": 4, + "column": 18 + }, + "end": { + "line": 4, + "column": 22 + }, + "identifierName": "test" + }, + "name": "test" + } + ], + "quasis": [ + { + "type": "TemplateElement", + "start": 53, + "end": 61, + "loc": { + "start": { + "line": 4, + "column": 8 + }, + "end": { + "line": 4, + "column": 16 + } + }, + "value": { + "raw": "testing/", + "cooked": "testing/" + }, + "tail": false + }, + { + "type": "TemplateElement", + "start": 68, + "end": 71, + "loc": { + "start": { + "line": 4, + "column": 23 + }, + "end": { + "line": 4, + "column": 26 + } + }, + "value": { + "raw": ".js", + "cooked": ".js" + }, + "tail": true + } + ] + } + ] + } + }, + { + "type": "ExpressionStatement", + "start": 76, + "end": 112, + "loc": { + "start": { + "line": 6, + "column": 0 + }, + "end": { + "line": 6, + "column": 36 + } + }, + "expression": { + "type": "CallExpression", + "start": 76, + "end": 111, + "loc": { + "start": { + "line": 6, + "column": 0 + }, + "end": { + "line": 6, + "column": 35 + } + }, + "callee": { + "type": "MemberExpression", + "start": 76, + "end": 101, + "loc": { + "start": { + "line": 6, + "column": 0 + }, + "end": { + "line": 6, + "column": 25 + } + }, + "object": { + "type": "CallExpression", + "start": 76, + "end": 96, + "loc": { + "start": { + "line": 6, + "column": 0 + }, + "end": { + "line": 6, + "column": 20 + } + }, + "callee": { + "type": "Import", + "start": 76, + "end": 82, + "loc": { + "start": { + "line": 6, + "column": 0 + }, + "end": { + "line": 6, + "column": 6 + } + } + }, + "arguments": [ + { + "type": "StringLiteral", + "start": 83, + "end": 95, + "loc": { + "start": { + "line": 6, + "column": 7 + }, + "end": { + "line": 6, + "column": 19 + } + }, + "extra": { + "rawValue": "testing.js", + "raw": "'testing.js'" + }, + "value": "testing.js" + } + ] + }, + "property": { + "type": "Identifier", + "start": 97, + "end": 101, + "loc": { + "start": { + "line": 6, + "column": 21 + }, + "end": { + "line": 6, + "column": 25 + }, + "identifierName": "then" + }, + "name": "then" + }, + "computed": false + }, + "arguments": [ + { + "type": "ArrowFunctionExpression", + "start": 102, + "end": 110, + "loc": { + "start": { + "line": 6, + "column": 26 + }, + "end": { + "line": 6, + "column": 34 + } + }, + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start": 108, + "end": 110, + "loc": { + "start": { + "line": 6, + "column": 32 + }, + "end": { + "line": 6, + "column": 34 + } + }, + "body": [], + "directives": [] + } + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/experimental/dynamic-import/variable-arguments/actual.js b/test/fixtures/experimental/dynamic-import/variable-arguments/actual.js new file mode 100644 index 0000000000..272d2ebb05 --- /dev/null +++ b/test/fixtures/experimental/dynamic-import/variable-arguments/actual.js @@ -0,0 +1,2 @@ +const testVarible = 'test.js'; +import(testVarible).then(() => {}); diff --git a/test/fixtures/experimental/dynamic-import/variable-arguments/expected.json b/test/fixtures/experimental/dynamic-import/variable-arguments/expected.json new file mode 100644 index 0000000000..9b9dc9d289 --- /dev/null +++ b/test/fixtures/experimental/dynamic-import/variable-arguments/expected.json @@ -0,0 +1,255 @@ +{ + "type": "File", + "start": 0, + "end": 66, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 35 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 66, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 35 + } + }, + "sourceType": "script", + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 30, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 30 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 6, + "end": 29, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 29 + } + }, + "id": { + "type": "Identifier", + "start": 6, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 17 + }, + "identifierName": "testVarible" + }, + "name": "testVarible" + }, + "init": { + "type": "StringLiteral", + "start": 20, + "end": 29, + "loc": { + "start": { + "line": 1, + "column": 20 + }, + "end": { + "line": 1, + "column": 29 + } + }, + "extra": { + "rawValue": "test.js", + "raw": "'test.js'" + }, + "value": "test.js" + } + } + ], + "kind": "const" + }, + { + "type": "ExpressionStatement", + "start": 31, + "end": 66, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 35 + } + }, + "expression": { + "type": "CallExpression", + "start": 31, + "end": 65, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 34 + } + }, + "callee": { + "type": "MemberExpression", + "start": 31, + "end": 55, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 24 + } + }, + "object": { + "type": "CallExpression", + "start": 31, + "end": 50, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 19 + } + }, + "callee": { + "type": "Import", + "start": 31, + "end": 37, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 6 + } + } + }, + "arguments": [ + { + "type": "Identifier", + "start": 38, + "end": 49, + "loc": { + "start": { + "line": 2, + "column": 7 + }, + "end": { + "line": 2, + "column": 18 + }, + "identifierName": "testVarible" + }, + "name": "testVarible" + } + ] + }, + "property": { + "type": "Identifier", + "start": 51, + "end": 55, + "loc": { + "start": { + "line": 2, + "column": 20 + }, + "end": { + "line": 2, + "column": 24 + }, + "identifierName": "then" + }, + "name": "then" + }, + "computed": false + }, + "arguments": [ + { + "type": "ArrowFunctionExpression", + "start": 56, + "end": 64, + "loc": { + "start": { + "line": 2, + "column": 25 + }, + "end": { + "line": 2, + "column": 33 + } + }, + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start": 62, + "end": 64, + "loc": { + "start": { + "line": 2, + "column": 31 + }, + "end": { + "line": 2, + "column": 33 + } + }, + "body": [], + "directives": [] + } + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file