@putout/plugin-nodejs 18.0.1 → 18.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
@@ -19,6 +19,7 @@ npm i putout @putout/plugin-nodejs -D
19
19
 
20
20
  - ✅ [add-node-prefix](#add-node-prefix);
21
21
  - ✅ [add-missing-strict-mode](#add-missing-strict-mode);
22
+ - ✅ [apply-global-this](#apply-global-this);
22
23
  - ✅ [convert-buffer-to-buffer-alloc](#convert-buffer-to-buffer-alloc);
23
24
  - ✅ [convert-commonjs-to-esm](#convert-commonjs-to-esm);
24
25
  - ✅ [convert-dirname-to-url](#convert-dirname-to-url);
@@ -45,13 +46,15 @@ npm i putout @putout/plugin-nodejs -D
45
46
  ```json
46
47
  {
47
48
  "rules": {
49
+ "nodejs/add-missing-strict-mode": "on",
50
+ "nodejs/add-node-prefix": "on",
51
+ "nodejs/apply-global-this": "off",
48
52
  "nodejs/convert-commonjs-to-esm": "off",
49
53
  "nodejs/convert-esm-to-commonjs": "off",
50
54
  "nodejs/cjs-file": "off",
51
55
  "nodejs/mjs-file": "off",
52
56
  "nodejs/rename-file-cjs-to-js": "off",
53
57
  "nodejs/rename-file-mjs-to-js": "off",
54
- "nodejs/add-node-prefix": "on",
55
58
  "nodejs/convert-buffer-to-buffer-alloc": "on",
56
59
  "nodejs/convert-fs-promises": "on",
57
60
  "nodejs/convert-promisify-to-fs-promises": "on",
@@ -63,7 +66,6 @@ npm i putout @putout/plugin-nodejs -D
63
66
  "nodejs/declare-after-require": "on",
64
67
  "nodejs/group-require-by-id": "on",
65
68
  "nodejs/remove-process-exit": "on",
66
- "nodejs/add-missing-strict-mode": "on",
67
69
  "nodejs/remove-useless-strict-mode": "on",
68
70
  "nodejs/remove-illegal-strict-mode": "on",
69
71
  "nodejs/remove-useless-promisify": "on"
@@ -101,9 +103,36 @@ await import('node:path');
101
103
 
102
104
  Linter | Rule | Fix
103
105
  --------|-------|------------|
104
- 🐊 **Putout** | [`apply-node-prefix`](https://github.com/coderaiser/putout/tree/master/packages/plugin-nodejs/apply-node-prefix#readme) | ✅
106
+ 🐊 **Putout** | [`add-node-prefix`](https://github.com/coderaiser/putout/tree/master/packages/plugin-nodejs/add-node-prefix#readme) | ✅
105
107
  ⏣ **ESLint** | [`prefer-node-protocol`](https://github.com/eslint-community/eslint-plugin-n/blob/master/docs/rules/prefer-node-protocol.md#readme) | ✅
106
108
 
109
+ ## apply-global-this
110
+
111
+ > Legacy. Use `globalThis` instead.
112
+ >
113
+ > (c) [nodejs.org](https://nodejs.org/api/globals.html#global)
114
+
115
+ Check out in 🐊[Putout Editor](https://putout.cloudcmd.io/#/gist/c72c8e1b1d0c2b45fd0d15d837dcae52/c7f33d77d19efe962339debbcf20f68bf2159aee).
116
+
117
+ ### ❌ Example of incorrect code
118
+
119
+ ```js
120
+ global.__putout_debug = debugFn;
121
+ ```
122
+
123
+ ### ✅ Example of correct code
124
+
125
+ ```js
126
+ globalThis.__putout_debug = debugFn;
127
+ ```
128
+
129
+ ### Comparison
130
+
131
+ Linter | Rule | Fix
132
+ -------|------|------------|
133
+ 🐊 **Putout** | [`apply-global-this`](https://github.com/coderaiser/putout/tree/master/packages/plugin-nodejs/apply-node-prefix#readme) | ✅
134
+ ⏣ **ESLint** | [`no-node-globals`](https://docs.deno.com/lint/rules/no-node-globals/) | ❌
135
+
107
136
  ## convert-buffer-to-buffer-alloc
108
137
 
109
138
  > The `Buffer()` function and `new Buffer()` constructor are **deprecated** due to API usability issues that can lead to accidental security issues.
@@ -254,6 +283,7 @@ process.exit();
254
283
 
255
284
  Add declarations to built-in node.js modules:
256
285
 
286
+ - [buffer](https://nodejs.org/dist/latest-v24.x/docs/api/buffer.html);
257
287
  - [child_process](https://nodejs.org/dist/latest-v24.x/docs/api/child_process.html);
258
288
  - [events](https://nodejs.org/dist/latest-v24.x/docs/api/events.html);
259
289
  - [fs](https://nodejs.org/dist/latest-v24.x/docs/api/fs.html);
@@ -0,0 +1,6 @@
1
+ export const report = () => `Use 'globalThis' instead of 'global'`;
2
+
3
+ export const replace = () => ({
4
+ 'global.__a': 'globalThis.__a',
5
+ 'const __a = global': 'const __a = globalThis',
6
+ });
@@ -5,6 +5,7 @@ const {
5
5
  isIdentifier,
6
6
  isImportSpecifier,
7
7
  exportNamedDeclaration,
8
+ isObjectExpression,
8
9
  } = types;
9
10
 
10
11
  const {replaceWith} = operator;
@@ -39,7 +40,25 @@ export const match = () => ({
39
40
  });
40
41
 
41
42
  export const replace = () => ({
42
- 'module.exports = __a': 'export default __a',
43
+ 'module.exports = __a': ({__a}) => {
44
+ if (!isObjectExpression(__a))
45
+ return 'export default __a';
46
+
47
+ const result = ['export {'];
48
+
49
+ for (const {key, value} of __a.properties) {
50
+ if (key.name === value.name) {
51
+ result.push(`${key.name},`);
52
+ continue;
53
+ }
54
+
55
+ result.push(`${key.name} as ${value.name}`);
56
+ }
57
+
58
+ result.push('};');
59
+
60
+ return result.join('\n');
61
+ },
43
62
  'module.exports.__a = __b': ({__a, __b}, path) => {
44
63
  const {name} = __a;
45
64
 
@@ -1,3 +1,4 @@
1
+ import buffer from './modules/buffer.js';
1
2
  import child_process from './modules/child_process.js';
2
3
  import util from './modules/util.js';
3
4
  import url from './modules/url.js';
@@ -13,6 +14,7 @@ import events from './modules/events.js';
13
14
  import timers from './modules/timers.js';
14
15
 
15
16
  export const declare = () => ({
17
+ ...buffer,
16
18
  ...events,
17
19
  ...fs,
18
20
  ...fsPromises,
@@ -0,0 +1,9 @@
1
+ export default {
2
+ Blob: `import {Blob} from 'node:buffer'`,
3
+ Buffer: `import {Buffer} from 'node:buffer'`,
4
+ File: `import {File} from 'node:buffer'`,
5
+ isAscii: `import {isAscii} from 'node:buffer'`,
6
+ isUtf8: `import {isUtf8} from 'node:buffer'`,
7
+ resolveObjectURL: `import {resolveObjectURL} from 'node:buffer'`,
8
+ transcode: `import {transcode} from 'node:buffer'`,
9
+ };
package/lib/index.js CHANGED
@@ -1,3 +1,4 @@
1
+ import * as applyGlobalThis from './apply-global-this/index.js';
1
2
  import * as convertBufferToBufferAlloc from './convert-buffer-to-buffer-alloc/index.js';
2
3
  import * as convertFsPromises from './convert-fs-promises/index.js';
3
4
  import * as convertPromisifyToFsPromises from './convert-promisify-to-fs-promises/index.js';
@@ -50,4 +51,5 @@ export const rules = {
50
51
  'remove-illegal-strict-mode': strictMode.rules['remove-illegal'],
51
52
  'remove-useless-promisify': removeUselessPromisify,
52
53
  'group-require-by-id': groupRequireById,
54
+ 'apply-global-this': applyGlobalThis,
53
55
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/plugin-nodejs",
3
- "version": "18.0.1",
3
+ "version": "18.2.0",
4
4
  "type": "module",
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",
@@ -44,7 +44,7 @@
44
44
  "@putout/plugin-declare-before-reference": "*",
45
45
  "@putout/plugin-esm": "*",
46
46
  "@putout/plugin-putout": "*",
47
- "@putout/plugin-reuse-duplicate-init": "*",
47
+ "@putout/plugin-variables": "*",
48
48
  "@putout/plugin-typescript": "*",
49
49
  "@putout/test": "^14.0.0",
50
50
  "c8": "^10.0.0",