@putout/plugin-nodejs 9.2.0 β†’ 9.3.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
@@ -22,6 +22,9 @@ npm i putout @putout/plugin-nodejs -D
22
22
  "rules": {
23
23
  "nodejs/convert-commonjs-to-esm": "off",
24
24
  "nodejs/convert-esm-to-commonjs": "off",
25
+ "nodejs/cjs-file": "off",
26
+ "nodejs/mjs-file": "off",
27
+ "nodejs/rename-file-cjs-to-js": "off",
25
28
  "nodejs/add-node-prefix": "on",
26
29
  "nodejs/convert-buffer-to-buffer-alloc": "on",
27
30
  "nodejs/convert-fs-promises": "on",
@@ -32,15 +35,12 @@ npm i putout @putout/plugin-nodejs -D
32
35
  "nodejs/convert-top-level-return": "on",
33
36
  "nodejs/declare": "on",
34
37
  "nodejs/declare-after-require": "on",
35
- "nodejs/remove-process-exit": "on",
36
- "nodejs/cjs-file": "off"
38
+ "nodejs/remove-process-exit": "on"
37
39
  }
38
40
  }
39
41
  ```
40
42
 
41
- ## Rules
42
-
43
- ### add-node-prefix
43
+ ## add-node-prefix
44
44
 
45
45
  > `Deno` supports using Node.js built-in modules such as `fs`, `path`, `process`, and many more via `node`: specifiers.
46
46
  >
@@ -48,19 +48,19 @@ npm i putout @putout/plugin-nodejs -D
48
48
 
49
49
  Check out in 🐊[Putout Editor](https://putout.cloudcmd.io/#/gist/534093e0bf0a4407796c08d62bcbcb92/766a1d608f155920b21aa1f53a8e33280a664309).
50
50
 
51
- #### ❌ Example of incorrect code
51
+ ### ❌ Example of incorrect code
52
52
 
53
53
  ```js
54
54
  import fs from 'fs';
55
55
  ```
56
56
 
57
- #### βœ… Example of correct code
57
+ ### βœ… Example of correct code
58
58
 
59
59
  ```js
60
60
  import fs from 'node:fs';
61
61
  ```
62
62
 
63
- ### convert-buffer-to-buffer-alloc
63
+ ## convert-buffer-to-buffer-alloc
64
64
 
65
65
  > The `Buffer()` function and `new Buffer()` constructor are **deprecated** due to API usability issues that can lead to accidental security issues.
66
66
  >
@@ -68,7 +68,7 @@ import fs from 'node:fs';
68
68
 
69
69
  Check out in 🐊[Putout Editor](https://putout.cloudcmd.io/#/gist/5379bcdfa3d76f7b7121c9671ae48375/2fc2c7f96fc8284788c00914a9b29bfeea8b13d4).
70
70
 
71
- #### ❌ Example of incorrect code
71
+ ### ❌ Example of incorrect code
72
72
 
73
73
  ```js
74
74
  const n = 100;
@@ -82,7 +82,7 @@ new Buffer([]);
82
82
  new Buffer(buf);
83
83
  ```
84
84
 
85
- #### βœ… Example of correct code
85
+ ### βœ… Example of correct code
86
86
 
87
87
  ```js
88
88
  const n = 100;
@@ -96,42 +96,42 @@ Buffer.from([]);
96
96
  Buffer.from(buf);
97
97
  ```
98
98
 
99
- ### convert-fs-promises
99
+ ## convert-fs-promises
100
100
 
101
101
  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**.
102
102
 
103
- #### ❌ Example of incorrect code
103
+ ### ❌ Example of incorrect code
104
104
 
105
105
  ```js
106
106
  const {readFile} = require('fs').promises;
107
107
  ```
108
108
 
109
- #### βœ… Example of correct code
109
+ ### βœ… Example of correct code
110
110
 
111
111
  ```js
112
112
  const {readFile} = require('fs/promises');
