@putout/plugin-nodejs 4.7.0 β 4.8.1
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
|
@@ -20,6 +20,7 @@ npm i putout @putout/plugin-nodejs -D
|
|
|
20
20
|
```json
|
|
21
21
|
{
|
|
22
22
|
"rules": {
|
|
23
|
+
"nodejs/convert-buffer-to-buffer-alloc": "on",
|
|
23
24
|
"nodejs/convert-fs-promises": "on",
|
|
24
25
|
"nodejs/convert-promisify-to-fs-promises": "on",
|
|
25
26
|
"nodejs/convert-dirname-to-url": "on",
|
|
@@ -33,6 +34,42 @@ npm i putout @putout/plugin-nodejs -D
|
|
|
33
34
|
|
|
34
35
|
## Rules
|
|
35
36
|
|
|
37
|
+
### convert-buffer-to-buffer-alloc
|
|
38
|
+
|
|
39
|
+
> The `Buffer()` function and `new Buffer()` constructor are **deprecated** due to API usability issues that can lead to accidental security issues.
|
|
40
|
+
>
|
|
41
|
+
> (c) [DEP0005](https://nodejs.org/api/deprecations.html#deprecations_dep0005_buffer_constructor)
|
|
42
|
+
|
|
43
|
+
Check out in π[Putout Editor](https://putout.cloudcmd.io/#/gist/5379bcdfa3d76f7b7121c9671ae48375/2fc2c7f96fc8284788c00914a9b29bfeea8b13d4).
|
|
44
|
+
|
|
45
|
+
#### β Example of incorrect code
|
|
46
|
+
|
|
47
|
+
```js
|
|
48
|
+
const n = 100;
|
|
49
|
+
const buf = [];
|
|
50
|
+
|
|
51
|
+
new Buffer(123);
|
|
52
|
+
new Buffer(n);
|
|
53
|
+
new Buffer('hello');
|
|
54
|
+
|
|
55
|
+
new Buffer([]);
|
|
56
|
+
new Buffer(buf);
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
#### β
Example of correct code
|
|
60
|
+
|
|
61
|
+
```js
|
|
62
|
+
const n = 100;
|
|
63
|
+
const buf = [];
|
|
64
|
+
|
|
65
|
+
Buffer.alloc(123);
|
|
66
|
+
Buffer.alloc(n);
|
|
67
|
+
Buffer.from('hello');
|
|
68
|
+
|
|
69
|
+
Buffer.from([]);
|
|
70
|
+
Buffer.from(buf);
|
|
71
|
+
```
|
|
72
|
+
|
|
36
73
|
### convert-fs-promises
|
|
37
74
|
|
|
38
75
|
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 to and from **ESM**.
|
|
@@ -71,7 +108,8 @@ Only for **ESM**.
|
|
|
71
108
|
#### β Example of incorrect code
|
|
72
109
|
|
|
73
110
|
```js
|
|
74
|
-
|
|
111
|
+
const {join} = require('path');
|
|
112
|
+
const path = require('path');
|
|
75
113
|
|
|
76
114
|
const file1 = join(__dirname, '../../package.json');
|
|
77
115
|
const file2 = path.join(__dirname, '../../package.json');
|
|
@@ -80,8 +118,6 @@ const file2 = path.join(__dirname, '../../package.json');
|
|
|
80
118
|
#### β
Example of correct code
|
|
81
119
|
|
|
82
120
|
```js
|
|
83
|
-
import {readFile} from 'fs/promises';
|
|
84
|
-
|
|
85
121
|
const file1 = new URL('../../package.json', import.meta.url);
|
|
86
122
|
const file2 = new URL('../../package.json', import.meta.url);
|
|
87
123
|
```
|
|
@@ -101,6 +137,7 @@ const file = new URL('../../package.json', import.meta.url);
|
|
|
101
137
|
|
|
102
138
|
```js
|
|
103
139
|
const {readFile} = require('fs/promises');
|
|
140
|
+
const {join} = require('path');
|
|
104
141
|
const file = join(__dirname, '../../package.json');
|
|
105
142
|
```
|
|
106
143
|
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const {operator} = require('putout');
|
|
4
|
+
|
|
5
|
+
const isNumber = (a) => typeof a === 'number';
|
|
6
|
+
|
|
7
|
+
const {compute} = operator;
|
|
8
|
+
|
|
9
|
+
module.exports.report = () => `Use 'Buffer.alloc()' or 'Buffer.from()' instead of 'Buffer()' and 'new Buffer()'`;
|
|
10
|
+
|
|
11
|
+
module.exports.match = () => ({
|
|
12
|
+
'new Buffer(__a)': (vars, path) => {
|
|
13
|
+
const __aPath = path.get('arguments.0');
|
|
14
|
+
const [is] = compute(__aPath);
|
|
15
|
+
|
|
16
|
+
return is;
|
|
17
|
+
},
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
module.exports.replace = () => ({
|
|
21
|
+
'new Buffer(__a)': transform,
|
|
22
|
+
'Buffer(__a)': transform,
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
function transform(vars, path) {
|
|
26
|
+
const [, value] = compute(path.get('arguments.0'));
|
|
27
|
+
|
|
28
|
+
if (isNumber(value))
|
|
29
|
+
return 'Buffer.alloc(__a)';
|
|
30
|
+
|
|
31
|
+
return 'Buffer.from(__a)';
|
|
32
|
+
}
|
|
33
|
+
|
|
@@ -55,7 +55,10 @@ module.exports.traverse = ({push, listStore}) => ({
|
|
|
55
55
|
if (path.node.__putoutNodeDeclareAfterRequire)
|
|
56
56
|
continue;
|
|
57
57
|
|
|
58
|
-
if (path.isVariableDeclaration()
|
|
58
|
+
if (!path.isVariableDeclaration())
|
|
59
|
+
continue;
|
|
60
|
+
|
|
61
|
+
if (path.node.declarations[0].id.name === 'require')
|
|
59
62
|
continue;
|
|
60
63
|
|
|
61
64
|
if (compareAny(path, REQUIRE_LIST)) {
|
package/lib/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/plugin-nodejs",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.8.1",
|
|
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,14 +31,14 @@
|
|
|
31
31
|
"nodejs"
|
|
32
32
|
],
|
|
33
33
|
"devDependencies": {
|
|
34
|
-
"@putout/plugin-convert-commonjs-to-esm": "
|
|
34
|
+
"@putout/plugin-convert-commonjs-to-esm": "*",
|
|
35
35
|
"@putout/plugin-convert-esm-to-commonjs": "*",
|
|
36
36
|
"@putout/plugin-putout": "*",
|
|
37
37
|
"@putout/test": "^5.0.0",
|
|
38
38
|
"c8": "^7.5.0",
|
|
39
39
|
"eslint": "^8.0.1",
|
|
40
|
-
"eslint-plugin-
|
|
41
|
-
"eslint-plugin-putout": "^
|
|
40
|
+
"eslint-plugin-n": "^15.2.4",
|
|
41
|
+
"eslint-plugin-putout": "^16.0.0",
|
|
42
42
|
"lerna": "^5.0.0",
|
|
43
43
|
"madrun": "^9.0.0",
|
|
44
44
|
"montag": "^1.2.1",
|