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