113
113
  ```
114
114
 
115
- ### convert-promisify-to-fs-promises
115
+ ## convert-promisify-to-fs-promises
116
116
 
117
- #### ❌ Example of incorrect code
117
+ ### ❌ Example of incorrect code
118
118
 
119
119
  ```js
120
120
  const fs = require('fs');
121
121
  const readFile = promisify(fs.readFile);
122
122
  ```
123
123
 
124
- #### βœ… Example of correct code
124
+ ### βœ… Example of correct code
125
125
 
126
126
  ```js
127
127
  const {readFile} = require('fs/promises');
128
128
  ```
129
129
 
130
- ### convert-dirname-to-url
130
+ ## convert-dirname-to-url
131
131
 
132
132
  Only for **ESM**.
133
133
 
134
- #### ❌ Example of incorrect code
134
+ ### ❌ Example of incorrect code
135
135
 
136
136
  ```js
137
137
  const {join} = require('path');
@@ -141,25 +141,25 @@ const file1 = join(__dirname, '../../package.json');
141
141
  const file2 = path.join(__dirname, '../../package.json');
142
142
  ```
143
143
 
144
- #### βœ… Example of correct code
144
+ ### βœ… Example of correct code
145
145
 
146
146
  ```js
147
147
  const file1 = new URL('../../package.json', import.meta.url);
148
148
  const file2 = new URL('../../package.json', import.meta.url);
149
149
  ```
150
150
 
151
- ### convert-url-to-dirname
151
+ ## convert-url-to-dirname
152
152
 
153
153
  Only for **CommonJS**.
154
154
 
155
- #### ❌ Example of incorrect code
155
+ ### ❌ Example of incorrect code
156
156
 
157
157
  ```js
158
158
  const {readFile} = require('fs/promises');
159
159
  const file = new URL('../../package.json', import.meta.url);
160
160
  ```
161
161
 
162
- #### βœ… Example of correct code
162
+ ### βœ… Example of correct code
163
163
 
164
164
  ```js
165
165
  const {readFile} = require('fs/promises');
@@ -167,7 +167,7 @@ const {join} = require('path');
167
167
  const file = join(__dirname, '../../package.json');
168
168
  ```
169
169
 
170
- ### remove-process-exit
170
+ ## remove-process-exit
171
171
 
172
172
  In most cases `process.exit()` is called from `bin` directory, if not - disable this rule using `match`.
173
173
 
@@ -175,38 +175,38 @@ In most cases `process.exit()` is called from `bin` directory, if not - disable
175
175
  -process.exit();
176
176
  ```
177
177
 
178
- ### convert-exports-to-module-exports
178
+ ## convert-exports-to-module-exports
179
179
 
180
180
  Since `exports = 5` wan't make any export, just change value of variable.
181
181
  Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/8b2af2c4ad005ed1c77cde41377caaad/dfdccc794037d7f67bde1e7d7244bf5f14abebce).
182
182
 
183
- #### ❌ Example of incorrect code
183
+ ### ❌ Example of incorrect code
184
184
 
185
185
  ```js
186
186
  exports.x = 5;
187
187
  ```
188
188
 
189
- #### βœ… Example of correct code
189
+ ### βœ… Example of correct code
190
190
 
191
191
  ```js
192
192
  module.exports.x = 5;
193
193
  ```
194
194
 
195
- ### convert-top-level-return
195
+ ## convert-top-level-return
196
196
 
197
- #### ❌ Example of incorrect code
197
+ ### ❌ Example of incorrect code
198
198
 
199
199
  ```js
200
200
  return;
201
201
  ```
202
202
 
203
- #### βœ… Example of correct code
203
+ ### βœ… Example of correct code
204
204
 
205
205
  ```js
206
206
  process.exit();
207
207
  ```
208
208
 
209
- ### declare
209
+ ## declare
210
210
 
211
211
  Add declarations to built-in node.js modules:
212
212
 
@@ -224,13 +224,13 @@ Add declarations to built-in node.js modules:
224
224
 
