Skip to content

Commit 3aca6de

Browse files
committed
fix: @putout/plugin-esm: sort-imports-by-specifiers: report after transform
1 parent 27b156c commit 3aca6de

File tree

5 files changed

+85
-47
lines changed

5 files changed

+85
-47
lines changed

packages/plugin-esm/lib/sort-imports-by-specifiers/.putoutcache.json

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import {e1} from '../../../../../../hello3';
2+
import {d1} from '../../../../Hello4';
3+
import {c1} from '../../../../Hello5';
4+
import {
5+
a2,
6+
b2,
7+
c2,
8+
d2,
9+
} from '../../../../../../hello';
10+
import {b1} from '../../../../Hello6';
11+
import {a1} from '../../../../abc/hello';
12+
import e from '../../../Hello/Hello';
13+
import d from '../../../Hello/Hello1';
14+
import b from '../../../Hello/Hello5';
15+
import a from '../../../Hello/HelloA';
16+
import f from '../../Hello/Hello1';
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import {
2+
a2,
3+
b2,
4+
c2,
5+
d2,
6+
} from '../../../../../../hello';
7+
8+
import { e1 } from '../../../../../../hello3';
9+
import { d1 } from '../../../../Hello4';
10+
import { c1 } from '../../../../Hello5';
11+
import { b1 } from '../../../../Hello6';
12+
import { a1 } from '../../../../abc/hello';
13+
import e from '../../../Hello/Hello';
14+
import d from '../../../Hello/Hello1';
15+
import b from '../../../Hello/Hello5';
16+
import a from '../../../Hello/HelloA';
17+
import f from '../../Hello/Hello1';

packages/plugin-esm/lib/sort-imports-by-specifiers/index.js

Lines changed: 47 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import {parseImportSpecifiers} from 'parse-import-specifiers';
2-
import {operator} from 'putout';
2+
import {operator, types} from 'putout';
33

44
const {insertBefore, remove} = operator;
5+
const {isImportDeclaration} = types;
56

67
export const report = () => `Sort imports by specifiers count`;
78

@@ -12,44 +13,52 @@ export const fix = ({path, nextPath}) => {
1213
};
1314

1415
export const traverse = ({push}) => ({
15-
ImportDeclaration(path) {
16-
const {node} = path;
17-
const {source, specifiers} = node;
18-
const {imports} = parseImportSpecifiers(specifiers);
16+
Program(mainPath) {
17+
const paths = mainPath.get('body').filter(isImportDeclaration);
1918

20-
if (imports.length < 4)
21-
return;
22-
23-
const nextPath = path.getNextSibling();
24-
25-
if (!nextPath.isImportDeclaration())
26-
return;
27-
28-
if (nextPath.node.specifiers.length !== 1)
29-
return;
30-
31-
const first = source.value;
32-
const second = nextPath.node.source.value;
33-
34-
const is = isExcluded(first, second, {
35-
direct: [
36-
['node:', 'node:'],
37-
['#', '#'],
38-
],
39-
reversed: [
40-
['./', './'],
41-
['../', '../'],
42-
['#', '#'],
43-
],
44-
});
45-
46-
if (is)
47-
return;
48-
49-
push({
50-
path,
51-
nextPath,
52-
});
19+
for (const path of paths) {
20+
const {node} = path;
21+
22+
if (!node)
23+
continue;
24+
25+
const {source, specifiers} = node;
26+
const {imports} = parseImportSpecifiers(specifiers);
27+
28+
if (imports.length < 4)
29+
continue;
30+
31+
const nextPath = path.getNextSibling();
32+
33+
if (!nextPath.isImportDeclaration())
34+
continue;
35+
36+
if (nextPath.node.specifiers.length !== 1)
37+
continue;
38+
39+
const first = source.value;
40+
const second = nextPath.node.source.value;
41+
42+
const is = isExcluded(first, second, {
43+
direct: [
44+
['node:', 'node:'],
45+
['#', '#'],
46+
],
47+
reversed: [
48+
['./', './'],
49+
['../', '../'],
50+
['#', '#'],
51+
],
52+
});
53+
54+
if (is)
55+
continue;
56+
57+
push({
58+
path,
59+
nextPath,
60+
});
61+
}
5362
},
5463
});
5564

packages/plugin-esm/lib/sort-imports-by-specifiers/index.spec.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,8 @@ test('putout: sort-imports-by-specifiers: external-hashed', (t) => {
4646
t.noReport('external-hashed');
4747
t.end();
4848
});
49+
50+
test('putout: sort-imports-by-specifiers: lots', (t) => {
51+
t.transform('lots');
52+
t.end();
53+
});

0 commit comments

Comments
 (0)