[dynamic-import] Implementing import() syntax (#163)

This commit is contained in:
Jordan Gensler 2016-10-14 14:54:21 -04:00 committed by Henry Zhu
parent 4c445fd5f8
commit c63c1bc728
27 changed files with 1475 additions and 1 deletions

View File

@ -117,3 +117,4 @@ require("babylon").parse("code", {
- `asyncGenerators`
- `functionBind`
- `functionSent`
- `dynamicImport`

View File

@ -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 ];
}
```

View File

@ -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();

View File

@ -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");

View File

@ -0,0 +1,3 @@
function failsParse() {
return import.then();
}

View File

@ -0,0 +1,3 @@
{
"throws": "Unexpected token (2:15)"
}

View File

@ -0,0 +1,3 @@
function loadImport(file) {
return import(`test/${file}.js`);
}

View File

@ -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": []
}
}

View File

@ -0,0 +1 @@
import('hello', 'world');

View File

@ -0,0 +1,3 @@
{
"throws": "import() requires exactly one argument (1:0)"
}

View File

@ -0,0 +1 @@
import();

View File

@ -0,0 +1,3 @@
{
"throws": "import() requires exactly one argument (1:0)"
}

View File

@ -0,0 +1 @@
import('test.js');

View File

@ -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": []
}
}

View File

@ -0,0 +1,3 @@
{
"plugins": []
}

View File

@ -0,0 +1,3 @@
{
"plugins": ["dynamicImport"]
}

View File

@ -0,0 +1 @@
import('test.js');

View File

@ -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": []
}
}

View File

@ -0,0 +1,3 @@
{
"sourceType": "module"
}

View File

@ -0,0 +1,3 @@
"use strict";
import('test.js');

View File

@ -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"
}
}
}
]
}
}

View File

@ -0,0 +1 @@
const importResult = import('test.js');

View File

@ -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": []
}
}

View File

@ -0,0 +1,6 @@
import('testing.js');
const test = 'hello';
import(`testing/${test}.js`);
import('testing.js').then(() => {});

View File

@ -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": []
}
}

View File

@ -0,0 +1,2 @@
const testVarible = 'test.js';
import(testVarible).then(() => {});

View File

@ -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": []
}
}