@putout/plugin-nodejs 1.0.0 → 3.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
@@ -1,12 +1,9 @@
1
- # @putout/plugin-nodejs [![NPM version][NPMIMGURL]][NPMURL] [![Dependency Status][DependencyStatusIMGURL]][DependencyStatusURL]
1
+ # @putout/plugin-nodejs [![NPM version][NPMIMGURL]][NPMURL]
2
2
 
3
- [NPMIMGURL]: https://img.shields.io/npm/v/@putout/plugin-nodejs.svg?style=flat&longCache=true
4
- [NPMURL]: https://npmjs.org/package/@putout/plugin-nodejs"npm"
3
+ [NPMIMGURL]: https://img.shields.io/npm/v/@putout/plugin-nodejs.svg?style=flat&longCache=true
4
+ [NPMURL]: https://npmjs.org/package/@putout/plugin-nodejs"npm"
5
5
 
6
- [DependencyStatusURL]: https://david-dm.org/coderaiser/putout?path=packages/plugin-nodejs
7
- [DependencyStatusIMGURL]: https://david-dm.org/coderaiser/putout.svg?path=packages/plugin-nodejs
8
-
9
- `putout` plugin adds ability to transform to new [nodejs.org](https://nodejs.io) API and best practices.
6
+ 🐊[`Putout`](https://github.com/coderaiser/putout) plugin adds ability to transform to new [nodejs.org](https://nodejs.io) API and best practices.
10
7
 
11
8
  ## Install
12
9
 
@@ -14,17 +11,22 @@
14
11
  npm i putout @putout/plugin-nodejs -D
15
12
  ```
16
13
 
17
- ## Rules
14
+ ## Options
18
15
 
19
16
  ```json
20
17
  {
21
18
  "rules": {
22
- "nodejs/convert-fs-promises": "on"
19
+ "nodejs/convert-fs-promises": "on",
20
+ "nodejs/convert-promisify-to-fs-promises": "on",
21
+ "nodejs/convert-dirname-to-url": "on",
22
+ "nodejs/remove-process-exit": "on"
23
23
  }
24
24
  }
25
25
  ```
26
26
 
27
- # convert-fs-promises
27
+ ## Rules
28
+
29
+ ### convert-fs-promises
28
30
 
29
31
  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 from in `ESM` to:
30
32
 
@@ -32,19 +34,72 @@ Convert [fs.promises](https://nodejs.org/dist/latest-v15.x/docs/api/fs.html#fs_f
32
34
  import {readFile} from 'fs/promises';
33
35
  ```
34
36
 
35
- ## ❌ Incorrect code example
37
+ #### ❌ Incorrect code example
36
38
 
37
39
  ```js
38
40
  const {readFile} = require('fs').promises;
39
41
  ```
40
42
 
41
- ## ✅ Correct code Example
43
+ #### ✅ Correct code Example
42
44
 
43
45
  ```js
44
46
  const {readFile} = require('fs/promises');
45
47
  ```
46
48
 
49
+ ### convert-promisify-to-fs-promises
50
+
51
+ #### ❌ Incorrect code example
52
+
53
+ ```js
54
+ const fs = require('fs');
55
+ const readFile = promisify(fs.readFile);
56
+ ```
57
+
58
+ #### ✅ Correct code Example
59
+
60
+ ```js
61
+ const {readFile} = require('fs/promises');
62
+ ```
63
+
64
+ ### convert-dirname-to-url
65
+
66
+ Only for `EcmaScript Modules`.
67
+
68
+ #### ❌ Incorrect code example
69
+
70
+ ```js
71
+ import {readFile} from 'fs/promises';
72
+
73
+ const file1 = join(__dirname, '../../package.json');
74
+ const file2 = path.join(__dirname, '../../package.json');
75
+ ```
76
+
77
+ #### ✅ Correct code Example
78
+
79
+ ```js
80
+ import {readFile} from 'fs/promises';
81
+
82
+ const file1 = new URL('../../package.json', import.meta.url);
83
+ const file2 = new URL('../../package.json', import.meta.url);
84
+ ```
85
+
86
+ ### convert-promisify-to-fs-promises
87
+
88
+ #### ❌ Incorrect code example
89
+
90
+ ```js
91
+ const fs = require('fs');
92
+ const readFile = promisify(fs.readFile);
93
+ ```
94
+
95
+ ### remove-process-exit
96
+
97
+ In most cases `process.exit()` is called from `bin` directory, if not - disable this rule using `match`.
98
+
99
+ ```diff
100
+ -process.exit();
101
+ ```
102
+
47
103
  ## License
48
104
 
49
105
  MIT
50
-
@@ -0,0 +1,13 @@
1
+ 'use strict';
2
+
3
+ const {operator} = require('putout');
4
+ const {isESM} = operator;
5
+
6
+ module.exports.report = () => `Use 'import.meta.url' instead of '__dirname'`;
7
+
8
+ module.exports.filter = isESM;
9
+
10
+ module.exports.replace = () => ({
11
+ 'join(__dirname, __a)': 'new URL(__a, import.meta.url).pathname',
12
+ 'path.join(__dirname, __a)': 'new URL(__a, import.meta.url).pathname',
13
+ });
@@ -1,4 +1,4 @@
1
- const {StringLiteral, RegExpLiteral} = require('putout').types;
1
+ 'use strict';
2
2
 
3
3
  module.exports.report = () => '"fs/promises" should be used instead of "fs.promises"';
4
4
 
@@ -0,0 +1,58 @@
1
+ 'use strict';
2
+
3
+ const {
4
+ types: t,
5
+ operator,
6
+ } = require('putout');
7
+
8
+ const {replaceWith} = operator;
9
+
10
+ const NOT_COMPUTED = false;
11
+ const SHORTHAND = true;
12
+
13
+ module.exports.report = () => `fs.promises should be used instead of fs`;
14
+
15
+ module.exports.fix = ({path, promisified}) => {
16
+ const props = [];
17
+
18
+ for (const path of promisified) {
19
+ const [declarator] = path.node.declarations;
20
+ const {name} = declarator.id;
21
+
22
+ props.push(t.ObjectProperty(t.Identifier(name), t.Identifier(name), NOT_COMPUTED, SHORTHAND));
23
+ path.remove();
24
+ }
25
+
26
+ const {init} = path.node;
27
+ init.arguments[0].value = 'fs/promises';
28
+
29
+ replaceWith(path.get('id'), t.ObjectPattern(props));
30
+ };
31
+
32
+ module.exports.find = (ast, {push, traverse}) => {
33
+ const fs = [];
34
+ const promisified = [];
35
+
36
+ traverse(ast, {
37
+ 'const fs = require("fs")'(path) {
38
+ fs.push(path);
39
+ },
40
+ 'const __ = promisify(fs.__)'(path) {
41
+ promisified.push(path);
42
+ },
43
+ });
44
+
45
+ const [fsPath] = fs;
46
+
47
+ if (!fsPath)
48
+ return;
49
+
50
+ if (!promisified.length)
51
+ return;
52
+
53
+ push({
54
+ path: fsPath.get('declarations.0'),
55
+ promisified,
56
+ });
57
+ };
58
+
package/lib/index.js CHANGED
@@ -6,5 +6,8 @@ const getRule = (a) => ({
6
6
 
7
7
  module.exports.rules = {
8
8
  ...getRule('convert-fs-promises'),
9
+ ...getRule('convert-promisify-to-fs-promises'),
10
+ ...getRule('convert-dirname-to-url'),
11
+ ...getRule('remove-process-exit'),
9
12
  };
10
13
 
@@ -0,0 +1,9 @@
1
+ 'use strict';
2
+
3
+ module.exports.report = () => '"process.exit" should not be used';
4
+
5
+ module.exports.replace = () => ({
6
+ 'process.exit()': '',
7
+ 'process["exit"]()': '',
8
+ });
9
+
package/package.json CHANGED
@@ -1,9 +1,10 @@
1
1
  {
2
2
  "name": "@putout/plugin-nodejs",
3
- "version": "1.0.0",
3
+ "version": "3.0.0",
4
+ "type": "commonjs",
4
5
  "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
5
6
  "description": "putout plugin adds ability to transform code to new API of Node.js",
6
- "homepage": "http://github.com/coderaiser/putout",
7
+ "homepage": "https://github.com/coderaiser/putout/tree/master/packages/plugin-nodejs#readme",
7
8
  "main": "lib/index.js",
8
9
  "release": false,
9
10
  "tag": false,
@@ -30,20 +31,17 @@
30
31
  "nodejs"
31
32
  ],
32
33
  "devDependencies": {
33
- "@putout/plugin-remove-unused-expressions": "^1.2.1",
34
- "@putout/plugin-strict-mode": "^1.2.1",
35
- "@putout/test": "^3.0.0",
36
- "coveralls": "^3.0.0",
37
- "eslint": "^7.6.0",
34
+ "@putout/test": "^4.0.0",
35
+ "c8": "^7.5.0",
36
+ "eslint": "^8.0.1",
38
37
  "eslint-plugin-node": "^11.0.0",
39
- "eslint-plugin-putout": "^6.0.0",
40
- "lerna": "^3.8.5",
38
+ "eslint-plugin-putout": "^12.0.0",
39
+ "lerna": "^4.0.0",
41
40
  "madrun": "^8.0.1",
42
- "nodemon": "^2.0.1",
43
- "nyc": "^15.0.1"
41
+ "nodemon": "^2.0.1"
44
42
  },
45
43
  "peerDependencies": {
46
- "putout": ">=13"
44
+ "putout": ">=24"
47
45
  },
48
46
  "license": "MIT",
49
47
  "engines": {