@putout/plugin-nodejs 12.0.1 → 13.1.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.
package/README.md CHANGED
@@ -33,6 +33,7 @@ npm i putout @putout/plugin-nodejs -D
33
33
  - ✅ [convert-url-to-dirname](#convert-url-to-dirname);
34
34
  - ✅ [declare](#declare);
35
35
  - ✅ [declare-after-require](#declare-after-require);
36
+ - ✅ [group-require-by-id](#group-require-by-id);
36
37
  - ✅ [remove-process-exit](#remove-process-exit);
37
38
  - ✅ [remove-useless-promisify](#remove-useless-promisify);
38
39
  - ✅ [rename-file-cjs-to-js](#rename-file-cjs-to-js);
@@ -63,6 +64,7 @@ npm i putout @putout/plugin-nodejs -D
63
64
  "nodejs/convert-top-level-return": "on",
64
65
  "nodejs/declare": "on",
65
66
  "nodejs/declare-after-require": "on",
67
+ "nodejs/group-require-by-id": "on",
66
68
  "nodejs/remove-process-exit": "on",
67
69
  "nodejs/add-missing-strict-mode": "on",
68
70
  "nodejs/remove-useless-strict-mode": "on",
@@ -301,7 +303,7 @@ When you want to skip some declaration use `dismiss`:
301
303
  >
302
304
  > (c) [Nodejs.org](https://nodejs.org/en/knowledge/getting-started/what-is-require/)
303
305
 
304
- Check out in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/https://putout.cloudcmd.io/#/gist/ddf5731ae829beec4d3018d4d9ac2150/342738b63337bfa9b4fc08c5b301483ea2b5ba9c).
306
+ For ESM use [declare-imports-first](https://github.com/coderaiser/putout/tree/master/packages/plugin-declare-imports-first#readme). Check out in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/https://putout.cloudcmd.io/#/gist/ddf5731ae829beec4d3018d4d9ac2150/342738b63337bfa9b4fc08c5b301483ea2b5ba9c).
305
307
 
306
308
  ### ❌ Example of incorrect code
307
309
 
@@ -348,6 +350,37 @@ const args = minimist({
348
350
  });
349
351
  ```
350
352
 
353
+ ## group-require-by-id
354
+
355
+ Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/ff39c5d912d836a25b96772d8045dacb/fa8d8e1ebf8ac5f19a536247536f4bccf4fdac3d).
356
+
357
+ ### ❌ Example of incorrect code
358
+
359
+ ```js
360
+ const ss = require('../../bb/ss');
361
+ const d = require('../hello');
362
+ const react = require('react');
363
+ const {lodash} = require('lodash');
364
+ const fs = require('node:fs');
365
+ const b = require('./ss');
366
+ const m = require(x);
367
+ const c = 5;
368
+ ```
369
+
370
+ ### ✅ Example of correct code
371
+
372
+ ```js
373
+ const fs = require('node:fs');
374
+ const react = require('react');
375
+ const {lodash} = require('lodash');
376
+ const ss = require('../../bb/ss');
377
+ const d = require('../hello');
378
+
379
+ const b = require('./ss');
380
+ const m = require(x);
381
+ const c = 5;
382
+ ```
383
+
351
384
  ### exports
352
385
 
353
386
  ### ❌ Example of incorrect code
@@ -1,7 +1,8 @@
1
1
  'use strict';
2
2
 
3
- const {types, operator} = require('putout');
4
3
  const {isBuiltin} = require('node:module');
4
+ const {types, operator} = require('putout');
5
+
5
6
  const {
6
7
  setLiteralValue,
7
8
  getTemplateValues,
@@ -0,0 +1,72 @@
1
+ 'use strict';
2
+
3
+ const {isDeepStrictEqual} = require('node:util');
4
+ const {operator} = require('putout');
5
+ const {
6
+ replaceWithMultiple,
7
+ remove,
8
+ } = operator;
9
+
10
+ module.exports.report = () => 'Group require by id';
11
+
12
+ module.exports.fix = ({grouped}) => {
13
+ const [first, ...others] = grouped;
14
+ const nodes = [first.node];
15
+
16
+ for (const current of others) {
17
+ const {node} = current;
18
+ delete node.declarations[0].loc;
19
+ remove(current);
20
+ nodes.push(node);
21
+ }
22
+
23
+ replaceWithMultiple(first, nodes);
24
+ };
25
+
26
+ module.exports.traverse = ({pathStore, push}) => ({
27
+ 'const __ = require(__)': (path) => {
28
+ if (!path.parentPath.isProgram())
29
+ return;
30
+
31
+ pathStore(path);
32
+ },
33
+ 'Program': {
34
+ exit(path) {
35
+ const external = [];
36
+ const internal = [];
37
+ const builtin = [];
38
+ const all = pathStore();
39
+
40
+ for (const current of all) {
41
+ const [declaration] = current.node.declarations;
42
+ const {value} = declaration.init.arguments[0];
43
+
44
+ if (!value || value.startsWith('.')) {
45
+ internal.push(current);
46
+ continue;
47
+ }
48
+
49
+ if (value.startsWith('node:')) {
50
+ builtin.push(current);
51
+ continue;
52
+ }
53
+
54
+ external.push(current);
55
+ }
56
+
57
+ const grouped = [
58
+ ...builtin,
59
+ ...external,
60
+ ...internal,
61
+ ];
62
+
63
+ if (isDeepStrictEqual(all, grouped))
64
+ return;
65
+
66
+ push({
67
+ path,
68
+ grouped,
69
+ });
70
+ },
71
+ },
72
+ });
package/lib/index.js CHANGED
@@ -26,6 +26,7 @@ const renameFileMjsToJs = require('./rename-file-mjs-to-js');
26
26
 
27
27
  const strictMode = require('./strict-mode');
28
28
  const removeUselessPromisify = require('./remove-useless-promisify');
29
+ const groupRequireById = require('./group-require-by-id');
29
30
 
30
31
  module.exports.rules = {
31
32
  'convert-buffer-to-buffer-alloc': convertBufferToBufferAlloc,
@@ -55,4 +56,5 @@ module.exports.rules = {
55
56
  'remove-useless-strict-mode': strictMode.rules['remove-useless'],
56
57
  'remove-illegal-strict-mode': strictMode.rules['remove-illegal'],
57
58
  'remove-useless-promisify': removeUselessPromisify,
59
+ 'group-require-by-id': groupRequireById,
58
60
  };
@@ -23,12 +23,12 @@ module.exports.traverse = ({push}) => ({
23
23
  if (!isFunction(fnPath))
24
24
  return;
25
25
 
26
- if (isIlligal(fnPath))
26
+ if (isIllegal(fnPath))
27
27
  push(path.parentPath);
28
28
  },
29
29
  });
30
30
 
31
- function isIlligal(fnPath) {
31
+ function isIllegal(fnPath) {
32
32
  const params = fnPath.get('params');
33
33
 
34
34
  for (const param of params) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/plugin-nodejs",
3
- "version": "12.0.1",
3
+ "version": "13.1.0",
4
4
  "type": "commonjs",
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",
@@ -41,7 +41,9 @@
41
41
  "devDependencies": {
42
42
  "@putout/eslint-flat": "^2.0.0",
43
43
  "@putout/plugin-declare": "*",
44
+ "@putout/plugin-declare-before-reference": "*",
44
45
  "@putout/plugin-putout": "*",
46
+ "@putout/plugin-reuse-duplicate-init": "*",
45
47
  "@putout/plugin-typescript": "*",
46
48
  "@putout/test": "^11.0.0",
47
49
  "c8": "^10.0.0",
@@ -54,7 +56,7 @@
54
56
  "nodemon": "^3.0.1"
55
57
  },
56
58
  "peerDependencies": {
57
- "putout": ">=36"
59
+ "putout": ">=37"
58
60
  },
59
61
  "license": "MIT",
60
62
  "engines": {