Skip to content

Commit 8f89126

Browse files
committed
feature: @putout/plugin-esm: group-imports-by-source: add support of hash
1 parent 855082d commit 8f89126

File tree

8 files changed

+83
-6
lines changed

8 files changed

+83
-6
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import {test} from 'supertape';
2+
import {createFilesystem} from '#filesystem';
3+
import {traverse} from '#fatlint';
4+
import {
5+
parse,
6+
print,
7+
operator,
8+
types,
9+
} from 'putout';
10+
import {createDisk} from '../fatdisk.js';
11+
import {readAST, writeAST} from '../fsast.js';
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import {createFilesystem} from '#filesystem';
2+
import {test} from 'supertape';
3+
import {traverse} from '#fatlint';
4+
import {createDisk} from '../fatdisk.js';
5+
import {
6+
parse,
7+
print,
8+
operator,
9+
types,
10+
} from 'putout';
11+
import {readAST, writeAST} from '../fsast.js';

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ module.exports.traverse = ({pathStore, push}) => ({
3131
const external = [];
3232
const internal = [];
3333
const builtin = [];
34+
const hashed = [];
3435
const all = pathStore().filter(isImportDeclaration);
3536

3637
if (!all.length)
@@ -49,12 +50,18 @@ module.exports.traverse = ({pathStore, push}) => ({
4950
continue;
5051
}
5152

53+
if (value.startsWith('#')) {
54+
hashed.push(current);
55+
continue;
56+
}
57+
5258
external.push(current);
5359
}
5460

5561
const grouped = [
5662
...builtin,
5763
...external,
64+
...hashed,
5865
...internal,
5966
];
6067

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const {createTest} = require('@putout/test');
55
const nodejs = require('@putout/plugin-nodejs');
66
const plugin = require('..');
77

8+
const sortImportsBySpecifiers = require('../sort-imports-by-specifiers');
89
const convertEsmToCommonJS = nodejs.rules['convert-esm-to-commonjs'];
910

1011
const test = createTest(__dirname, {
@@ -39,3 +40,10 @@ test('putout: group-imports-by-source: transform: convert-esm-to-commonjs', (t)
3940
});
4041
t.end();
4142
});
43+
44+
test('putout: group-imports-by-source: transform: sort-imports-by-specifiers', (t) => {
45+
t.transform('sort-imports-by-specifiers', {
46+
sortImportsBySpecifiers,
47+
});
48+
t.end();
49+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[
2+
["1"], {
3+
"key": "2",
4+
"value": "3"
5+
}, "index.js",{
6+
"size": 1745,
7+
"mtime": 1739812065844
8+
}
9+
]
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import {
2+
parse,
3+
print,
4+
operator,
5+
types,
6+
} from 'putout';
7+
import {createDisk} from '../fatdisk.js';

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

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,18 @@ module.exports.traverse = ({push}) => ({
2929
if (nextPath.node.specifiers.length !== 1)
3030
return;
3131

32-
if (!source.value.startsWith('./') && nextPath.node.source.value.startsWith('./'))
33-
return;
34-
35-
if (source.value.startsWith('node:') && !nextPath.node.source.value.startsWith('node:'))
36-
return;
32+
const is = isExcluded(source, nextPath, {
33+
direct: [
34+
['node:', 'node:'],
35+
['#', '#'],
36+
],
37+
reversed: [
38+
['./', './'],
39+
['../', '../'],
40+
],
41+
});
3742

38-
if (source.value.startsWith('#') && !nextPath.node.source.value.startsWith('#'))
43+
if (is)
3944
return;
4045

4146
push({
@@ -44,3 +49,17 @@ module.exports.traverse = ({push}) => ({
4449
});
4550
},
4651
});
52+
53+
function isExcluded(source, nextPath, {direct, reversed}) {
54+
for (const [current, next] of direct) {
55+
if (source.value.startsWith(current) && !nextPath.node.source.value.startsWith(next))
56+
return true;
57+
}
58+
59+
for (const [current, next] of reversed) {
60+
if (!source.value.startsWith(current) && nextPath.node.source.value.startsWith(next))
61+
return true;
62+
}
63+
64+
return false;
65+
}

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
@@ -38,3 +38,8 @@ test('putout: sort-imports-by-specifiers: three', (t) => {
3838
t.noTransform('three');
3939
t.end();
4040
});
41+
42+
test('putout: sort-imports-by-specifiers: external-internal', (t) => {
43+
t.noTransform('external-internal');
44+
t.end();
45+
});

0 commit comments

Comments
 (0)