Skip to content

Commit 4e8875e

Browse files
committed
feature: @putout/plugin-esm: apply-name-to-imported-file: dynamic
1 parent df86a07 commit 4e8875e

File tree

11 files changed

+103
-8
lines changed

11 files changed

+103
-8
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
const a = await import('./a.js');

packages/plugin-esm/lib/apply-name-to-imported-file/apply-named-import/index.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,26 @@
1-
import {types} from 'putout';
1+
import {types, operator} from 'putout';
22

3-
const {isImportDefaultSpecifier} = types;
3+
const {getTemplateValues} = operator;
4+
const {
5+
isImportDefaultSpecifier,
6+
isVariableDeclaration,
7+
} = types;
8+
9+
const DYNAMIC = 'const __identifier__a = await import(__b)';
410

511
export const report = (path) => {
12+
if (isVariableDeclaration(path)) {
13+
const {
14+
__identifier__a,
15+
__b,
16+
} = getTemplateValues(path, DYNAMIC);
17+
18+
const {name} = __identifier__a;
19+
const source = __b.value;
20+
21+
return `'const ${name} = await import("${source}")' -> 'const {${name}} = await import("${source}")'`;
22+
}
23+
624
const source = path.node.source.value;
725
const {specifiers} = path.node;
826
const [first] = specifiers;
@@ -24,6 +42,7 @@ export const replace = ({options}) => {
2442
const {name, source} = options;
2543

2644
return {
45+
[`const ${name} = await import("${source}")`]: `const {${name}} = await import("${source}")`,
2746
[`import ${name} from "${source}"`]: `import {${name}} from "${source}"`,
2847
[`export * as ${name} from "${source}"`]: `export {${name}} from "${source}"`,
2948
};

packages/plugin-esm/lib/apply-name-to-imported-file/apply-named-import/index.spec.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,11 @@ test('putout: plugin-esm: apply-name-to-imported-file: apply-named-import: repor
3838
});
3939
t.end();
4040
});
41+
42+
test('putout: plugin-esm: apply-name-to-imported-file: apply-named-import: report with options: dynamic', (t) => {
43+
t.reportWithOptions('dynamic', `'const a = await import("./a.js")' -> 'const {a} = await import("./a.js")'`, {
44+
name: 'a',
45+
source: './a.js',
46+
});
47+
t.end();
48+
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
__putout_processor_filesystem([
2+
"/",
3+
"/lib/",
4+
["/lib/index.js", "Y29uc3Qge2RvdGRvdH0gPSBhd2FpdCBpbXBvcnQoJy4vYi9pbmRleC5qcycpOwo="],
5+
["/lib/a.js", "CiAgICAgICAgaW1wb3J0IGRvdGRvdCBmcm9tICcuL2MuanMnOwogICAg"],
6+
"/lib/b/",
7+
[
8+
"/lib/b/index.js",
9+
"export const dotdot = 7\n"
10+
]
11+
]);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
__putout_processor_filesystem([
2+
'/',
3+
'/lib/',
4+
['/lib/index.js', `
5+
const dotdot = await import('./b/index.js');
6+
`],
7+
['/lib/a.js', `
8+
import dotdot from './c.js';
9+
`],
10+
'/lib/b/',
11+
['/lib/b/index.js', 'export const dotdot = 7\\n'],
12+
]);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
const {read} = await import('./vfs/read/index.js');
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
const read = await import('./vfs/read/index.js');

packages/plugin-esm/lib/apply-name-to-imported-file/get-imports/index.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
1-
import {types} from 'putout';
1+
import {types, operator} from 'putout';
22

3-
const {isImportDefaultSpecifier} = types;
3+
const {getTemplateValues} = operator;
4+
const {
5+
isImportDefaultSpecifier,
6+
isVariableDeclaration,
7+
} = types;
8+
9+
const DYNAMIC = 'const __identifier__a = await import(__b)';
410

511
export const include = () => [
612
'import __a from "__b"',
713
'export * as __a from "__b"',
14+
DYNAMIC,
815
];
916

1017
export const report = (path) => {
@@ -26,6 +33,19 @@ const CommentLine = (value) => ({
2633
});
2734

2835
const getImport = (path) => {
36+
if (isVariableDeclaration(path)) {
37+
const {
38+
__identifier__a,
39+
__b,
40+
} = getTemplateValues(path, DYNAMIC);
41+
42+
return [
43+
__identifier__a.name,
44+
__b.value,
45+
'dynamic',
46+
];
47+
}
48+
2949
const source = path.node.source.value;
3050
const [first] = path.node.specifiers;
3151

packages/plugin-esm/lib/apply-name-to-imported-file/get-imports/index.spec.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,13 @@ test('esm: get-imports: report: export', (t) => {
2121
t.report('export', 'read <- ./vfs/read/index.js <- export');
2222
t.end();
2323
});
24+
25+
test('esm: get-imports: report: dynamic', (t) => {
26+
t.report('dynamic', 'read <- ./vfs/read/index.js <- dynamic');
27+
t.end();
28+
});
29+
30+
test('esm: get-imports: no report: dynamic-destructure', (t) => {
31+
t.noReport('dynamic-destructure');
32+
t.end();
33+
});

packages/plugin-esm/lib/apply-name-to-imported-file/index.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@ const {
1717

1818
export const report = (file, {name, source, type}) => {
1919
const filename = getFilename(file);
20+
const reports = {
21+
import: `Use \`import {${name}} from '${source}'\` in '${filename}'`,
22+
dynamic: `Use \`const {${name}} = await import('${source}')\` in '${filename}'`,
23+
export: `Use \`export {${name}} from '${source}'\` in '${filename}'`,
24+
};
2025

21-
if (type === 'import')
22-
return `Use \`import {${name}} from '${source}'\` in '${filename}'`;
23-
24-
return `Use \`export {${name}} from '${source}'\` in '${filename}'`;
26+
return reports[type];
2527
};
2628

2729
export const fix = (file, {name, source, content, ast}) => {

0 commit comments

Comments
 (0)