Skip to content

Commit

Permalink
feature: convert-import-to-call: add loc
Browse files Browse the repository at this point in the history
  • Loading branch information
coderaiser committed Jun 20, 2023
1 parent 4761331 commit 04f7587
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
16 changes: 14 additions & 2 deletions lib/convert-import-to-call.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,24 @@
const {CallExpression, Import} = require('@babel/types');
const setLiteral = require('./set-literal');

const IMPORT_LENGTH = 'import'.length;

module.exports = (path) => {
const {source} = path.node;
const {loc, source} = path.node;
const calleeNode = Import();

calleeNode.loc = {
start: loc.start,
end: {
line: loc.start.line,
column: loc.start.column + IMPORT_LENGTH,
},
};

setLiteral(source);

const callNode = CallExpression(Import(), [source]);
const callNode = CallExpression(calleeNode, [source]);
callNode.loc = loc;

path.replaceWith(callNode);
};
22 changes: 21 additions & 1 deletion test/fixture/import-expression.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,17 @@
"expression": {
"type": "CallExpression",
"callee": {
"type": "Import"
"type": "Import",
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 6
}
}
},
"arguments": [
{
Expand All @@ -56,6 +66,16 @@
}
}
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 11
}
},
"trailingComments": [],
"leadingComments": [],
"innerComments": []
Expand Down

0 comments on commit 04f7587

Please sign in to comment.