@putout/plugin-nodejs 17.4.0 → 17.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,13 +1,17 @@
1
1
  import {isDeepStrictEqual} from 'node:util';
2
- import {operator, types} from 'putout';
3
-
4
- const {isVariableDeclaration} = types;
2
+ import {operator} from 'putout';
5
3
 
6
4
  const {
7
5
  replaceWithMultiple,
8
6
  remove,
7
+ compare,
9
8
  } = operator;
10
9
 
10
+ const REQUIRE = 'const __ = require(__)';
11
+
12
+ const compareWith = (a) => (b) => compare(b, a);
13
+ const compareWithRequire = compareWith(REQUIRE);
14
+
11
15
  export const report = () => 'Group require by id';
12
16
 
13
17
  export const fix = ({grouped}) => {
@@ -26,50 +30,45 @@ export const fix = ({grouped}) => {
26
30
  replaceWithMultiple(first, nodes);
27
31
  };
28
32
 
29
- export const traverse = ({pathStore, push}) => ({
30
- 'const __ = require(__)': (path) => {
31
- if (!path.parentPath.isProgram())
33
+ export const traverse = ({push}) => ({
34
+ Program(path) {
35
+ const external = [];
36
+ const internal = [];
37
+ const builtin = [];
38
+ const all = path.get('body').filter(compareWithRequire);
39
+
40
+ if (!all.length)
32
41
  return;
33
42
 
34
- pathStore(path);
35
- },
36
- 'Program': {
37
- exit(path) {
38
- const external = [];
39
- const internal = [];
40
- const builtin = [];
41
- const all = pathStore().filter(isVariableDeclaration);
43
+ for (const current of all) {
44
+ const [declaration] = current.node.declarations;
45
+ const {value} = declaration.init.arguments[0];
42
46
 
43
- for (const current of all) {
44
- const [declaration] = current.node.declarations;
45
- const {value} = declaration.init.arguments[0];
46
-
47
- if (!value || value.startsWith('.')) {
48
- internal.push(current);
49
- continue;
50
- }
51
-
52
- if (value.startsWith('node:')) {
53
- builtin.push(current);
54
- continue;
55
- }
56
-
57
- external.push(current);
47
+ if (!value || value.startsWith('.')) {
48
+ internal.push(current);
49
+ continue;
58
50
  }
59
51
 
60
- const grouped = [
61
- ...builtin,
62
- ...external,
63
- ...internal,
64
- ];
65
-
66
- if (isDeepStrictEqual(all, grouped))
67
- return;
52
+ if (value.startsWith('node:')) {
53
+ builtin.push(current);
54
+ continue;
55
+ }
68
56
 
69
- push({
70
- path,
71
- grouped,
72
- });
73
- },
57
+ external.push(current);
58
+ }
59
+
60
+ const grouped = [
61
+ ...builtin,
62
+ ...external,
63
+ ...internal,
64
+ ];
65
+
66
+ if (isDeepStrictEqual(all, grouped))
67
+ return;
68
+
69
+ push({
70
+ path,
71
+ grouped,
72
+ });
74
73
  },
75
74
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/plugin-nodejs",
3
- "version": "17.4.0",
3
+ "version": "17.6.0",
4
4
  "type": "module",
5
5
  "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
6
6
  "description": "🐊Putout plugin adds ability to transform code to new API of Node.js",
@@ -42,7 +42,7 @@
42
42
  "@putout/eslint-flat": "^3.0.0",
43
43
  "@putout/plugin-declare": "*",
44
44
  "@putout/plugin-declare-before-reference": "*",
45
- "@putout/plugin-esm": "^5.0.0",
45
+ "@putout/plugin-esm": "*",
46
46
  "@putout/plugin-putout": "*",
47
47
  "@putout/plugin-reuse-duplicate-init": "*",
48
48
  "@putout/plugin-typescript": "*",