Skip to content

Commit fcac2e0

Browse files
aduh95targos
authored andcommitted
lib: harden lint checks for globals
PR-URL: #38419 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Darshan Sen <[email protected]>
1 parent 9c06103 commit fcac2e0

File tree

14 files changed

+77
-9
lines changed

14 files changed

+77
-9
lines changed

lib/.eslintrc.yaml

+52-2
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,65 @@ rules:
2727
message: "Use 'overrideStackTrace' from 'lib/internal/errors.js' instead of 'Error.prepareStackTrace'."
2828
no-restricted-globals:
2929
- error
30-
- name: globalThis
31-
message: "Use `const { globalThis } = primordials;` instead of the global."
30+
- name: AbortController
31+
message: "Use `const { AbortController } = require('internal/abort_controller');` instead of the global."
32+
- name: AbortSignal
33+
message: "Use `const { AbortSignal } = require('internal/abort_controller');` instead of the global."
34+
# Atomics is not available in primordials because it can be
35+
# disabled with --no-harmony-atomics CLI flag.
36+
- name: Atomics
37+
message: "Use `const { Atomics } = globalThis;` instead of the global."
38+
- name: Buffer
39+
message: "Use `const { Buffer } = require('buffer');` instead of the global."
40+
- name: Event
41+
message: "Use `const { Event } = require('internal/event_target');` instead of the global."
42+
- name: EventTarget
43+
message: "Use `const { EventTarget } = require('internal/event_target');` instead of the global."
44+
# Intl is not available in primordials because it can be
45+
# disabled with --without-intl build flag.
46+
- name: Intl
47+
message: "Use `const { Intl } = globalThis;` instead of the global."
48+
- name: MessageChannel
49+
message: "Use `const { MessageChannel } = require('internal/worker/io');` instead of the global."
50+
- name: MessageEvent
51+
message: "Use `const { MessageEvent } = require('internal/worker/io');` instead of the global."
52+
- name: MessagePort
53+
message: "Use `const { MessagePort } = require('internal/worker/io');` instead of the global."
54+
# SharedArrayBuffer is not available in primordials because it can be
55+
# disabled with --no-harmony-sharedarraybuffer CLI flag.
56+
- name: SharedArrayBuffer
57+
message: "Use `const { SharedArrayBuffer } = globalThis;` instead of the global."
58+
- name: TextDecoder
59+
message: "Use `const { TextDecoder } = require('internal/encoding');` instead of the global."
60+
- name: TextEncoder
61+
message: "Use `const { TextEncoder } = require('internal/encoding');` instead of the global."
62+
- name: URL
63+
message: "Use `const { URL } = require('internal/url');` instead of the global."
64+
- name: URLSearchParams
65+
message: "Use `const { URLSearchParams } = require('internal/url');` instead of the global."
66+
# WebAssembly is not available in primordials because it can be
67+
# disabled with --jitless CLI flag.
68+
- name: WebAssembly
69+
message: "Use `const { WebAssembly } = globalThis;` instead of the global."
70+
- name: atob
71+
message: "Use `const { atob } = require('buffer');` instead of the global."
72+
- name: btoa
73+
message: "Use `const { btoa } = require('buffer');` instead of the global."
3274
- name: global
3375
message: "Use `const { globalThis } = primordials;` instead of `global`."
76+
- name: globalThis
77+
message: "Use `const { globalThis } = primordials;` instead of the global."
78+
- name: performance
79+
message: "Use `const { performance } = require('perf_hooks');` instead of the global."
80+
- name: queueMicrotask
81+
message: "Use `const { queueMicrotask } = require('internal/process/task_queues');` instead of the global."
3482
# Custom rules in tools/eslint-rules
3583
node-core/lowercase-name-for-primitive: error
3684
node-core/non-ascii-character: error
3785
node-core/no-array-destructuring: error
3886
node-core/prefer-primordials:
3987
- error
88+
- name: AggregateError
4089
- name: Array
4190
- name: ArrayBuffer
4291
- name: BigInt
@@ -76,6 +125,7 @@ rules:
76125
into: Number
77126
- name: parseInt
78127
into: Number
128+
- name: Proxy
79129
- name: Promise
80130
- name: RangeError
81131
- name: ReferenceError

lib/internal/blob.js

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ const {
2020
FixedSizeBlobCopyJob,
2121
} = internalBinding('buffer');
2222

