Skip to content

Commit e033dab

Browse files
committed
feature: @putout/plugin-esm: group-imports-by-source: sort-imports-by-specifiers: couple
1 parent 1df9617 commit e033dab

File tree

4 files changed

+68
-46
lines changed

4 files changed

+68
-46
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import {hello11} from '@hello11';
2+
import {
3+
abc,
4+
def,
5+
hello,
6+
world,
7+
} from '@hello';
8+
import {
9+
abc1,
10+
def1,
11+
hello1,
12+
world1,
13+
} from '@hello1';
14+
import {
15+
Iabc,
16+
Idef,
17+
Ihello,
18+
} from '../hello.js';

packages/plugin-esm/lib/group-imports-by-source/index.js

Lines changed: 41 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -23,54 +23,51 @@ export const fix = ({grouped}) => {
2323
replaceWithMultiple(first, nodes);
2424
};
2525

26-
export const traverse = ({pathStore, push}) => ({
27-
ImportDeclaration: pathStore,
28-
Program: {
29-
exit(path) {
30-
const external = [];
31-
const internal = [];
32-
const builtin = [];
33-
const hashed = [];
34-
const all = pathStore().filter(isImportDeclaration);
26+
export const traverse = ({push}) => ({
27+
Program(path) {
28+
const external = [];
29+
const internal = [];
30+
const builtin = [];
31+
const hashed = [];
32+
const all = path.get('body').filter(isImportDeclaration);
33+
34+
if (!all.length)
35+
return;
36+
37+
for (const current of all) {
38+
const {value} = current.node.source;
3539

36-
if (!all.length)
37-
return;
38-
39-
for (const current of all) {
40-
const {value} = current.node.source;
41-
42-
if (value.startsWith('.')) {
43-
internal.push(current);
44-
continue;
45-
}
46-
47-
if (value.startsWith('node:')) {
48-
builtin.push(current);
49-
continue;
50-
}
51-
52-
if (value.startsWith('#')) {
53-
hashed.push(current);
54-
continue;
55-
}
56-
57-
external.push(current);
40+
if (value.startsWith('.')) {
41+
internal.push(current);
42+
continue;
5843
}
5944

60-
const grouped = [
61-
...builtin,
62-
...external,
63-
...hashed,
64-
...internal,
65-
];
45+
if (value.startsWith('node:')) {
46+
builtin.push(current);
47+
continue;
48+
}
6649

67-
if (isDeepStrictEqual(all, grouped))
68-
return;
50+
if (value.startsWith('#')) {
51+
hashed.push(current);
52+
continue;
53+
}
6954

70-
push({
71-
path,
72-
grouped,
73-
});
74-
},
55+
external.push(current);
56+
}
57+
58+
const grouped = [
59+
...builtin,
60+
...external,
61+
...hashed,
62+
...internal,
63+
];
64+
65+
if (isDeepStrictEqual(all, grouped))
66+
return;
67+
68+
push({
69+
path,
70+
grouped,
71+
});
7572
},
7673
});

packages/plugin-esm/lib/group-imports-by-source/index.spec.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,10 @@ test('putout: group-imports-by-source: no report after transform: sort-imports-b
4949
});
5050
t.end();
5151
});
52+
53+
test('putout: group-imports-by-source: no report: sort-imports-by-specifiers-couple', (t) => {
54+
t.noReport('sort-imports-by-specifiers-couple', [
55+
['sort-imports-by-specifiers', sortImportsBySpecifiers],
56+
]);
57+
t.end();
58+
});

packages/plugin-esm/test/fixture/sort-imports-by-specifiers-fix.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@ import {
44
unlink,
55
mkdir,
66
} from 'node:path';
7+
import a1 from 'a1';
8+
import {test} from 'supertape';
79
import {
810
a,
911
b,
1012
c,
1113
d,
1214
} from 'd';
13-
import {test} from 'supertape';
1415
import {
1516
hello,
1617
world,
1718
something,
1819
else1,
1920
} from 'hello';
20-
import a1 from 'a1';
2121
import tryCatch from './index.js';

0 commit comments

Comments
 (0)