@putout/plugin-nodejs 9.4.0 β†’ 10.0.1

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,46 @@
1
+ 'use strict';
2
+
3
+ const {
4
+ isExpressionStatement,
5
+ StringLiteral,
6
+ ExpressionStatement,
7
+ } = require('putout').types;
8
+
9
+ module.exports.report = () => `Add missing 'use strict' directive on top of CommonJS`;
10
+
11
+ module.exports.fix = ({node}) => {
12
+ node.body.unshift(ExpressionStatement(StringLiteral('use strict')));
13
+ };
14
+
15
+ module.exports.traverse = ({push, store}) => ({
16
+ 'ImportExpression|ImportDeclaration|ExportNamedDeclaration|ExportDefaultDeclaration|ExportAllDeclaration|TypeAlias'() {
17
+ store('is-module', true);
18
+ },
19
+ 'module.exports = __a'() {
20
+ store('is-common', true);
21
+ },
22
+ 'module.exports.__a = __b'() {
23
+ store('is-common', true);
24
+ },
25
+ 'require(__a)'() {
26
+ store('is-common', true);
27
+ },
28
+ Program: {
29
+ exit(path) {
30
+ for (const node of path.node.body)
31
+ if (isExpressionStatement(node) && node.expression.value === 'use strict')
32
+ return;
33
+
34
+ if (store('is-module'))
35
+ return;
36
+
37
+ if (path.node.directives.length)
38
+ return;
39
+
40
+ if (!store('is-common'))
41
+ return;
42
+
43
+ push(path);
44
+ },
45
+ },
46
+ });
@@ -0,0 +1,9 @@
1
+ 'use strict';
2
+
3
+ const addMissing = require('./add-missing');
4
+ const removeUseless = require('./remove-useless');
5
+
6
+ module.exports.rules = {
7
+ 'add-missing': addMissing,
8
+ 'remove-useless': removeUseless,
9
+ };
@@ -0,0 +1,44 @@
1
+ 'use strict';
2
+
3
+ const {operator} = require('putout');
4
+ const {remove} = operator;
5
+
6
+ const isStrictMode = (a) => a.node.value.value === 'use strict';
7
+
8
+ module.exports.report = () => `Avoid 'use strict' in ESM`;
9
+
10
+ module.exports.fix = (path) => remove(path);
11
+
12
+ module.exports.traverse = ({push, store}) => ({
13
+ 'ImportExpression|ImportDeclaration|ExportNamedDeclaration|ExportDefaultDeclaration|ExportAllDeclaration'() {
14
+ store('is-module', true);
15
+ },
16
+ Program: {
17
+ exit(path) {
18
+ const [strictPath, ...paths] = path.get('body');
19
+
20
+ for (const path of paths) {
21
+ if (path.isExpressionStatement() && path.node.expression.value === 'use strict')
22
+ push(path);
23
+ }
24
+
25
+ const directives = path.get('directives');
26
+
27
+ if (directives.length)
28
+ directives
29
+ .slice(1)
30
+ .filter(isStrictMode)
31
+ .forEach(push);
32
+
33
+ if (!store('is-module'))
34
+ return;
35
+
36
+ directives
37
+ .filter(isStrictMode)
38
+ .forEach(push);
39
+
40
+ if (strictPath.isExpressionStatement() && strictPath.node.expression.value === 'use strict')
41
+ push(strictPath);
42
+ },
43
+ },
44
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/plugin-nodejs",
3
- "version": "9.4.0",
3
+ "version": "10.0.1",
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,23 +39,25 @@
38
39
  "nodejs"
39
40
  ],
40
41
  "devDependencies": {
42
+ "@putout/plugin-declare": "*",
41
43
  "@putout/plugin-putout": "*",
42
- "@putout/test": "^7.0.0",
44
+ "@putout/plugin-typescript": "*",
45
+ "@putout/test": "^8.0.0",
43
46
  "c8": "^8.0.0",
44
47
  "eslint": "^8.0.1",
45
48
  "eslint-plugin-n": "^16.0.0",
46
- "eslint-plugin-putout": "^21.0.0",
49
+ "eslint-plugin-putout": "^22.0.0",
47
50
  "lerna": "^6.0.1",
48
- "madrun": "^9.0.0",
51
+ "madrun": "^10.0.0",
49
52
  "montag": "^1.2.1",
50
53
  "nodemon": "^3.0.1"
51
54
  },
52
55
  "peerDependencies": {
53
- "putout": ">=33"
56
+ "putout": ">=34"
54
57
  },
55
58
  "license": "MIT",
56
59
  "engines": {
57
- "node": ">=16.17"
60
+ "node": ">=18"
58
61
  },
59
62
  "publishConfig": {
60
63
  "access": "public"