Skip to content

Commit b8df205

Browse files
papandreoumichaelficarra
authored andcommitted
Support dynamic import (take 2) (estools#395)
* feat: support dynamic import * Add a couple of tests * Align with estree/estree#198 * Update escodegen.js Co-Authored-By: Michael Ficarra <github@michael.ficarra.me> * Remove outdated "State 3" from comment * Add test of a SequenceExpression as a dynamic import source estools#395 (comment) * Properly parenthesize when used with a higher precedence operator estools#395 (comment)
1 parent 4a93ffc commit b8df205

File tree

2 files changed

+113
-2
lines changed

2 files changed

+113
-2
lines changed

escodegen.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2434,7 +2434,15 @@
24342434

24352435
ModuleSpecifier: function (expr, precedence, flags) {
24362436
return this.Literal(expr, precedence, flags);
2437-
}
2437+
},
2438+
2439+
ImportExpression: function(expr, precedence, flag) {
2440+
return parenthesize([
2441+
'import(',
2442+
this.generateExpression(expr.source, Precedence.Assignment, E_TTT),
2443+
')'
2444+
], Precedence.Call, precedence);
2445+
},
24382446

24392447
};
24402448

test/harmony.js

Lines changed: 104 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6416,8 +6416,111 @@ data = {
64166416
}
64176417
}
64186418
}
6419-
}
6419+
},
6420+
6421+
// https://github.com/tc39/proposal-dynamic-import/#import
6422+
'dynamic import': {
6423+
"import('foo').then(quux);": {
6424+
generateFrom: {
6425+
"type": "ExpressionStatement",
6426+
"expression": {
6427+
"type": "CallExpression",
6428+
"callee": {
6429+
"type": "MemberExpression",
6430+
"object": {
6431+
"type": "ImportExpression",
6432+
"source": {
6433+
"type": "Literal",
6434+
"value": "foo"
6435+
}
6436+
},
6437+
"property": {
6438+
"type": "Identifier",
6439+
"name": "then"
6440+
},
6441+
"computed": false
6442+
},
6443+
"arguments": [
6444+
{
6445+
"type": "Identifier",
6446+
"name": "quux"
6447+
}
6448+
]
6449+
}
6450+
}
6451+
},
64206452

6453+
"import(('a', 'b'))": {
6454+
generateFrom: {
6455+
"type": "ImportExpression",
6456+
"source": {
6457+
"type": "SequenceExpression",
6458+
"expressions": [
6459+
{
6460+
"type": "Literal",
6461+
"value": "a"
6462+
},
6463+
{
6464+
"type": "Literal",
6465+
"value": "b"
6466+
}
6467+
]
6468+
}
6469+
}
6470+
},
6471+
6472+
"new (import('foo'))()": {
6473+
generateFrom: {
6474+
"type": "NewExpression",
6475+
"callee": {
6476+
"type": "ImportExpression",
6477+
"source": {
6478+
"type": "Literal",
6479+
"value": "foo"
6480+
}
6481+
},
6482+
"arguments": []
6483+
}
6484+
},
6485+
6486+
"import('foo' + bar).then(quux);": {
6487+
generateFrom: {
6488+
"type": "ExpressionStatement",
6489+
"expression": {
6490+
"type": "CallExpression",
6491+
"callee": {
6492+
"type": "MemberExpression",
6493+
"object": {
6494+
"type": "ImportExpression",
6495+
"source": {
6496+
"type": "BinaryExpression",
6497+
"left": {
6498+
"type": "Literal",
6499+
"value": "foo"
6500+
},
6501+
"operator": "+",
6502+
"right": {
6503+
"type": "Identifier",
6504+
"name": "bar"
6505+
}
6506+
}
6507+
},
6508+
"property": {
6509+
"type": "Identifier",
6510+
"name": "then"
6511+
},
6512+
"computed": false
6513+
},
6514+
"arguments": [
6515+
{
6516+
"type": "Identifier",
6517+
"name": "quux"
6518+
}
6519+
]
6520+
}
6521+
}
6522+
}
6523+
}
64216524
};
64226525

64236526
function updateDeeply(target, override) {

0 commit comments

Comments
 (0)