Skip to content

Commit bab0228

Browse files
authored
fix: support import attributes (#61)
* fix: support import attributes * remove empty files * remove line * fix failing tests
1 parent 6688727 commit bab0228

File tree

5 files changed

+45
-0
lines changed

5 files changed

+45
-0
lines changed

.changeset/silent-buses-tie.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'esrap': patch
3+
---
4+
5+
fix: support import attributes

src/handlers.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,12 +1165,34 @@ const handlers = {
11651165

11661166
state.commands.push(' from ');
11671167
handle(node.source, state);
1168+
if (node.attributes) {
1169+
state.commands.push(' with { ');
1170+
for (let index = 0; index < node.attributes.length; index++) {
1171+
const { key, value } = node.attributes[index];
1172+
handle(key, state);
1173+
state.commands.push(': ');
1174+
handle(value, state);
1175+
if (index + 1 !== node.attributes.length) {
1176+
state.commands.push(', ');
1177+
}
1178+
}
1179+
state.commands.push(' }');
1180+
}
11681181
state.commands.push(';');
11691182
},
11701183

11711184
ImportExpression(node, state) {
11721185
state.commands.push('import(');
11731186
handle(node.source, state);
1187+
//@ts-expect-error for some reason the types haven't been updated
1188+
if (node.arguments) {
1189+
//@ts-expect-error
1190+
for (let index = 0; index < node.arguments.length; index++) {
1191+
state.commands.push(', ');
1192+
//@ts-expect-error
1193+
handle(node.arguments[index], state);
1194+
}
1195+
}
11741196
state.commands.push(')');
11751197
},
11761198

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { foo } from 'x' with { type: 'json' };
2+
import { bar } from 'y' with { "blah": "true", baz: "false" };
3+
4+
import('baz', { with: { stuff: true } }).then(blah);

test/samples/import-attributes/expected.js.map

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { foo } from 'x' with {type: 'json'};
2+
import { bar } from 'y' with { "blah": "true", baz: "false" };
3+
import('baz', { with: { stuff: true }}).then(blah);

0 commit comments

Comments
 (0)