Skip to content

Commit cbf5429

Browse files
committed
feature: @putout/plugin-nodejs: group-require-by-id: changed node before exit: add support
1 parent 67a02fa commit cbf5429

File tree

4 files changed

+26
-6
lines changed

4 files changed

+26
-6
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
const a = () => {};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
const a = require('b');

packages/plugin-nodejs/lib/group-require-by-id/index.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
import {isDeepStrictEqual} from 'node:util';
2-
import {operator, types} from 'putout';
3-
4-
const {isVariableDeclaration} = types;
2+
import {operator} from 'putout';
53

64
const {
75
replaceWithMultiple,
86
remove,
7+
compare,
98
} = operator;
109

10+
const REQUIRE = 'const __ = require(__)';
11+
12+
const compareWith = (a) => (b) => compare(b, a);
13+
const compareWithRequire = compareWith(REQUIRE);
14+
1115
export const report = () => 'Group require by id';
1216

1317
export const fix = ({grouped}) => {
@@ -27,18 +31,18 @@ export const fix = ({grouped}) => {
2731
};
2832

2933
export const traverse = ({pathStore, push}) => ({
30-
'const __ = require(__)': (path) => {
34+
[REQUIRE]: (path) => {
3135
if (!path.parentPath.isProgram())
3236
return;
3337

3438
pathStore(path);
3539
},
36-
'Program': {
40+
Program: {
3741
exit(path) {
3842
const external = [];
3943
const internal = [];
4044
const builtin = [];
41-
const all = pathStore().filter(isVariableDeclaration);
45+
const all = pathStore().filter(compareWithRequire);
4246

4347
for (const current of all) {
4448
const [declaration] = current.node.declarations;

packages/plugin-nodejs/lib/group-require-by-id/index.spec.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,20 @@ test('nodejs: group-require-by-id: merge-declaration-with-export', (t) => {
5454
t.end();
5555
});
5656

57+
test('nodejs: group-require-by-id: no-argument', (t) => {
58+
const replacer = {
59+
report: () => `Replace`,
60+
replace: () => ({
61+
'const __a = require(__b)': 'const __a = () => {}',
62+
}),
63+
};
64+
65+
t.transform('no-argument', {
66+
replacer,
67+
});
68+
t.end();
69+
});
70+
5771
test('nodejs: group-require-by-id: merge', (t) => {
5872
const merge = {
5973
report: () => `Inline export`,

0 commit comments

Comments
 (0)