225
225
  Based on [`@putout/operator-declare`](https://github.com/coderaiser/putout/tree/master/packages/operator-declare#putoutoperator-declare-).
226
226
 
227
- #### ❌ Example of incorrect code
227
+ ### ❌ Example of incorrect code
228
228
 
229
229
  ```js
230
230
  await readFile('hello.txt', 'utf8');
231
231
  ```
232
232
 
233
- #### βœ… Example of correct code
233
+ ### βœ… Example of correct code
234
234
 
235
235
  ```js
236
236
  import {readFile} from 'fs/promises';
@@ -250,7 +250,7 @@ When you want to skip some declaration use `dismiss`:
250
250
  }
251
251
  ```
252
252
 
253
- ### declare-after-require
253
+ ## declare-after-require
254
254
 
255
255
  > **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.
256
256
  >
@@ -258,21 +258,21 @@ When you want to skip some declaration use `dismiss`:
258
258
 
259
259
  Check out in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/https://putout.cloudcmd.io/#/gist/ddf5731ae829beec4d3018d4d9ac2150/342738b63337bfa9b4fc08c5b301483ea2b5ba9c).
260
260
 
261
- #### ❌ Example of incorrect code
261
+ ### ❌ Example of incorrect code
262
262
 
263
263
  ```js
264
264
  const name = 'hello.txt';
265
265
  const {readFile} = require('fs/promises');
266
266
  ```
267
267
 
268
- #### βœ… Example of correct code
268
+ ### βœ… Example of correct code
269
269
 
270
270
  ```js
271
271
  const {readFile} = require('fs/promises');
272
272
  const name = 'hello.txt';
273
273
  ```
274
274
 
275
- ### convert-commonjs-to-esm
275
+ ## convert-commonjs-to-esm
276
276
 
277
277
  Convert **CommonJS** **EcmaScript Modules**.
278
278
 
@@ -280,9 +280,9 @@ Convert **CommonJS** **EcmaScript Modules**.
280
280
  >
281
281
  > (c) [parceljs](https://parceljs.org/languages/javascript/)
282
282
 
283
- #### require
283
+ ### require
284
284
 
285
- ##### ❌ Example of incorrect code
285
+ ### ❌ Example of incorrect code
286
286
 
287
287
  ```js
288
288
  const {join} = require('path');
@@ -292,7 +292,7 @@ const args = require('minimist')({
292
292
  });
293
293
  ```
294
294
 
295
- ##### βœ… Example of correct code
295
+ ### βœ… Example of correct code
296
296
 
297
297
  ```js
298
298
  import {join} from 'path';
@@ -303,23 +303,23 @@ const args = minimist({
303
303
  });
304
304
  ```
305
305
 
306
- #### exports
306
+ ### exports
307
307
 
308
- ##### ❌ Example of incorrect code
308
+ ### ❌ Example of incorrect code
309
309
 
310
310
  ```js
311
311
  module.exports = () => {};
312
312
  ```
313
313
 
314
- ##### βœ… Example of correct code
314
+ ### βœ… Example of correct code
315
315
 
316
316
  ```js
317
317
  export default () => {};
318
318
  ```
319
319
 
320
- #### Commons
320
+ ### Commons
321
321
 
322
- ##### ❌ Example of incorrect code
322
+ ### ❌ Example of incorrect code
323
323
 
324
324
  ```js
325
325
  const {readFile} = require('fs/promises');
@@ -327,7 +327,7 @@ const {readFile} = require('fs/promises');
327
327
  await readFile(__filename);
328
328
  ```
329
329
 
330
- ##### βœ… Example of correct code
330
+ ### βœ… Example of correct code
331
331
 
332
332
  ```js
333
333
  import {readFile} from 'fs/promises';
@@ -337,7 +337,7 @@ const __filename = fileURLToPath(import.meta.url);
337
337
  await readFile(__filename);
338
338
  ```
339
339
 
340
- ### convert-esm-to-commonjs
340
+ ## convert-esm-to-commonjs
341
341
 
342
342
  > **CommonJS** is a module system supported in Node, it provides a `require` function, which can be used to access the `exports` object exposed by another file.
343
343
  >
@@ -345,30 +345,58 @@ await readFile(__filename);
345
345
 
346
346
  Convert **EcmaScript Modules** to **CommonJS**.
347
347
 
348
- ## ❌ Example of incorrect code
348
+ ### ❌ Example of incorrect code
349
349
 
350
350
  ```js
351
351
  import hello from 'world';
352
352
  ```
353
353
 
354
- ## βœ… Example of correct code
354
+ ### βœ… Example of correct code
355
355
 
356
356
  ```js
357
357
  const hello = require('world');
358
358
  ```
359
359
 
360
- ### cjs-file
360
+ ## cjs-file
361
361
 
362
362
  Run [convert-esm-to-commonjs](#convert-esm-to-commonjs) for all `*.cjs` files with help of [redlint](https://github.com/putoutjs/redlint).
363
363
 
364
364
  Check out in 🐊[Putout Editor](https://putout.cloudcmd.io/#/gist/779e7fb720af59afc2d3da082088fd4c/d0b85b07c6aaf2b902a1c7eb7ae121dbcd181033).
365
365
 
366
- ### mjs-file
366
+ ## mjs-file
367
367
 
368
368
  Run [convert-commonjs-to-esm](#convert-commonjs-to-esm) for all `*.cjs` files with help of [redlint](https://github.com/putoutjs/redlint).
369
369
 
370
370
  Check out in 🐊[Putout Editor](https://putout.cloudcmd.io/#/gist/779e7fb720af59afc2d3da082088fd4c/d0b85b07c6aaf2b902a1c7eb7ae121dbcd181033).
371
371
 
372
+ ## rename-file-cjs-to-js
373
+
374
+ Rename `*.cjs` files when `module !== "module"`:
375
+
376
+ ```diff
377
+ /
378
+ |-- package.json
379
+ `-- lib/
380
+ - `-- hello.cjs
381
+ + `-- hello.js
382
+ ```
383
+
384
+ Check out in 🐊[Putout Editor](https://putout.cloudcmd.io/#/gist/8d8f3cd6662b70abbd5e4a2e4835077f/e43319fd63291ec3a5028b30a83f3c91fe90325e).
385
+
386
+ ## rename-file-mjs-to-js
387
+
388
+ Rename `*.mjs` files when `module === "module"`:
389
+
390
+ ```diff
391
+ /
392
+ |-- package.json
393
+ `-- lib/
394
+ - `-- hello.mjs
395
+ + `-- hello.js
396
+ ```
397
+
398
+ Check out in 🐊[Putout Editor](https://putout.cloudcmd.io/#/gist/94fb3298b210e703b01db9a6826942bc/dfe2462451c6b3d4d47da7fd8d39dc8e53bb16eb).
399
+
372
400
  ## License
373
401
 
374
402
  MIT
package/lib/index.js CHANGED
@@ -21,6 +21,9 @@ const convertCommonjsToEsmRequire = require('./convert-commonjs-to-esm-require')
21
21
  const cjsFile = require('./cjs-file');
22
22
  const mjsFile = require('./mjs-file');
23
23
 
24
+ const renameFileCjsToJs = require('./rename-file-cjs-to-js');
25
+ const renameFileMjsToJs = require('./rename-file-mjs-to-js');
26
+
24
27
  module.exports.rules = {
25
28
  'convert-buffer-to-buffer-alloc': convertBufferToBufferAlloc,
26
29
  'convert-fs-promises': convertFsPromises,
@@ -40,4 +43,6 @@ module.exports.rules = {
40
43
  'convert-commonjs-to-esm-require': ['off', convertCommonjsToEsmRequire],
41
44
  'cjs-file': ['off', cjsFile],
42
45
  'mjs-file': ['off', mjsFile],
46
+ 'rename-file-cjs-to-js': ['off', renameFileCjsToJs],
47
+ 'rename-file-mjs-to-js': renameFileMjsToJs,
43
48
  };
@@ -0,0 +1,68 @@
1
+ 'use strict';
2
+
3
+ const {operator} = require('putout');
4
+ const {join} = require('path');
5
+ const {parse} = JSON;
6
+
7
+ const {
8
+ getParentDirectory,
9
+ getFilename,
10
+ readFileContent,
11
+ findFile,
12
+ __filesystem,
13
+ renameFile,
14
+ } = operator;
15
+
16
+ module.exports.report = ({cjs, js}) => `Rename '${cjs}' to '${js}'`;
17
+
18
+ module.exports.fix = ({path, js}) => {
19
+ renameFile(path, js);
20
+ };
21
+
22
+ module.exports.traverse = ({push}) => ({
23
+ [__filesystem]: (path) => {
24
+ for (const file of findFile(path, '*.cjs')) {
25
+ const packagePath = findUpPackage(file);
26
+
27
+ const cjs = getFilename(file);
28
+ const js = cjs.replace(/cjs$/, 'js');
29
+
30
+ if (!packagePath) {
31
+ push({
32
+ path: file,
33
+ cjs,
34
+ js,
35
+ });
36
+ continue;
37
+ }
38
+
39
+ const packageContent = readFileContent(packagePath);
40
+
41
+ if (!packageContent)
42
+ continue;
43
+
44
+ const {type} = parse(packageContent);
45
+
46
+ if (type === 'module')
47
+ continue;
48
+
49
+ push({
50
+ path: file,
51
+ cjs,
52
+ js,
53
+ });
54
+ }
55
+ },
56
+ });
57
+
58
+ function findUpPackage(file) {
59
+ let packageJSON;
60
+ let dirPath = getParentDirectory(file);
61
+
62
+ do {
63
+ const dir = getFilename(dirPath);
64
+ [packageJSON] = findFile(dirPath, join(dir, 'package.json'));
65
+ } while (!packageJSON && (dirPath = getParentDirectory(dirPath)));
66
+
67
+ return packageJSON;
68
+ }
@@ -0,0 +1,62 @@
1
+ 'use strict';
2
+
3
+ const {operator} = require('putout');
4
+ const {join} = require('path');
5
+
6
+ const {parse} = JSON;
7
+ const {
8
+ getParentDirectory,
9
+ getFilename,
10
+ readFileContent,
11
+ findFile,
12
+ __filesystem,
13
+ renameFile,
14
+ } = operator;
15
+
16
+ module.exports.report = ({mjs, js}) => `Rename '${mjs}' to '${js}'`;
17
+
18
+ module.exports.fix = ({path, js}) => {
19
+ renameFile(path, js);
20
+ };
21
+
22
+ module.exports.traverse = ({push}) => ({
23
+ [__filesystem]: (path) => {
24
+ for (const file of findFile(path, '*.mjs')) {
25
+ const packagePath = findUpPackage(file);
26
+
27
+ if (!packagePath)
28
+ continue;
29
+
30
+ const packageContent = readFileContent(packagePath);
31
+
32
+ if (!packageContent)
33
+ continue;
34
+
35
+ const {type} = parse(packageContent);
36
+
37
+ if (type !== 'module')
38
+ continue;
39
+
40
+ const mjs = getFilename(file);
41
+ const js = mjs.replace(/mjs$/, 'js');
42
+
43
+ push({
44
+ path: file,
45
+ mjs,
46
+ js,
47
+ });
48
+ }
49
+ },
50
+ });
51
+
52
+ function findUpPackage(file) {
53
+ let packageJSON;
54
+ let dirPath = getParentDirectory(file);
55
+
56
+ do {
57
+ const dir = getFilename(dirPath);
58
+ [packageJSON] = findFile(dirPath, join(dir, 'package.json'));
59
+ } while (!packageJSON && (dirPath = getParentDirectory(dirPath)));
60
+
61
+ return packageJSON;
62
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/plugin-nodejs",
3
- "version": "9.2.0",
3
+ "version": "9.3.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",