@putout/plugin-nodejs 11.10.0 → 12.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
|
@@ -17,28 +17,30 @@ npm i putout @putout/plugin-nodejs -D
|
|
|
17
17
|
|
|
18
18
|
## Rules
|
|
19
19
|
|
|
20
|
-
- ✅ [add-node-prefix]
|
|
21
|
-
- ✅ [
|
|
22
|
-
- ✅ [convert-
|
|
23
|
-
- ✅ [convert-commonjs-to-esm-
|
|
24
|
-
- ✅ [convert-commonjs-to-esm-
|
|
25
|
-
- ✅ [convert-commonjs-to-esm
|
|
26
|
-
- ✅ [convert-
|
|
27
|
-
- ✅ [convert-
|
|
28
|
-
- ✅ [convert-
|
|
29
|
-
- ✅ [convert-
|
|
30
|
-
- ✅ [convert-
|
|
31
|
-
- ✅ [convert-
|
|
32
|
-
- ✅ [convert-
|
|
33
|
-
- ✅ [
|
|
34
|
-
- ✅ [declare
|
|
35
|
-
- ✅ [
|
|
36
|
-
- ✅ [remove-
|
|
37
|
-
- ✅ [
|
|
38
|
-
- ✅ [rename-file-
|
|
39
|
-
- ✅ [
|
|
40
|
-
- ✅ [
|
|
41
|
-
- ✅ [
|
|
20
|
+
- ✅ [add-node-prefix](#add-node-prefix);
|
|
21
|
+
- ✅ [add-missing-strict-mode](#add-missing-strict-mode);
|
|
22
|
+
- ✅ [convert-buffer-to-buffer-alloc](#convert-buffer-to-buffer-alloc);
|
|
23
|
+
- ✅ [convert-commonjs-to-esm-commons](#convert-commonjs-to-esm-commons);
|
|
24
|
+
- ✅ [convert-commonjs-to-esm-exports](#convert-commonjs-to-esm-exports);
|
|
25
|
+
- ✅ [convert-commonjs-to-esm-require](#convert-commonjs-to-esm-require);
|
|
26
|
+
- ✅ [convert-commonjs-to-esm.js](#convert-commonjs-to-esm.js);
|
|
27
|
+
- ✅ [convert-dirname-to-url](#convert-dirname-to-url);
|
|
28
|
+
- ✅ [convert-esm-to-commonjs](#convert-esm-to-commonjs);
|
|
29
|
+
- ✅ [convert-exports-to-module-exports](#convert-exports-to-module-exports);
|
|
30
|
+
- ✅ [convert-fs-promises](#convert-fs-promises);
|
|
31
|
+
- ✅ [convert-promisify-to-fs-promises](#convert-promisify-to-fs-promises);
|
|
32
|
+
- ✅ [convert-top-level-return](#convert-top-level-return);
|
|
33
|
+
- ✅ [convert-url-to-dirname](#convert-url-to-dirname);
|
|
34
|
+
- ✅ [declare](#declare);
|
|
35
|
+
- ✅ [declare-after-require](#declare-after-require);
|
|
36
|
+
- ✅ [remove-process-exit](#remove-process-exit);
|
|
37
|
+
- ✅ [remove-useless-promisify](#remove-useless-promisify);
|
|
38
|
+
- ✅ [rename-file-cjs-to-js](#rename-file-cjs-to-js);
|
|
39
|
+
- ✅ [rename-file-mjs-to-js](#rename-file-mjs-to-js);
|
|
40
|
+
- ✅ [remove-useless-strict-mode](#remove-useless-strict-mode);
|
|
41
|
+
- ✅ [remove-illigal-strict-mode](#remove-useless-strict-mode);
|
|
42
|
+
- ✅ [cjs-file](#cjs-file);
|
|
43
|
+
- ✅ [mjs-file](#mjs-file);
|
|
42
44
|
|
|
43
45
|
## Config
|
|
44
46
|
|
|
@@ -485,6 +487,53 @@ import a from 'b';
|
|
|
485
487
|
import a from 'b';
|
|
486
488
|
```
|
|
487
489
|
|
|
490
|
+
## remove-illigal-strict-mode
|
|
491
|
+
|
|
492
|
+
> `SyntaxError: "use strict" not allowed in function with non-simple parameters`
|
|
493
|
+
> The JavaScript exception `"use strict" not allowed in function` occurs when a `use strict` directive is used at the top of a function with default parameters, rest parameters, or destructuring parameters.
|
|
494
|
+
>
|
|
495
|
+
> (c) [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Strict_non_simple_params)
|
|
496
|
+
|
|
497
|
+
Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/a2ff9020bd33d11f3927d4d68c830c50/e6732329a0cca03c09ec0d36b43bfaec17966eff).
|
|
498
|
+
|
|
499
|
+
### ❌ Example of incorrect code
|
|
500
|
+
|
|
501
|
+
```ts
|
|
502
|
+
function x1(...a) {
|
|
503
|
+
'use strict';
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
function x2(a, b = 3) {
|
|
507
|
+
'use strict';
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
function x3({a}) {
|
|
511
|
+
'use strict';
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
function x4([a]) {
|
|
515
|
+
'use strict';
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
function x5(...a) {
|
|
519
|
+
'use strict';
|
|
520
|
+
}
|
|
521
|
+
```
|
|
522
|
+
|
|
523
|
+
### ✅ Example of correct code
|
|
524
|
+
|
|
525
|
+
```js
|
|
526
|
+
function x1(...a) {}
|
|
527
|
+
|
|
528
|
+
function x2(a, b = 3) {}
|
|
529
|
+
|
|
530
|
+
function x3({a}) {}
|
|
531
|
+
|
|
532
|
+
function x4([a]) {}
|
|
533
|
+
|
|
534
|
+
function x5(...a) {}
|
|
535
|
+
```
|
|
536
|
+
|
|
488
537
|
## remove-useless-promisify
|
|
489
538
|
|
|
490
539
|
> Takes a function following the common error-first callback style, i.e. taking an (err, value) => ... callback as the last argument, and returns a version that returns promises.
|
|
@@ -87,7 +87,11 @@ module.exports.replace = () => ({
|
|
|
87
87
|
return applyDynamicImport(path);
|
|
88
88
|
}
|
|
89
89
|
|
|
90
|
-
// disabled while not supported
|
|
90
|
+
// disabled while not supported
|
|
91
|
+
// https://babeljs.io/blog/2023/05/26/7.22.0#import-attributes-15536-15620
|
|
92
|
+
//
|
|
93
|
+
// const isJSON = /\.json$/.test(value);
|
|
94
|
+
// const assertion = !isJSON ? '' : 'with { type: "json" }';
|
|
91
95
|
const assertion = '';
|
|
92
96
|
|
|
93
97
|
if (isObjectPattern(__a)) {
|
|
@@ -1,15 +1,22 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const {types
|
|
3
|
+
const {types} = require('putout');
|
|
4
4
|
const {isImportDefaultSpecifier} = types;
|
|
5
5
|
|
|
6
|
+
const BODY = '{__body}';
|
|
7
|
+
const FN = `function __a(__args) ${BODY}`;
|
|
8
|
+
const GEN_FN = `function* __a(__args) ${BODY}`;
|
|
9
|
+
const ASYNC_FN = `async function __a(__args) ${BODY}`;
|
|
10
|
+
const CLASS = `class __a ${BODY}`;
|
|
11
|
+
|
|
6
12
|
module.exports.report = () => `Use 'CommonJS' instead of 'ESM'`;
|
|
7
13
|
|
|
8
14
|
module.exports.replace = () => ({
|
|
9
15
|
'export default __a': 'module.exports = __a',
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
16
|
+
[`export ${CLASS}`]: `module.exports.__a = ${CLASS}`,
|
|
17
|
+
[`export ${FN}`]: `module.epxorts.__a = ${FN}`,
|
|
18
|
+
[`export ${ASYNC_FN}`]: `module.epxorts.__a = ${ASYNC_FN}`,
|
|
19
|
+
[`export ${GEN_FN}`]: `module.epxorts.__a = ${GEN_FN}`,
|
|
13
20
|
'export const __a = __b': 'module.exports.__a = __b',
|
|
14
21
|
'export {__exports}': ({__exports}) => {
|
|
15
22
|
let result = 'module.exports = {\n';
|
|
@@ -56,13 +63,3 @@ module.exports.replace = () => ({
|
|
|
56
63
|
}`;
|
|
57
64
|
},
|
|
58
65
|
});
|
|
59
|
-
|
|
60
|
-
function replaceFn({__a}, path) {
|
|
61
|
-
const {name} = __a;
|
|
62
|
-
const {declaration} = path.node;
|
|
63
|
-
const node = template.ast.fresh(`module.exports.${name} = __x`);
|
|
64
|
-
|
|
65
|
-
node.right = declaration;
|
|
66
|
-
|
|
67
|
-
return node;
|
|
68
|
-
}
|
package/lib/index.js
CHANGED
|
@@ -53,5 +53,6 @@ module.exports.rules = {
|
|
|
53
53
|
|
|
54
54
|
'add-missing-strict-mode': strictMode.rules['add-missing'],
|
|
55
55
|
'remove-useless-strict-mode': strictMode.rules['remove-useless'],
|
|
56
|
+
'remove-illigal-strict-mode': strictMode.rules['remove-illigal'],
|
|
56
57
|
'remove-useless-promisify': removeUselessPromisify,
|
|
57
58
|
};
|
package/lib/strict-mode/index.js
CHANGED
|
@@ -2,8 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
const addMissing = require('./add-missing');
|
|
4
4
|
const removeUseless = require('./remove-useless');
|
|
5
|
+
const removeIlligal = require('./remove-illigal');
|
|
5
6
|
|
|
6
7
|
module.exports.rules = {
|
|
7
8
|
'add-missing': addMissing,
|
|
8
9
|
'remove-useless': removeUseless,
|
|
10
|
+
'remove-illigal': removeIlligal,
|
|
9
11
|
};
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const {operator, types} = require('putout');
|
|
4
|
+
const {
|
|
5
|
+
isArrayPattern,
|
|
6
|
+
isObjectPattern,
|
|
7
|
+
isAssignmentPattern,
|
|
8
|
+
isRestElement,
|
|
9
|
+
isFunction,
|
|
10
|
+
} = types;
|
|
11
|
+
|
|
12
|
+
const {remove} = operator;
|
|
13
|
+
|
|
14
|
+
module.exports.report = () => `Avoid illigal 'use strict'`;
|
|
15
|
+
|
|
16
|
+
module.exports.fix = (path) => {
|
|
17
|
+
remove(path);
|
|
18
|
+
};
|
|
19
|
+
module.exports.traverse = ({push}) => ({
|
|
20
|
+
DirectiveLiteral(path) {
|
|
21
|
+
const fnPath = path.parentPath.parentPath.parentPath;
|
|
22
|
+
|
|
23
|
+
if (!isFunction(fnPath))
|
|
24
|
+
return;
|
|
25
|
+
|
|
26
|
+
if (isIlligal(fnPath))
|
|
27
|
+
push(path.parentPath);
|
|
28
|
+
},
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
function isIlligal(fnPath) {
|
|
32
|
+
const params = fnPath.get('params');
|
|
33
|
+
|
|
34
|
+
for (const param of params) {
|
|
35
|
+
if (isRestElement(param))
|
|
36
|
+
return true;
|
|
37
|
+
|
|
38
|
+
if (isAssignmentPattern(param))
|
|
39
|
+
return true;
|
|
40
|
+
|
|
41
|
+
if (isObjectPattern(param))
|
|
42
|
+
return true;
|
|
43
|
+
|
|
44
|
+
if (isArrayPattern(param))
|
|
45
|
+
return true;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return false;
|
|
49
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/plugin-nodejs",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "12.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",
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
"changelog": false,
|
|
18
18
|
"repository": {
|
|
19
19
|
"type": "git",
|
|
20
|
-
"url": "git://github.com/coderaiser/putout.git"
|
|
20
|
+
"url": "git+https://github.com/coderaiser/putout.git"
|
|
21
21
|
},
|
|
22
22
|
"scripts": {
|
|
23
23
|
"test": "madrun test",
|
|
@@ -43,18 +43,18 @@
|
|
|
43
43
|
"@putout/plugin-declare": "*",
|
|
44
44
|
"@putout/plugin-putout": "*",
|
|
45
45
|
"@putout/plugin-typescript": "*",
|
|
46
|
-
"@putout/test": "^
|
|
47
|
-
"c8": "^
|
|
46
|
+
"@putout/test": "^11.0.0",
|
|
47
|
+
"c8": "^10.0.0",
|
|
48
48
|
"eslint": "^9.0.0",
|
|
49
49
|
"eslint-plugin-n": "^17.0.0",
|
|
50
|
-
"eslint-plugin-putout": "^
|
|
50
|
+
"eslint-plugin-putout": "^23.0.0",
|
|
51
51
|
"lerna": "^6.0.1",
|
|
52
52
|
"madrun": "^10.0.0",
|
|
53
53
|
"montag": "^1.2.1",
|
|
54
54
|
"nodemon": "^3.0.1"
|
|
55
55
|
},
|
|
56
56
|
"peerDependencies": {
|
|
57
|
-
"putout": ">=
|
|
57
|
+
"putout": ">=36"
|
|
58
58
|
},
|
|
59
59
|
"license": "MIT",
|
|
60
60
|
"engines": {
|