Skip to content

Commit e3223ed

Browse files
aduh95richardlau
authored andcommitted
lib: harden lint checks for globals
PR-URL: #38419 Backport-PR-URL: #39448 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Darshan Sen <raisinten@gmail.com>
1 parent d4f96bb commit e3223ed

11 files changed

+74
-9
lines changed

lib/.eslintrc.yaml

+51-2
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,58 @@ 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
@@ -73,6 +121,7 @@ rules:
73121
into: Number
74122
- name: parseInt
75123
into: Number
124+
- name: Proxy
76125
- name: Promise
77126
- name: RangeError
78127
- name: ReferenceError

lib/child_process.js

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ let debug = require('internal/util/debuglog').debuglog(
4343
debug = fn;
4444
}
4545
);
46+
const { AbortController } = require('internal/abort_controller');
4647
const { Buffer } = require('buffer');
4748
const { Pipe, constants: PipeConstants } = internalBinding('pipe_wrap');
4849

lib/internal/freeze_intrinsics.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
// https://github.com/google/caja/blob/HEAD/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 {
@@ -73,6 +73,7 @@ const {
7373
ObjectPrototypeHasOwnProperty,
7474
Promise,
7575
PromisePrototype,
76+
Proxy,
7677
RangeError,
7778
RangeErrorPrototype,
7879
ReferenceError,
@@ -111,8 +112,16 @@ const {
111112
decodeURIComponent,
112113
encodeURI,
113114
encodeURIComponent,
115+
globalThis,
114116
} = primordials;
115117

118+
const {
119+
Atomics,
120+
Intl,
121+
SharedArrayBuffer,
122+
WebAssembly
123+
} = globalThis;
124+
116125
module.exports = function() {
117126
const {
118127
clearImmediate,

lib/internal/http2/compat.js

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const {
77
ObjectCreate,
88
ObjectKeys,
99
ObjectPrototypeHasOwnProperty,
10+
Proxy,
1011
ReflectGetPrototypeOf,
1112
Symbol,
1213
} = primordials;

lib/internal/http2/core.js

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const {
1212
ObjectDefineProperty,
1313
ObjectPrototypeHasOwnProperty,
1414
Promise,
15+
Proxy,
1516
ReflectApply,
1617
ReflectGet,
1718
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
@@ -44,6 +44,7 @@ const {
4444
ObjectPrototype,
4545
ObjectPrototypeHasOwnProperty,
4646
ObjectSetPrototypeOf,
47+
Proxy,
4748
ReflectApply,
4849
ReflectSet,
4950
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
ArrayPrototypeMap,
75
Boolean,
@@ -19,6 +17,7 @@ const {
1917
StringPrototypeSplit,
2018
StringPrototypeStartsWith,
2119
SyntaxErrorPrototype,
20+
globalThis: { WebAssembly },
2221
} = primordials;
2322

2423
let _TYPES = null;
@@ -61,6 +60,7 @@ const experimentalImportMetaResolve =
6160
getOptionValue('--experimental-import-meta-resolve');
6261
const asyncESM = require('internal/process/esm_loader');
6362
const { emitWarningSync } = require('internal/process/warning');
63+
const { TextDecoder } = require('internal/encoding');
6464

6565
let cjsParse;
6666
async function initCJSParse() {

lib/internal/per_context/primordials.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ primordials.SafeWeakSet = makeSafe(
173173

174174
// Create copies of configurable value properties of the global object
175175
[
176+
'Proxy',
176177
'globalThis',
177178
].forEach((name) => {
178179
// eslint-disable-next-line no-restricted-globals
@@ -193,7 +194,8 @@ primordials.SafeWeakSet = makeSafe(
193194
[
194195
'JSON',
195196
'Math',
196-
'Reflect'
197+
'Proxy',
198+
'Reflect',
197199
].forEach((name) => {
198200
// eslint-disable-next-line no-restricted-globals
199201
copyPropsRenamed(global[name], primordials, name);

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 = { // eslint-disable-line no-unused-vars
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
ArrayPrototypeMap,
@@ -20,6 +18,7 @@ const {
2018
SymbolFor,
2119
TypedArrayPrototypeFill,
2220
Uint32Array,
21+
globalThis: { Atomics, SharedArrayBuffer },
2322
} = primordials;
2423

2524
const EventEmitter = require('events');

0 commit comments

Comments
 (0)