Skip to content

Commit 710c5f1

Browse files
committed
feature: @putout/plugin-nodejs: apply-global-this: add
1 parent bb6a80a commit 710c5f1

File tree

25 files changed

+212
-134
lines changed

25 files changed

+212
-134
lines changed

packages/compare/lib/debug.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ module.exports.createDebug = (namespace) => {
77

88
return new Proxy(log, {
99
apply(target, thisArg, args) {
10-
global.__putout_debug?.(namespace, ...args);
10+
globalThis.__putout_debug?.(namespace, ...args);
1111
return target(...args);
1212
},
1313
get(target, prop) {
14-
if (global.__putout_debug?.[prop])
14+
if (globalThis.__putout_debug?.[prop])
1515
return true;
1616

1717
return target[prop];

packages/compare/lib/log.spec.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ test('putout: compare: log', (t) => {
1212
const debug = stub();
1313

1414
debug.enabled = true;
15-
global.__putout_debug = debug;
15+
globalThis.__putout_debug = debug;
1616
log('hello', 'world');
17-
delete global.__putout_debug;
17+
delete globalThis.__putout_debug;
1818

1919
const expected = [
2020
'putout:compare',
@@ -29,10 +29,10 @@ test('putout: compare: run-plugins: template: log: array', (t) => {
2929
const debug = stub();
3030

3131
debug.enabled = true;
32-
global.__putout_debug = debug;
32+
globalThis.__putout_debug = debug;
3333

3434
log([identifier('hello')], [identifier('world')]);
35-
delete global.__putout_debug;
35+
delete globalThis.__putout_debug;
3636

3737
const expected = [
3838
'putout:compare',
@@ -47,10 +47,10 @@ test('putout: compare: log: object', (t) => {
4747
const debug = stub();
4848

4949
debug.enabled = true;
50-
global.__putout_debug = debug;
50+
globalThis.__putout_debug = debug;
5151

5252
log(identifier('hello'), identifier('world'));
53-
delete global.__putout_debug;
53+
delete globalThis.__putout_debug;
5454

5555
const expected = [
5656
'putout:compare',

packages/engine-runner/lib/debug.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ module.exports.createDebug = (namespace) => {
77

88
return new Proxy(log, {
99
apply(target, thisArg, args) {
10-
global.__putout_debug?.(namespace, ...args);
10+
globalThis.__putout_debug?.(namespace, ...args);
1111
return target(...args);
1212
},
1313
get(target, prop) {
14-
if (global.__putout_debug?.[prop])
14+
if (globalThis.__putout_debug?.[prop])
1515
return true;
1616

1717
return target[prop];

packages/engine-runner/lib/run-fix.spec.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ test('fix: error: nested path: debug', (t) => {
3636

3737
debugFn.enabled = true;
3838

39-
global.__putout_debug = stub().returns(debugFn);
39+
globalThis.__putout_debug = stub().returns(debugFn);
4040

4141
const remove = () => {
4242
throw Error('hello');
@@ -60,19 +60,19 @@ test('fix: error: nested path: debug', (t) => {
6060
position,
6161
});
6262

63-
delete global.__putout_debug;
63+
delete globalThis.__putout_debug;
6464

6565
t.equal(e.loc, position);
6666
t.end();
6767
});
6868

6969
test('fix: error: nested path: debug: nested path', (t) => {
70-
const {__putout_debug} = global;
70+
const {__putout_debug} = globalThis;
7171
const debugFn = stub();
7272

7373
debugFn.enabled = true;
7474

75-
global.__putout_debug = stub().returns(debugFn);
75+
globalThis.__putout_debug = stub().returns(debugFn);
7676

7777
const remove = () => {
7878
throw Error('hello');
@@ -98,7 +98,7 @@ test('fix: error: nested path: debug: nested path', (t) => {
9898
position,
9999
});
100100

101-
global.__putout_debug = __putout_debug;
101+
globalThis.__putout_debug = __putout_debug;
102102

103103
t.equal(e.loc, position);
104104
t.end();

packages/engine-runner/lib/template/index.spec.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -437,26 +437,26 @@ test('putout: plugin: traverse: template: log', (t) => {
437437

438438
test('putout: engine: runner: template: log', (t) => {
439439
const debug = stub();
440-
const {__putout_debug} = global;
440+
const {__putout_debug} = globalThis;
441441

442-
global.__putout_debug = stub().returns(debug);
442+
globalThis.__putout_debug = stub().returns(debug);
443443

444444
_log();
445-
global.__putout_debug = __putout_debug;
445+
globalThis.__putout_debug = __putout_debug;
446446

447447
t.notCalled(debug, 'should call debug');
448448
t.end();
449449
});
450450

451451
test('putout: engine: runner: template: log: enabled', (t) => {
452-
const {__putout_debug} = global;
452+
const {__putout_debug} = globalThis;
453453
const debug = stub();
454454

455455
debug.enabled = true;
456-
global.__putout_debug = debug;
456+
globalThis.__putout_debug = debug;
457457

458458
_log('rule', 'path');
459-
global.__putout_debug = __putout_debug;
459+
globalThis.__putout_debug = __putout_debug;
460460

461461
t.calledWith(debug, ['putout:runner:template', 'rule', 'path'], 'should call debug');
462462
t.end();

packages/engine-runner/test/runner.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,7 @@ test('putout: runner: debug', (t) => {
732732

733733
debugFn.enabled = true;
734734

735-
global.__putout_debug = stub().returns(debugFn);
735+
globalThis.__putout_debug = stub().returns(debugFn);
736736

737737
const {code} = putout(fixture.debug, {
738738
fix: true,
@@ -755,7 +755,7 @@ test('putout: runner: debug: replace', (t) => {
755755
const debugFn = stub();
756756

757757
debugFn.enabled = true;
758-
global.__putout_debug = debugFn;
758+
globalThis.__putout_debug = debugFn;
759759

760760
process.env.DEBUG = 'putout:runner:fix';
761761

@@ -770,7 +770,7 @@ test('putout: runner: debug: replace', (t) => {
770770
];
771771

772772
process.env.DEBUG = DEBUG;
773-
delete global.__putout_debug;
773+
delete globalThis.__putout_debug;
774774

775775
t.calledWith(debugFn, expected);
776776
t.end();

packages/plugin-nodejs/README.md

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ npm i putout @putout/plugin-nodejs -D
1919

2020
-[add-node-prefix](#add-node-prefix);
2121
-[add-missing-strict-mode](#add-missing-strict-mode);
22+
-[apply-global-this](#apply-global-this);
2223
-[convert-buffer-to-buffer-alloc](#convert-buffer-to-buffer-alloc);
2324
-[convert-commonjs-to-esm](#convert-commonjs-to-esm);
2425
-[convert-dirname-to-url](#convert-dirname-to-url);
@@ -45,13 +46,15 @@ npm i putout @putout/plugin-nodejs -D
4546
```json
4647
{
4748
"rules": {
49+
"nodejs/add-missing-strict-mode": "on",
50+
"nodejs/add-node-prefix": "on",
51+
"nodejs/apply-global-this": "off",
4852
"nodejs/convert-commonjs-to-esm": "off",
4953
"nodejs/convert-esm-to-commonjs": "off",
5054
"nodejs/cjs-file": "off",
5155
"nodejs/mjs-file": "off",
5256
"nodejs/rename-file-cjs-to-js": "off",
5357
"nodejs/rename-file-mjs-to-js": "off",
54-
"nodejs/add-node-prefix": "on",
5558
"nodejs/convert-buffer-to-buffer-alloc": "on",
5659
"nodejs/convert-fs-promises": "on",
5760
"nodejs/convert-promisify-to-fs-promises": "on",
@@ -63,7 +66,6 @@ npm i putout @putout/plugin-nodejs -D
6366
"nodejs/declare-after-require": "on",
6467
"nodejs/group-require-by-id": "on",
6568
"nodejs/remove-process-exit": "on",
66-
"nodejs/add-missing-strict-mode": "on",
6769
"nodejs/remove-useless-strict-mode": "on",
6870
"nodejs/remove-illegal-strict-mode": "on",
6971
"nodejs/remove-useless-promisify": "on"
@@ -101,9 +103,36 @@ await import('node:path');
101103

102104
Linter | Rule | Fix
103105
--------|-------|------------|
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) | ✅
105107
**ESLint** | [`prefer-node-protocol`](https://github.com/eslint-community/eslint-plugin-n/blob/master/docs/rules/prefer-node-protocol.md#readme) | ✅
106108

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+
107136
## convert-buffer-to-buffer-alloc
108137

109138
> The `Buffer()` function and `new Buffer()` constructor are **deprecated** due to API usability issues that can lead to accidental security issues.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
globalThis.__putout_debug = debugFn;
2+
delete globalThis.__putout_debug;
3+
const {__putout_debug} = globalThis;
4+
const fn = globalThis.unlinkSync || unlinkSync;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
global.__putout_debug = debugFn;
2+
delete global.__putout_debug;
3+
const {__putout_debug} = global;
4+
const fn = global.unlinkSync || unlinkSync;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
});

0 commit comments

Comments
 (0)