23+
const { TextDecoder } = require('internal/encoding');
24+
2325
const {
2426
JSTransferable,
2527
kClone,

lib/internal/bootstrap/node.js

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ const {
4646
ObjectGetPrototypeOf,
4747
ObjectPreventExtensions,
4848
ObjectSetPrototypeOf,
49+
Proxy,
4950
ReflectGet,
5051
ReflectSet,
5152
SymbolToStringTag,

lib/internal/crypto/webcrypto.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const {
2626
validateString,
2727
} = require('internal/validators');
2828

29-
const { TextDecoder } = require('internal/encoding');
29+
const { TextDecoder, TextEncoder } = require('internal/encoding');
3030

3131
const {
3232
codes: {

lib/internal/freeze_intrinsics.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
// https://github.com/google/caja/blob/master/src/com/google/caja/ses/repairES5.js
2020
// https://github.com/tc39/proposal-ses/blob/e5271cc42a257a05dcae2fd94713ed2f46c08620/shim/src/freeze.js
2121

22-
/* global WebAssembly, SharedArrayBuffer, console */
22+
/* global console */
2323
'use strict';
2424

2525
const {
@@ -78,6 +78,7 @@ const {
7878
ObjectPrototypeHasOwnProperty,
7979
Promise,
8080
PromisePrototype,
81+
Proxy,
8182
RangeError,
8283
RangeErrorPrototype,
8384
ReferenceError,
@@ -124,6 +125,13 @@ const {
124125
globalThis,
125126
} = primordials;
126127

128+
const {
129+
Atomics,
130+
Intl,
131+
SharedArrayBuffer,
132+
WebAssembly
133+
} = globalThis;
134+
127135
module.exports = function() {
128136
const {
129137
clearImmediate,

lib/internal/http2/compat.js

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const {
99
ObjectCreate,
1010
ObjectKeys,
1111
ObjectPrototypeHasOwnProperty,
12+
Proxy,
1213
ReflectApply,
1314
ReflectGetPrototypeOf,
1415
StringPrototypeIncludes,

lib/internal/http2/core.js

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const {
1818
ObjectPrototypeHasOwnProperty,
1919
Promise,
2020
PromisePrototypeCatch,
21+
Proxy,
2122
ReflectApply,
2223
ReflectGet,
2324
ReflectGetPrototypeOf,

lib/internal/main/worker_thread.js

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const {
99
ArrayPrototypeSplice,
1010
ObjectDefineProperty,
1111
PromisePrototypeCatch,
12+
globalThis: { Atomics },
1213
} = primordials;
1314

1415
const {

lib/internal/modules/cjs/loader.js

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ const {
4545
ObjectPrototype,
4646
ObjectPrototypeHasOwnProperty,
4747
ObjectSetPrototypeOf,
48+
Proxy,
4849
ReflectApply,
4950
ReflectSet,
5051
RegExpPrototypeTest,

lib/internal/modules/esm/translators.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
'use strict';
22

3-
/* global WebAssembly */
4-
53
const {
64
ArrayPrototypeForEach,
75
ArrayPrototypeMap,
@@ -21,6 +19,7 @@ const {
2119
StringPrototypeSplit,
2220
StringPrototypeStartsWith,
2321
SyntaxErrorPrototype,
22+
globalThis: { WebAssembly },
2423
} = primordials;
2524

2625
let _TYPES = null;
@@ -63,6 +62,7 @@ const experimentalImportMetaResolve =
6362
getOptionValue('--experimental-import-meta-resolve');
6463
const asyncESM = require('internal/process/esm_loader');
6564
const { emitWarningSync } = require('internal/process/warning');
65+
const { TextDecoder } = require('internal/encoding');
6666

6767
let cjsParse;
6868
async function initCJSParse() {

lib/internal/per_context/primordials.js

+2
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ function copyPrototype(src, dest, prefix) {
137137

138138
// Create copies of configurable value properties of the global object
139139
[
140+
'Proxy',
140141
'globalThis',
141142
].forEach((name) => {
142143
// eslint-disable-next-line no-restricted-globals
@@ -157,6 +158,7 @@ function copyPrototype(src, dest, prefix) {
157158
[
158159
'JSON',
159160
'Math',
161+
'Proxy',
160162
'Reflect',
161163
].forEach((name) => {
162164
// eslint-disable-next-line no-restricted-globals

lib/internal/v8_prof_polyfill.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
'use strict';
2929

3030
/* eslint-disable node-core/prefer-primordials */
31-
/* global Buffer, console */
31+
/* global console */
3232

3333
module.exports = { versionCheck };
3434

@@ -40,6 +40,7 @@ if (module.id === 'internal/v8_prof_polyfill') return;
4040
// Node polyfill
4141
const fs = require('fs');
4242
const cp = require('child_process');
43+
const { Buffer } = require('buffer');
4344
const os = {
4445
system: function(name, args) {
4546
if (process.platform === 'linux' && name === 'nm') {

lib/internal/worker.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
'use strict';
22

3-
/* global SharedArrayBuffer */
4-
53
const {
64
ArrayIsArray,
75
ArrayPrototypeForEach,
@@ -24,6 +22,7 @@ const {
2422
SymbolFor,
2523
TypedArrayPrototypeFill,
2624
Uint32Array,
25+
globalThis: { Atomics, SharedArrayBuffer },
2726
} = primordials;
2827

2928
const EventEmitter = require('events');

lib/internal/worker/io.js

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ const {
4141
const { Readable, Writable } = require('stream');
4242
const {
4343
Event,
44+
EventTarget,
4445
NodeEventTarget,
4546
defineEventHandler,
4647
initNodeEventTarget,

0 commit comments

Comments
 (0)