@putout/plugin-nodejs 3.1.0 → 3.2.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,7 +1,7 @@
1
1
  # @putout/plugin-nodejs [![NPM version][NPMIMGURL]][NPMURL]
2
2
 
3
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"
4
+ [NPMURL]: https://npmjs.org/package/@putout/plugin-nodejs "npm"
5
5
 
6
6
  🐊[`Putout`](https://github.com/coderaiser/putout) plugin adds ability to transform to new [nodejs.org](https://nodejs.io) API and best practices.
7
7
 
@@ -19,6 +19,7 @@ npm i putout @putout/plugin-nodejs -D
19
19
  "nodejs/convert-fs-promises": "on",
20
20
  "nodejs/convert-promisify-to-fs-promises": "on",
21
21
  "nodejs/convert-dirname-to-url": "on",
22
+ "nodejs/convert-url-to-dirname": "on",
22
23
  "nodejs/convert-top-level-return": "on",
23
24
  "nodejs/remove-process-exit": "on"
24
25
  }
@@ -35,13 +36,13 @@ Convert [fs.promises](https://nodejs.org/dist/latest-v15.x/docs/api/fs.html#fs_f
35
36
  import {readFile} from 'fs/promises';
36
37
  ```
37
38
 
38
- #### ❌ Incorrect code example
39
+ #### ❌ Example of incorrect code
39
40
 
40
41
  ```js
41
42
  const {readFile} = require('fs').promises;
42
43
  ```
43
44
 
44
- #### ✅ Correct code Example
45
+ #### ✅ Example of correct code
45
46
 
46
47
  ```js
47
48
  const {readFile} = require('fs/promises');
@@ -49,14 +50,14 @@ const {readFile} = require('fs/promises');
49
50
 
50
51
  ### convert-promisify-to-fs-promises
51
52
 
52
- #### ❌ Incorrect code example
53
+ #### ❌ Example of incorrect code
53
54
 
54
55
  ```js
55
56
  const fs = require('fs');
56
57
  const readFile = promisify(fs.readFile);
57
58
  ```
58
59
 
59
- #### ✅ Correct code Example
60
+ #### ✅ Example of correct code
60
61
 
61
62
  ```js
62
63
  const {readFile} = require('fs/promises');
@@ -66,7 +67,7 @@ const {readFile} = require('fs/promises');
66
67
 
67
68
  Only for `EcmaScript Modules`.
68
69
 
69
- #### ❌ Incorrect code example
70
+ #### ❌ Example of incorrect code
70
71
 
71
72
  ```js
72
73
  import {readFile} from 'fs/promises';
@@ -75,7 +76,7 @@ const file1 = join(__dirname, '../../package.json');
75
76
  const file2 = path.join(__dirname, '../../package.json');
76
77
  ```
77
78
 
78
- #### ✅ Correct code Example
79
+ #### ✅ Example of correct code
79
80
 
80
81
  ```js
81
82
  import {readFile} from 'fs/promises';
@@ -84,6 +85,24 @@ const file1 = new URL('../../package.json', import.meta.url);
84
85
  const file2 = new URL('../../package.json', import.meta.url);
85
86
  ```
86
87
 
88
+ ### convert-dirname-to-url
89
+
90
+ Only for `CommonJS`.
91
+
92
+ #### ❌ Example of incorrect code
93
+
94
+ ```js
95
+ const {readFile} = require('fs/promises');
96
+ const file = new URL('../../package.json', import.meta.url);
97
+ ```
98
+
99
+ #### ✅ Example of correct code
100
+
101
+ ```js
102
+ const {readFile} = require('fs/promises');
103
+ const file = join(__dirname, '../../package.json');
104
+ ```
105
+
87
106
  ### remove-process-exit
88
107
 
89
108
  In most cases `process.exit()` is called from `bin` directory, if not - disable this rule using `match`.
@@ -94,13 +113,13 @@ In most cases `process.exit()` is called from `bin` directory, if not - disable
94
113
 
95
114
  ### convert-top-level-return
96
115
 
97
- #### ❌ Incorrect code example
116
+ #### ❌ Example of incorrect code
98
117
 
99
118
  ```js
100
119
  return;
101
120
  ```
102
121
 
103
- #### ✅ Correct code Example
122
+ #### ✅ Example of correct code
104
123
 
105
124
  ```js
106
125
  process.exit();
@@ -0,0 +1,13 @@
1
+ 'use strict';
2
+
3
+ const {operator} = require('putout');
4
+ const {isESM} = operator;
5
+ const not = (fn) => (...a) => !fn(...a);
6
+
7
+ module.exports.report = () => `Use __dirname instead of 'import.meta.url' in CommonJS`;
8
+
9
+ module.exports.filter = not(isESM);
10
+
11
+ module.exports.replace = () => ({
12
+ 'new URL(__a, import.meta.url).pathname': 'join(__dirname, __a)',
13
+ });
package/lib/index.js CHANGED
@@ -8,6 +8,7 @@ module.exports.rules = {
8
8
  ...getRule('convert-fs-promises'),
9
9
  ...getRule('convert-promisify-to-fs-promises'),
10
10
  ...getRule('convert-dirname-to-url'),
11
+ ...getRule('convert-url-to-dirname'),
11
12
  ...getRule('convert-top-level-return'),
12
13
  ...getRule('remove-process-exit'),
13
14
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/plugin-nodejs",
3
- "version": "3.1.0",
3
+ "version": "3.2.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",
@@ -31,13 +31,13 @@
31
31
  "nodejs"
32
32
  ],
33
33
  "devDependencies": {
34
- "@putout/test": "^4.0.0",
34
+ "@putout/test": "^5.0.0",
35
35
  "c8": "^7.5.0",
36
36
  "eslint": "^8.0.1",
37
37
  "eslint-plugin-node": "^11.0.0",
38
38
  "eslint-plugin-putout": "^13.0.0",
39
39
  "lerna": "^4.0.0",
40
- "madrun": "^8.0.1",
40
+ "madrun": "^9.0.0",
41
41
  "nodemon": "^2.0.1"
42
42
  },
43
43
  "peerDependencies": {