Skip to content

Commit

Permalink
feature(estree-to-babel) add support of ImportExpression
Browse files Browse the repository at this point in the history
  • Loading branch information
coderaiser committed Jul 16, 2021
1 parent 35d69fa commit 8cd0249
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ The thing is `@babel/parser` has a [little differences](https://babeljs.io/docs/
- `ClassPrivateMethod`;
- `ClassPrivateName` stores name as `Identifier` in `id` field;
- `ClassPrivateProperty` instead of `FieldDefinition`;
- `CallExpression` instead of `ImportExpression`;
- etc...

`estree-to-babel` aims to smooth this differences.
Expand Down
21 changes: 21 additions & 0 deletions lib/convert-import-to-call.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';

const {
CallExpression,
Identifier,
} = require('@babel/types');

const setLiteral = require('./set-literal');

module.exports = (path) => {
const {source} = path.node;

setLiteral(source);

const callNode = CallExpression(Identifier('import'), [
source,
]);

path.replaceWith(callNode);
};

9 changes: 6 additions & 3 deletions lib/estree-to-babel.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const traverseObjectExpression = require('./traverse-object-expression');
const setClassMethod = require('./set-class-method');
const setClassPrivateProperty = require('./set-class-private-property');
const setClassPrivateName = require('./set-class-private-name');
const convertImportToCall = require('./convert-import-to-call');

const {convertNodeComments} = require('./comments');

Expand All @@ -22,11 +23,10 @@ module.exports = (node) => {
const {node} = path;
const {type} = node;

if (type === 'Literal')
if (/Literal$/.test(type)) {
setLiteral(node);

if (/Literal$/.test(type))
return setEsprimaRaw(node);
}

if (type === 'Property')
return setObjectProperty(node);
Expand All @@ -39,6 +39,9 @@ module.exports = (node) => {

if (type === 'PrivateName')
return setClassPrivateName(path);

if (type === 'ImportExpression')
return convertImportToCall(path);
},
exit(path) {
const {node} = path;
Expand Down
12 changes: 12 additions & 0 deletions test/estree-to-babel.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ const fixture = {
classPrivateProperty: readJSON('class-private-property.json'),
strictMode: readJSON('strict-mode.json'),
classMethodBabel: readJSON('class-method-babel.json'),
importExpression: readJSON('import-expression.json'),
},
js: {
property: readJS('property.js'),
Expand All @@ -93,6 +94,7 @@ const fixture = {
classMethod: readJS('class-method.js'),
classPrivateMethod: readJS('class-private-method.js'),
classPrivateProperty: readJS('class-private-property.js'),
importExpression: readJS('import-expression.js'),
},
};

Expand Down Expand Up @@ -256,3 +258,13 @@ test('estree-to-babel: acorn.parse: private property', (t) => {
t.end();
});

test('estree-to-babel: espree.parse: ImportExpression', (t) => {
const ast = acornParse(fixture.js.importExpression);
const result = estreeToBabel(ast);

update('import-expression', result);

t.jsonEqual(result, fixture.ast.importExpression, 'should equal');
t.end();
});

1 change: 1 addition & 0 deletions test/fixture/import-expression.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import('x');
70 changes: 70 additions & 0 deletions test/fixture/import-expression.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
{
"type": "File",
"program": {
"type": "Program",
"start": 0,
"end": 13,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 2,
"column": 0
}
},
"body": [
{
"type": "ExpressionStatement",
"start": 0,
"end": 12,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 12
}
},
"expression": {
"type": "CallExpression",
"callee": {
"type": "Identifier",
"name": "import"
},
"arguments": [
{
"type": "StringLiteral",
"start": 7,
"end": 10,
"loc": {
"start": {
"line": 1,
"column": 7
},
"end": {
"line": 1,
"column": 10
}
},
"value": "x",
"raw": "'x'",
"extra": {
"raw": "'x'"
}
}
],
"trailingComments": [],
"leadingComments": [],
"innerComments": []
}
}
],
"sourceType": "module",
"directives": []
},
"comments": []
}

0 comments on commit 8cd0249

Please sign in to comment.