@putout/plugin-nodejs 4.4.0 β†’ 4.5.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
@@ -25,6 +25,7 @@ npm i putout @putout/plugin-nodejs -D
25
25
  "nodejs/convert-dirname-to-url": "on",
26
26
  "nodejs/convert-url-to-dirname": "on",
27
27
  "nodejs/convert-top-level-return": "on",
28
+ "nodejs/declare-after-require": "on",
28
29
  "nodejs/remove-process-exit": "on"
29
30
  }
30
31
  }
@@ -154,6 +155,28 @@ import {readFile} from 'fs/promises';
154
155
  await readFile('hello.txt', 'utf8');
155
156
  ```
156
157
 
158
+ ### declare-after-require
159
+
160
+ > **Node.js** follows the **CommonJS** module system, and the builtin `require` function is the easiest way to include modules that exist in separate files. The basic functionality of `require` is that it reads a JavaScript file, executes the file, and then proceeds to return the `exports` object.
161
+ >
162
+ > (c) [Nodejs.org](https://nodejs.org/en/knowledge/getting-started/what-is-require/)
163
+
164
+ Check out in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/https://putout.cloudcmd.io/#/gist/ddf5731ae829beec4d3018d4d9ac2150/342738b63337bfa9b4fc08c5b301483ea2b5ba9c).
165
+
166
+ #### ❌ Example of incorrect code
167
+
168
+ ```js
169
+ const name = 'hello.txt';
170
+ const {readFile} = require('fs/promises');
171
+ ```
172
+
173
+ #### βœ… Example of correct code
174
+
175
+ ```js
176
+ const {readFile} = require('fs/promises');
177
+ const name = 'hello.txt';
178
+ ```
179
+
157
180
  ## License
158
181
 
159
182
  MIT
@@ -0,0 +1,94 @@
1
+ 'use strict';
2
+
3
+ const {operator} = require('putout');
4
+
5
+ const {
6
+ remove,
7
+ compareAny,
8
+ } = operator;
9
+
10
+ const REQUIRE_LIST = [
11
+ 'const __a = require(__b)',
12
+ 'const __a = require(__b)(__args)',
13
+ 'const __a = require(__b).__c',
14
+ ];
15
+
16
+ module.exports.report = ({path}) => {
17
+ const idPath = path.get('declarations.0.id');
18
+ const id = String(idPath).replace(/\s+/g, '');
19
+
20
+ return `Declare '${id}' after last 'require()'`;
21
+ };
22
+
23
+ module.exports.fix = ({path, firstRequire, lastRequire}) => {
24
+ const {node} = path;
25
+ const {comments} = path.node;
26
+
27
+ delete node.loc;
28
+ node.__putoutNodeDeclareAfterRequire = true;
29
+
30
+ if (comments) {
31
+ firstRequire.node.comments = comments;
32
+ delete path.node.comments;
33
+ }
34
+
35
+ remove(path);
36
+ lastRequire.insertAfter(node);
37
+ };
38
+
39
+ module.exports.traverse = ({push, listStore}) => ({
40
+ 'const __a = __b': (path) => {
41
+ if (!path.parentPath.isProgram())
42
+ return;
43
+
44
+ listStore(path);
45
+ },
46
+ 'Program': {
47
+ exit() {
48
+ const requirePaths = [];
49
+ const constPaths = [];
50
+
51
+ for (const path of listStore()) {
52
+ if (!path.node)
53
+ continue;
54
+
55
+ if (path.node.__putoutNodeDeclareAfterRequire)
56
+ continue;
57
+
58
+ if (path.node.declarations[0].id.name === 'require')
59
+ continue;
60
+
61
+ if (compareAny(path, REQUIRE_LIST)) {
62
+ requirePaths.push(path);
63
+ continue;
64
+ }
65
+
66
+ constPaths.push(path);
67
+ }
68
+
69
+ if (!requirePaths.length)
70
+ return;
71
+
72
+ const firstRequire = requirePaths.at(0);
73
+ const lastRequire = requirePaths.at(-1);
74
+
75
+ if (!lastRequire.node.loc)
76
+ return;
77
+
78
+ const lastRequireLine = lastRequire.node.loc.start.line;
79
+
80
+ for (const path of constPaths) {
81
+ const {loc} = path.node;
82
+ const line = !loc ? 0 : loc.start.line;
83
+
84
+ if (line < lastRequireLine) {
85
+ push({
86
+ path,
87
+ firstRequire,
88
+ lastRequire,
89
+ });
90
+ }
91
+ }
92
+ },
93
+ },
94
+ });
package/lib/index.js CHANGED
@@ -11,6 +11,7 @@ module.exports.rules = {
11
11
  ...getRule('convert-url-to-dirname'),
12
12
  ...getRule('convert-top-level-return'),
13
13
  ...getRule('declare'),
14
+ ...getRule('declare-after-require'),
14
15
  ...getRule('remove-process-exit'),
15
16
  };
16
17
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/plugin-nodejs",
3
- "version": "4.4.0",
3
+ "version": "4.5.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",
@@ -31,6 +31,8 @@
31
31
  "nodejs"
32
32
  ],
33
33
  "devDependencies": {
34
+ "@putout/plugin-convert-esm-to-commonjs": "*",
35
+ "@putout/plugin-putout": "*",
34
36
  "@putout/test": "^5.0.0",
35
37
  "c8": "^7.5.0",
36
38
  "eslint": "^8.0.1",