@putout/plugin-nodejs 4.7.1 β†’ 5.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
@@ -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
- import {readFile} from 'fs/promises';
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
+
@@ -36,22 +36,19 @@ module.exports.fix = ({path, firstRequire, lastRequire}) => {
36
36
  lastRequire.insertAfter(node);
37
37
  };
38
38
 
39
- module.exports.traverse = ({push, listStore}) => ({
39
+ module.exports.traverse = ({push, pathStore}) => ({
40
40
  'const __a = __b': (path) => {
41
41
  if (!path.parentPath.isProgram())
42
42
  return;
43
43
 
44
- listStore(path);
44
+ pathStore(path);
45
45
  },
46
46
  'Program': {
47
47
  exit() {
48
48
  const requirePaths = [];
49
49
  const constPaths = [];
50
50
 
51
- for (const path of listStore()) {
52
- if (!path.node)
53
- continue;
54
-
51
+ for (const path of pathStore()) {
55
52
  if (path.node.__putoutNodeDeclareAfterRequire)
56
53
  continue;
57
54
 
package/lib/index.js CHANGED
@@ -5,6 +5,7 @@ const getRule = (a) => ({
5
5
  });
6
6
 
7
7
  module.exports.rules = {
8
+ ...getRule('convert-buffer-to-buffer-alloc'),
8
9
  ...getRule('convert-fs-promises'),
9
10
  ...getRule('convert-promisify-to-fs-promises'),
10
11
  ...getRule('convert-dirname-to-url'),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/plugin-nodejs",
3
- "version": "4.7.1",
3
+ "version": "5.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",
@@ -37,15 +37,15 @@
37
37
  "@putout/test": "^5.0.0",
38
38
  "c8": "^7.5.0",
39
39
  "eslint": "^8.0.1",
40
- "eslint-plugin-node": "^11.0.0",
41
- "eslint-plugin-putout": "^15.0.0",
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",
45
45
  "nodemon": "^2.0.1"
46
46
  },
47
47
  "peerDependencies": {
48
- "putout": ">=25"
48
+ "putout": ">=27"
49
49
  },
50
50
  "license": "MIT",
51
51
  "engines": {