@putout/plugin-nodejs 9.4.0 β 10.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
|
@@ -35,7 +35,8 @@ npm i putout @putout/plugin-nodejs -D
|
|
|
35
35
|
"nodejs/convert-top-level-return": "on",
|
|
36
36
|
"nodejs/declare": "on",
|
|
37
37
|
"nodejs/declare-after-require": "on",
|
|
38
|
-
"nodejs/remove-process-exit": "on"
|
|
38
|
+
"nodejs/remove-process-exit": "on",
|
|
39
|
+
"nodejs/strict-mode": "on"
|
|
39
40
|
}
|
|
40
41
|
}
|
|
41
42
|
```
|
|
@@ -397,6 +398,50 @@ Rename `*.mjs` files when `module === "module"`:
|
|
|
397
398
|
|
|
398
399
|
Check out in π[Putout Editor](https://putout.cloudcmd.io/#/gist/94fb3298b210e703b01db9a6826942bc/dfe2462451c6b3d4d47da7fd8d39dc8e53bb16eb).
|
|
399
400
|
|
|
401
|
+
## strict-mode
|
|
402
|
+
|
|
403
|
+
> **Strict mode** makes several changes to normal **JavaScript** semantics:
|
|
404
|
+
>
|
|
405
|
+
> - Eliminates some **JavaScript** silent errors by changing them to throw errors.
|
|
406
|
+
> - Fixes mistakes that make it difficult for **JavaScript** engines to perform optimizations: strict mode code can sometimes be made to run faster than identical code that's not strict mode.
|
|
407
|
+
> - Prohibits some syntax likely to be defined in future versions of **ECMAScript**.
|
|
408
|
+
>
|
|
409
|
+
> (c) [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode)
|
|
410
|
+
|
|
411
|
+
Add **strict mode** to **CommonJS**, and remove from **ESM**, where it enabled by default.
|
|
412
|
+
|
|
413
|
+
### β Example of incorrect code
|
|
414
|
+
|
|
415
|
+
ESM:
|
|
416
|
+
|
|
417
|
+
```js
|
|
418
|
+
'strict mode';
|
|
419
|
+
|
|
420
|
+
import a from 'b';
|
|
421
|
+
```
|
|
422
|
+
|
|
423
|
+
CommonJS:
|
|
424
|
+
|
|
425
|
+
```js
|
|
426
|
+
const a = require('b');
|
|
427
|
+
```
|
|
428
|
+
|
|
429
|
+
### β
Example of correct code
|
|
430
|
+
|
|
431
|
+
ESM:
|
|
432
|
+
|
|
433
|
+
```js
|
|
434
|
+
import a from 'b';
|
|
435
|
+
```
|
|
436
|
+
|
|
437
|
+
CommonJS:
|
|
438
|
+
|
|
439
|
+
```js
|
|
440
|
+
'strict mode';
|
|
441
|
+
|
|
442
|
+
const a = require('b');
|
|
443
|
+
```
|
|
444
|
+
|
|
400
445
|
## License
|
|
401
446
|
|
|
402
447
|
MIT
|
package/lib/index.js
CHANGED
|
@@ -24,6 +24,8 @@ const mjsFile = require('./mjs-file');
|
|
|
24
24
|
const renameFileCjsToJs = require('./rename-file-cjs-to-js');
|
|
25
25
|
const renameFileMjsToJs = require('./rename-file-mjs-to-js');
|
|
26
26
|
|
|
27
|
+
const strictMode = require('./strict-mode');
|
|
28
|
+
|
|
27
29
|
module.exports.rules = {
|
|
28
30
|
'convert-buffer-to-buffer-alloc': convertBufferToBufferAlloc,
|
|
29
31
|
'convert-fs-promises': convertFsPromises,
|
|
@@ -41,8 +43,13 @@ module.exports.rules = {
|
|
|
41
43
|
'convert-commonjs-to-esm-exports': ['off', convertCommonjsToEsmExports],
|
|
42
44
|
'convert-commonjs-to-esm-common': ['off', convertCommonjsToEsmCommons],
|
|
43
45
|
'convert-commonjs-to-esm-require': ['off', convertCommonjsToEsmRequire],
|
|
46
|
+
|
|
44
47
|
'cjs-file': ['off', cjsFile],
|
|
45
48
|
'mjs-file': ['off', mjsFile],
|
|
49
|
+
|
|
46
50
|
'rename-file-cjs-to-js': ['off', renameFileCjsToJs],
|
|
47
51
|
'rename-file-mjs-to-js': renameFileMjsToJs,
|
|
52
|
+
|
|
53
|
+
'add-strict-mode': strictMode.rules['add-missing'],
|
|
54
|
+
'remove-strict-mode': strictMode.rules['remove-useless'],
|
|
48
55
|
};
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const {
|
|
4
|
+
isExpressionStatement,
|
|
5
|
+
StringLiteral,
|
|
6
|
+
ExpressionStatement,
|
|
7
|
+
isProgram,
|
|
8
|
+
} = require('putout').types;
|
|
9
|
+
|
|
10
|
+
module.exports.report = () => `Add missing 'use strict' directive on top of CommonJS`;
|
|
11
|
+
|
|
12
|
+
module.exports.fix = ({node}) => {
|
|
13
|
+
node.body.unshift(ExpressionStatement(StringLiteral('use strict')));
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
module.exports.traverse = ({push, store}) => ({
|
|
17
|
+
'await __a(__args)'({scope}) {
|
|
18
|
+
const {block} = scope;
|
|
19
|
+
|
|
20
|
+
if (!isProgram(block))
|
|
21
|
+
return;
|
|
22
|
+
|
|
23
|
+
store('is-module', true);
|
|
24
|
+
},
|
|
25
|
+
'ImportDeclaration|ExportNamedDeclaration|ExportDefaultDeclaration|ExportAllDeclaration|TypeAlias'() {
|
|
26
|
+
store('is-module', true);
|
|
27
|
+
},
|
|
28
|
+
'module.exports = __a'() {
|
|
29
|
+
store('is-common', true);
|
|
30
|
+
},
|
|
31
|
+
'module.exports.__a = __b'() {
|
|
32
|
+
store('is-common', true);
|
|
33
|
+
},
|
|
34
|
+
'require(__a)'() {
|
|
35
|
+
store('is-common', true);
|
|
36
|
+
},
|
|
37
|
+
Program: {
|
|
38
|
+
exit(path) {
|
|
39
|
+
for (const node of path.node.body)
|
|
40
|
+
if (isExpressionStatement(node) && node.expression.value === 'use strict')
|
|
41
|
+
return;
|
|
42
|
+
|
|
43
|
+
if (store('is-module'))
|
|
44
|
+
return;
|
|
45
|
+
|
|
46
|
+
if (path.node.directives.length)
|
|
47
|
+
return;
|
|
48
|
+
|
|
49
|
+
if (!store('is-common'))
|
|
50
|
+
return;
|
|
51
|
+
|
|
52
|
+
push(path);
|
|
53
|
+
},
|
|
54
|
+
},
|
|
55
|
+
});
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const {operator, types} = require('putout');
|
|
4
|
+
|
|
5
|
+
const {remove} = operator;
|
|
6
|
+
const {isProgram} = types;
|
|
7
|
+
|
|
8
|
+
const isStrictMode = (a) => a.node.value.value === 'use strict';
|
|
9
|
+
|
|
10
|
+
module.exports.report = () => `Avoid 'use strict' in ESM`;
|
|
11
|
+
|
|
12
|
+
module.exports.fix = (path) => remove(path);
|
|
13
|
+
|
|
14
|
+
module.exports.traverse = ({push, store}) => ({
|
|
15
|
+
'await __a(__args)'({scope}) {
|
|
16
|
+
const {block} = scope;
|
|
17
|
+
|
|
18
|
+
if (isProgram(block))
|
|
19
|
+
store('is-module', true);
|
|
20
|
+
},
|
|
21
|
+
'ImportDeclaration|ExportNamedDeclaration|ExportDefaultDeclaration|ExportAllDeclaration'() {
|
|
22
|
+
store('is-module', true);
|
|
23
|
+
},
|
|
24
|
+
Program: {
|
|
25
|
+
exit(path) {
|
|
26
|
+
const [strictPath, ...paths] = path.get('body');
|
|
27
|
+
|
|
28
|
+
for (const path of paths) {
|
|
29
|
+
if (path.isExpressionStatement() && path.node.expression.value === 'use strict')
|
|
30
|
+
push(path);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const directives = path.get('directives');
|
|
34
|
+
|
|
35
|
+
if (directives.length)
|
|
36
|
+
directives
|
|
37
|
+
.slice(1)
|
|
38
|
+
.filter(isStrictMode)
|
|
39
|
+
.forEach(push);
|
|
40
|
+
|
|
41
|
+
if (!store('is-module'))
|
|
42
|
+
return;
|
|
43
|
+
|
|
44
|
+
directives
|
|
45
|
+
.filter(isStrictMode)
|
|
46
|
+
.forEach(push);
|
|
47
|
+
|
|
48
|
+
if (strictPath.isExpressionStatement() && strictPath.node.expression.value === 'use strict')
|
|
49
|
+
push(strictPath);
|
|
50
|
+
},
|
|
51
|
+
},
|
|
52
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/plugin-nodejs",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "10.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",
|
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
"main": "lib/index.js",
|
|
9
9
|
"exports": {
|
|
10
10
|
".": "./lib/index.js",
|
|
11
|
+
"./strict-mode": "./lib/strict-mode/index.js",
|
|
11
12
|
"./convert-esm-to-commonjs": "./lib/convert-esm-to-commonjs/index.js",
|
|
12
13
|
"./convert-commonjs-to-esm": "./lib/convert-commonjs-to-esm.js"
|
|
13
14
|
},
|
|
@@ -38,7 +39,9 @@
|
|
|
38
39
|
"nodejs"
|
|
39
40
|
],
|
|
40
41
|
"devDependencies": {
|
|
42
|
+
"@putout/plugin-declare": "*",
|
|
41
43
|
"@putout/plugin-putout": "*",
|
|
44
|
+
"@putout/plugin-typescript": "*",
|
|
42
45
|
"@putout/test": "^7.0.0",
|
|
43
46
|
"c8": "^8.0.0",
|
|
44
47
|
"eslint": "^8.0.1",
|
|
@@ -50,11 +53,11 @@
|
|
|
50
53
|
"nodemon": "^3.0.1"
|
|
51
54
|
},
|
|
52
55
|
"peerDependencies": {
|
|
53
|
-
"putout": ">=
|
|
56
|
+
"putout": ">=34"
|
|
54
57
|
},
|
|
55
58
|
"license": "MIT",
|
|
56
59
|
"engines": {
|
|
57
|
-
"node": ">=
|
|
60
|
+
"node": ">=18"
|
|
58
61
|
},
|
|
59
62
|
"publishConfig": {
|
|
60
63
|
"access": "public"
|