@putout/plugin-nodejs 4.3.0 β 4.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.
- package/README.md +34 -10
- package/lib/declare/index.js +1 -0
- package/lib/declare/modules/events.json +12 -0
- package/lib/declare-after-require/index.js +122 -0
- package/lib/index.js +1 -0
- package/package.json +6 -4
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
|
}
|
|
@@ -129,16 +130,17 @@ process.exit();
|
|
|
129
130
|
|
|
130
131
|
Add declarations to built-in node.js modules:
|
|
131
132
|
|
|
132
|
-
- [child_process](https://nodejs.org/dist/latest-
|
|
133
|
-
- [
|
|
134
|
-
- [
|
|
135
|
-
- [
|
|
136
|
-
- [
|
|
137
|
-
- [
|
|
138
|
-
- [
|
|
139
|
-
- [
|
|
140
|
-
- [
|
|
141
|
-
- [
|
|
133
|
+
- [child_process](https://nodejs.org/dist/latest-v18.x/docs/api/child_process.html);
|
|
134
|
+
- [events](https://nodejs.org/dist/latest-v18.x/docs/api/events.html);
|
|
135
|
+
- [fs](https://nodejs.org/dist/latest-v18.x/docs/api/fs.html);
|
|
136
|
+
- [path](https://nodejs.org/dist/latest-v18.x/docs/api/path.html);
|
|
137
|
+
- [process](https://nodejs.org/dist/latest-v18.x/docs/api/process.html);
|
|
138
|
+
- [module](https://nodejs.org/dist/latest-v18.x/docs/api/module.html);
|
|
139
|
+
- [stream](https://nodejs.org/dist/latest-v18.x/docs/api/stream.html);
|
|
140
|
+
- [os](https://nodejs.org/dist/latest-v18.x/docs/api/os.html);
|
|
141
|
+
- [url](https://nodejs.org/dist/latest-v18.x/docs/api/url.html);
|
|
142
|
+
- [util](https://nodejs.org/dist/latest-v18.x/docs/api/util.html);
|
|
143
|
+
- [zlib](https://nodejs.org/dist/latest-v18.x/docs/api/zlib.html);
|
|
142
144
|
|
|
143
145
|
#### β Example of incorrect code
|
|
144
146
|
|
|
@@ -153,6 +155,28 @@ import {readFile} from 'fs/promises';
|
|
|
153
155
|
await readFile('hello.txt', 'utf8');
|
|
154
156
|
```
|
|
155
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
|
+
|
|
156
180
|
## License
|
|
157
181
|
|
|
158
182
|
MIT
|
package/lib/declare/index.js
CHANGED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
{
|
|
2
|
+
"once": "import {once} from 'events'",
|
|
3
|
+
"on": "import {on} from 'events'",
|
|
4
|
+
"getEventListeners": "import {getEventListeners} from 'events'",
|
|
5
|
+
"EventEmitter": "import {EventEmitter} from 'events'",
|
|
6
|
+
"captureRejectionSymbol": "import {captureRejectionSymbol} from 'events'",
|
|
7
|
+
"captureRejections": "import {captureRejections} from 'events'",
|
|
8
|
+
"EventEmitterAsyncResource": "import {EventEmitterAsyncResource} from 'events'",
|
|
9
|
+
"defaultMaxListeners": "import {defaultMaxListeners} from 'events'",
|
|
10
|
+
"setMaxListeners": "import {setMaxListeners} from 'events'",
|
|
11
|
+
"listenerCount": "import {listenerCount} from 'events'"
|
|
12
|
+
}
|
|
@@ -0,0 +1,122 @@
|
|
|
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 && lastRequireLine < getReferenceLine(path)) {
|
|
85
|
+
push({
|
|
86
|
+
path,
|
|
87
|
+
firstRequire,
|
|
88
|
+
lastRequire,
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
},
|
|
93
|
+
},
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
function getName(path) {
|
|
97
|
+
const idPath = path.get('declarations.0.id');
|
|
98
|
+
|
|
99
|
+
if (idPath.isIdentifier())
|
|
100
|
+
return idPath.node.name;
|
|
101
|
+
|
|
102
|
+
if (idPath.isObjectPattern() && idPath.node.properties.length)
|
|
103
|
+
return idPath.node.properties[0].key.name;
|
|
104
|
+
|
|
105
|
+
return null;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function getReferenceLine(path) {
|
|
109
|
+
const name = getName(path);
|
|
110
|
+
const binding = path.scope.bindings[name];
|
|
111
|
+
|
|
112
|
+
if (!binding)
|
|
113
|
+
return -1;
|
|
114
|
+
|
|
115
|
+
const {referencePaths} = binding;
|
|
116
|
+
|
|
117
|
+
if (!referencePaths.length)
|
|
118
|
+
return Infinity;
|
|
119
|
+
|
|
120
|
+
return referencePaths[0].node.loc.start.line;
|
|
121
|
+
}
|
|
122
|
+
|
package/lib/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/plugin-nodejs",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.6.0",
|
|
4
4
|
"type": "commonjs",
|
|
5
5
|
"author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
|
|
6
|
-
"description": "
|
|
6
|
+
"description": "πPutout plugin adds ability to transform code to new API of Node.js",
|
|
7
7
|
"homepage": "https://github.com/coderaiser/putout/tree/master/packages/plugin-nodejs#readme",
|
|
8
8
|
"main": "lib/index.js",
|
|
9
9
|
"release": false,
|
|
@@ -31,12 +31,14 @@
|
|
|
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",
|
|
37
39
|
"eslint-plugin-node": "^11.0.0",
|
|
38
|
-
"eslint-plugin-putout": "^
|
|
39
|
-
"lerna": "^
|
|
40
|
+
"eslint-plugin-putout": "^15.0.0",
|
|
41
|
+
"lerna": "^5.0.0",
|
|
40
42
|
"madrun": "^9.0.0",
|
|
41
43
|
"montag": "^1.2.1",
|
|
42
44
|
"nodemon": "^2.0.1"
|