@putout/plugin-nodejs 2.0.1 → 3.2.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
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# @putout/plugin-nodejs [![NPM version][NPMIMGURL]][NPMURL]
|
|
2
2
|
|
|
3
3
|
[NPMIMGURL]: https://img.shields.io/npm/v/@putout/plugin-nodejs.svg?style=flat&longCache=true
|
|
4
|
-
[NPMURL]: https://npmjs.org/package/@putout/plugin-nodejs"npm"
|
|
4
|
+
[NPMURL]: https://npmjs.org/package/@putout/plugin-nodejs "npm"
|
|
5
5
|
|
|
6
6
|
🐊[`Putout`](https://github.com/coderaiser/putout) plugin adds ability to transform to new [nodejs.org](https://nodejs.io) API and best practices.
|
|
7
7
|
|
|
@@ -11,19 +11,24 @@
|
|
|
11
11
|
npm i putout @putout/plugin-nodejs -D
|
|
12
12
|
```
|
|
13
13
|
|
|
14
|
-
##
|
|
14
|
+
## Options
|
|
15
15
|
|
|
16
16
|
```json
|
|
17
17
|
{
|
|
18
18
|
"rules": {
|
|
19
19
|
"nodejs/convert-fs-promises": "on",
|
|
20
20
|
"nodejs/convert-promisify-to-fs-promises": "on",
|
|
21
|
-
"nodejs/convert-dirname-to-url": "on"
|
|
21
|
+
"nodejs/convert-dirname-to-url": "on",
|
|
22
|
+
"nodejs/convert-url-to-dirname": "on",
|
|
23
|
+
"nodejs/convert-top-level-return": "on",
|
|
24
|
+
"nodejs/remove-process-exit": "on"
|
|
22
25
|
}
|
|
23
26
|
}
|
|
24
27
|
```
|
|
25
28
|
|
|
26
|
-
|
|
29
|
+
## Rules
|
|
30
|
+
|
|
31
|
+
### convert-fs-promises
|
|
27
32
|
|
|
28
33
|
Convert [fs.promises](https://nodejs.org/dist/latest-v15.x/docs/api/fs.html#fs_fs_promises_api) into form that will be simpler to use and convert from in `ESM` to:
|
|
29
34
|
|
|
@@ -31,38 +36,38 @@ Convert [fs.promises](https://nodejs.org/dist/latest-v15.x/docs/api/fs.html#fs_f
|
|
|
31
36
|
import {readFile} from 'fs/promises';
|
|
32
37
|
```
|
|
33
38
|
|
|
34
|
-
|
|
39
|
+
#### ❌ Example of incorrect code
|
|
35
40
|
|
|
36
41
|
```js
|
|
37
42
|
const {readFile} = require('fs').promises;
|
|
38
43
|
```
|
|
39
44
|
|
|
40
|
-
|
|
45
|
+
#### ✅ Example of correct code
|
|
41
46
|
|
|
42
47
|
```js
|
|
43
48
|
const {readFile} = require('fs/promises');
|
|
44
49
|
```
|
|
45
50
|
|
|
46
|
-
|
|
51
|
+
### convert-promisify-to-fs-promises
|
|
47
52
|
|
|
48
|
-
|
|
53
|
+
#### ❌ Example of incorrect code
|
|
49
54
|
|
|
50
55
|
```js
|
|
51
56
|
const fs = require('fs');
|
|
52
57
|
const readFile = promisify(fs.readFile);
|
|
53
58
|
```
|
|
54
59
|
|
|
55
|
-
|
|
60
|
+
#### ✅ Example of correct code
|
|
56
61
|
|
|
57
62
|
```js
|
|
58
63
|
const {readFile} = require('fs/promises');
|
|
59
64
|
```
|
|
60
65
|
|
|
61
|
-
|
|
66
|
+
### convert-dirname-to-url
|
|
62
67
|
|
|
63
68
|
Only for `EcmaScript Modules`.
|
|
64
69
|
|
|
65
|
-
|
|
70
|
+
#### ❌ Example of incorrect code
|
|
66
71
|
|
|
67
72
|
```js
|
|
68
73
|
import {readFile} from 'fs/promises';
|
|
@@ -71,7 +76,7 @@ const file1 = join(__dirname, '../../package.json');
|
|
|
71
76
|
const file2 = path.join(__dirname, '../../package.json');
|
|
72
77
|
```
|
|
73
78
|
|
|
74
|
-
|
|
79
|
+
#### ✅ Example of correct code
|
|
75
80
|
|
|
76
81
|
```js
|
|
77
82
|
import {readFile} from 'fs/promises';
|
|
@@ -80,6 +85,46 @@ const file1 = new URL('../../package.json', import.meta.url);
|
|
|
80
85
|
const file2 = new URL('../../package.json', import.meta.url);
|
|
81
86
|
```
|
|
82
87
|
|
|
88
|
+
### convert-dirname-to-url
|
|
89
|
+
|
|
90
|
+
Only for `CommonJS`.
|
|
91
|
+
|
|
92
|
+
#### ❌ Example of incorrect code
|
|
93
|
+
|
|
94
|
+
```js
|
|
95
|
+
const {readFile} = require('fs/promises');
|
|
96
|
+
const file = new URL('../../package.json', import.meta.url);
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
#### ✅ Example of correct code
|
|
100
|
+
|
|
101
|
+
```js
|
|
102
|
+
const {readFile} = require('fs/promises');
|
|
103
|
+
const file = join(__dirname, '../../package.json');
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
### remove-process-exit
|
|
107
|
+
|
|
108
|
+
In most cases `process.exit()` is called from `bin` directory, if not - disable this rule using `match`.
|
|
109
|
+
|
|
110
|
+
```diff
|
|
111
|
+
-process.exit();
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
### convert-top-level-return
|
|
115
|
+
|
|
116
|
+
#### ❌ Example of incorrect code
|
|
117
|
+
|
|
118
|
+
```js
|
|
119
|
+
return;
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
#### ✅ Example of correct code
|
|
123
|
+
|
|
124
|
+
```js
|
|
125
|
+
process.exit();
|
|
126
|
+
```
|
|
127
|
+
|
|
83
128
|
## License
|
|
84
129
|
|
|
85
130
|
MIT
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const {types} = require('putout');
|
|
4
|
+
const {isFunction} = types;
|
|
5
|
+
|
|
6
|
+
module.exports.report = () => `"process.exit" should be used instead of top-level return`;
|
|
7
|
+
|
|
8
|
+
module.exports.filter = (path) => !path.findParent(isFunction);
|
|
9
|
+
|
|
10
|
+
module.exports.replace = () => ({
|
|
11
|
+
'return __a()': '{__a(); process.exit()}',
|
|
12
|
+
'return __a': 'process.exit()',
|
|
13
|
+
'return': 'process.exit()',
|
|
14
|
+
});
|
|
15
|
+
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const {operator} = require('putout');
|
|
4
|
+
const {isESM} = operator;
|
|
5
|
+
const not = (fn) => (...a) => !fn(...a);
|
|
6
|
+
|
|
7
|
+
module.exports.report = () => `Use __dirname instead of 'import.meta.url' in CommonJS`;
|
|
8
|
+
|
|
9
|
+
module.exports.filter = not(isESM);
|
|
10
|
+
|
|
11
|
+
module.exports.replace = () => ({
|
|
12
|
+
'new URL(__a, import.meta.url).pathname': 'join(__dirname, __a)',
|
|
13
|
+
});
|
package/lib/index.js
CHANGED
|
@@ -8,5 +8,8 @@ module.exports.rules = {
|
|
|
8
8
|
...getRule('convert-fs-promises'),
|
|
9
9
|
...getRule('convert-promisify-to-fs-promises'),
|
|
10
10
|
...getRule('convert-dirname-to-url'),
|
|
11
|
+
...getRule('convert-url-to-dirname'),
|
|
12
|
+
...getRule('convert-top-level-return'),
|
|
13
|
+
...getRule('remove-process-exit'),
|
|
11
14
|
};
|
|
12
15
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/plugin-nodejs",
|
|
3
|
-
"version": "2.0
|
|
3
|
+
"version": "3.2.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,17 +31,17 @@
|
|
|
31
31
|
"nodejs"
|
|
32
32
|
],
|
|
33
33
|
"devDependencies": {
|
|
34
|
-
"@putout/test": "^
|
|
34
|
+
"@putout/test": "^5.0.0",
|
|
35
35
|
"c8": "^7.5.0",
|
|
36
36
|
"eslint": "^8.0.1",
|
|
37
37
|
"eslint-plugin-node": "^11.0.0",
|
|
38
|
-
"eslint-plugin-putout": "^
|
|
38
|
+
"eslint-plugin-putout": "^13.0.0",
|
|
39
39
|
"lerna": "^4.0.0",
|
|
40
|
-
"madrun": "^
|
|
40
|
+
"madrun": "^9.0.0",
|
|
41
41
|
"nodemon": "^2.0.1"
|
|
42
42
|
},
|
|
43
43
|
"peerDependencies": {
|
|
44
|
-
"putout": ">=
|
|
44
|
+
"putout": ">=24"
|
|
45
45
|
},
|
|
46
46
|
"license": "MIT",
|
|
47
47
|
"engines": {
|