@putout/plugin-nodejs 12.0.0 → 13.0.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,12 +33,13 @@ 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);
39
40
  - ✅ [rename-file-mjs-to-js](#rename-file-mjs-to-js);
40
41
  - ✅ [remove-useless-strict-mode](#remove-useless-strict-mode);
41
- - ✅ [remove-illigal-strict-mode](#remove-useless-strict-mode);
42
+ - ✅ [remove-illegal-strict-mode](#remove-useless-strict-mode);
42
43
  - ✅ [cjs-file](#cjs-file);
43
44
  - ✅ [mjs-file](#mjs-file);
44
45
 
@@ -63,9 +64,11 @@ 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",
71
+ "nodejs/remove-illegal-strict-mode": "on",
69
72
  "nodejs/remove-useless-promisify": "on"
70
73
  }
71
74
  }
@@ -300,7 +303,7 @@ When you want to skip some declaration use `dismiss`:
300
303
  >
301
304
  > (c) [Nodejs.org](https://nodejs.org/en/knowledge/getting-started/what-is-require/)
302
305
 
303
- 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).
304
307
 
305
308
  ### ❌ Example of incorrect code
306
309
 
@@ -347,6 +350,37 @@ const args = minimist({
347
350
  });
348
351
  ```
349
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
+
350
384
  ### exports
351
385
 
352
386
  ### ❌ Example of incorrect code
@@ -487,7 +521,7 @@ import a from 'b';
487
521
  import a from 'b';
488
522
  ```
489
523
 
490
- ## remove-illigal-strict-mode
524
+ ## remove-illegal-strict-mode
491
525
 
492
526
  > `SyntaxError: "use strict" not allowed in function with non-simple parameters`
493
527
  > The JavaScript exception `"use strict" not allowed in function` occurs when a `use strict` directive is used at the top of a function with default parameters, rest parameters, or destructuring parameters.
@@ -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,71 @@
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
+ remove(current);
19
+ nodes.push(node);
20
+ }
21
+
22
+ replaceWithMultiple(first, nodes);
23
+ };
24
+
25
+ module.exports.traverse = ({pathStore, push}) => ({
26
+ 'const __ = require(__)': (path) => {
27
+ if (!path.parentPath.isProgram())
28
+ return;
29
+
30
+ pathStore(path);
31
+ },
32
+ 'Program': {
33
+ exit(path) {
34
+ const external = [];
35
+ const internal = [];
36
+ const builtin = [];
37
+ const all = pathStore();
38
+
39
+ for (const current of all) {
40
+ const [declaration] = current.node.declarations;
41
+ const {value} = declaration.init.arguments[0];
42
+
43
+ if (!value || value.startsWith('.')) {
44
+ internal.push(current);
45
+ continue;
46
+ }
47
+
48
+ if (value.startsWith('node:')) {
49
+ builtin.push(current);
50
+ continue;
51
+ }
52
+
53
+ external.push(current);
54
+ }
55
+
56
+ const grouped = [
57
+ ...builtin,
58
+ ...external,
59
+ ...internal,
60
+ ];
61
+
62
+ if (isDeepStrictEqual(all, grouped))
63
+ return;
64
+
65
+ push({
66
+ path,
67
+ grouped,
68
+ });
69
+ },
70
+ },
71
+ });
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,
@@ -53,6 +54,7 @@ module.exports.rules = {
53
54
 
54
55
  'add-missing-strict-mode': strictMode.rules['add-missing'],
55
56
  'remove-useless-strict-mode': strictMode.rules['remove-useless'],
56
- 'remove-illigal-strict-mode': strictMode.rules['remove-illigal'],
57
+ 'remove-illegal-strict-mode': strictMode.rules['remove-illegal'],
57
58
  'remove-useless-promisify': removeUselessPromisify,
59
+ 'group-require-by-id': groupRequireById,
58
60
  };
@@ -2,10 +2,10 @@
2
2
 
3
3
  const addMissing = require('./add-missing');
4
4
  const removeUseless = require('./remove-useless');
5
- const removeIlligal = require('./remove-illigal');
5
+ const removeIllegal = require('./remove-illegal');
6
6
 
7
7
  module.exports.rules = {
8
8
  'add-missing': addMissing,
9
9
  'remove-useless': removeUseless,
10
- 'remove-illigal': removeIlligal,
10
+ 'remove-illegal': removeIllegal,
11
11
  };
@@ -11,7 +11,7 @@ const {
11
11
 
12
12
  const {remove} = operator;
13
13
 
14
- module.exports.report = () => `Avoid illigal 'use strict'`;
14
+ module.exports.report = () => `Avoid illegal 'use strict'`;
15
15
 
16
16
  module.exports.fix = (path) => {
17
17
  remove(path);
@@ -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.0",
3
+ "version": "13.0.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",
@@ -54,7 +54,7 @@
54
54
  "nodemon": "^3.0.1"
55
55
  },
56
56
  "peerDependencies": {
57
- "putout": ">=36"
57
+ "putout": ">=37"
58
58
  },
59
59
  "license": "MIT",
60
60
  "engines": {