Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v14.x backport] lint rules for globals #39448

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
55 changes: 55 additions & 0 deletions lib/.eslintrc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,60 @@ rules:
message: "Please use `require('internal/errors').hideStackFrames()` instead."
- selector: "AssignmentExpression:matches([left.name='prepareStackTrace'], [left.property.name='prepareStackTrace'])"
message: "Use 'overrideStackTrace' from 'lib/internal/errors.js' instead of 'Error.prepareStackTrace'."
no-restricted-globals:
- error
- name: AbortController
message: "Use `const { AbortController } = require('internal/abort_controller');` instead of the global."
- name: AbortSignal
message: "Use `const { AbortSignal } = require('internal/abort_controller');` instead of the global."
# Atomics is not available in primordials because it can be
# disabled with --no-harmony-atomics CLI flag.
- name: Atomics
message: "Use `const { Atomics } = globalThis;` instead of the global."
- name: Buffer
message: "Use `const { Buffer } = require('buffer');` instead of the global."
- name: Event
message: "Use `const { Event } = require('internal/event_target');` instead of the global."
- name: EventTarget
message: "Use `const { EventTarget } = require('internal/event_target');` instead of the global."
# Intl is not available in primordials because it can be
# disabled with --without-intl build flag.
- name: Intl
message: "Use `const { Intl } = globalThis;` instead of the global."
- name: MessageChannel
message: "Use `const { MessageChannel } = require('internal/worker/io');` instead of the global."
- name: MessageEvent
message: "Use `const { MessageEvent } = require('internal/worker/io');` instead of the global."
- name: MessagePort
message: "Use `const { MessagePort } = require('internal/worker/io');` instead of the global."
# SharedArrayBuffer is not available in primordials because it can be
# disabled with --no-harmony-sharedarraybuffer CLI flag.
- name: SharedArrayBuffer
message: "Use `const { SharedArrayBuffer } = globalThis;` instead of the global."
- name: TextDecoder
message: "Use `const { TextDecoder } = require('internal/encoding');` instead of the global."
- name: TextEncoder
message: "Use `const { TextEncoder } = require('internal/encoding');` instead of the global."
- name: URL
message: "Use `const { URL } = require('internal/url');` instead of the global."
- name: URLSearchParams
message: "Use `const { URLSearchParams } = require('internal/url');` instead of the global."
# WebAssembly is not available in primordials because it can be
# disabled with --jitless CLI flag.
- name: WebAssembly
message: "Use `const { WebAssembly } = globalThis;` instead of the global."
- name: atob
message: "Use `const { atob } = require('buffer');` instead of the global."
- name: btoa
message: "Use `const { btoa } = require('buffer');` instead of the global."
- name: global
message: "Use `const { globalThis } = primordials;` instead of `global`."
- name: globalThis
message: "Use `const { globalThis } = primordials;` instead of the global."
- name: performance
message: "Use `const { performance } = require('perf_hooks');` instead of the global."
- name: queueMicrotask
message: "Use `const { queueMicrotask } = require('internal/process/task_queues');` instead of the global."
# Custom rules in tools/eslint-rules
node-core/lowercase-name-for-primitive: error
node-core/non-ascii-character: error
Expand Down Expand Up @@ -67,6 +121,7 @@ rules:
into: Number
- name: parseInt
into: Number
- name: Proxy
- name: Promise
- name: RangeError
- name: ReferenceError
Expand Down
1 change: 1 addition & 0 deletions lib/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ let debug = require('internal/util/debuglog').debuglog(
debug = fn;
}
);
const { AbortController } = require('internal/abort_controller');
const { Buffer } = require('buffer');
const { Pipe, constants: PipeConstants } = internalBinding('pipe_wrap');

Expand Down
32 changes: 17 additions & 15 deletions lib/internal/bootstrap/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const {
ObjectGetPrototypeOf,
ObjectSetPrototypeOf,
SymbolToStringTag,
globalThis,
} = primordials;
const config = internalBinding('config');
const { deprecate } = require('internal/util');
Expand Down Expand Up @@ -119,34 +120,35 @@ if (!config.noBrowserGlobals) {
// Override global console from the one provided by the VM
// to the one implemented by Node.js
// https://console.spec.whatwg.org/#console-namespace
exposeNamespace(global, 'console', createGlobalConsole(global.console));
exposeNamespace(globalThis, 'console',
createGlobalConsole(globalThis.console));

const { URL, URLSearchParams } = require('internal/url');
// https://url.spec.whatwg.org/#url
exposeInterface(global, 'URL', URL);
exposeInterface(globalThis, 'URL', URL);
// https://url.spec.whatwg.org/#urlsearchparams
exposeInterface(global, 'URLSearchParams', URLSearchParams);
exposeInterface(globalThis, 'URLSearchParams', URLSearchParams);

const {
TextEncoder, TextDecoder
} = require('internal/encoding');
// https://encoding.spec.whatwg.org/#textencoder
exposeInterface(global, 'TextEncoder', TextEncoder);
exposeInterface(globalThis, 'TextEncoder', TextEncoder);
// https://encoding.spec.whatwg.org/#textdecoder
exposeInterface(global, 'TextDecoder', TextDecoder);
exposeInterface(globalThis, 'TextDecoder', TextDecoder);

// https://html.spec.whatwg.org/multipage/webappapis.html#windoworworkerglobalscope
const timers = require('timers');
defineOperation(global, 'clearInterval', timers.clearInterval);
defineOperation(global, 'clearTimeout', timers.clearTimeout);
defineOperation(global, 'setInterval', timers.setInterval);
defineOperation(global, 'setTimeout', timers.setTimeout);
defineOperation(globalThis, 'clearInterval', timers.clearInterval);
defineOperation(globalThis, 'clearTimeout', timers.clearTimeout);
defineOperation(globalThis, 'setInterval', timers.setInterval);
defineOperation(globalThis, 'setTimeout', timers.setTimeout);

defineOperation(global, 'queueMicrotask', queueMicrotask);
defineOperation(globalThis, 'queueMicrotask', queueMicrotask);

// Non-standard extensions:
defineOperation(global, 'clearImmediate', timers.clearImmediate);
defineOperation(global, 'setImmediate', timers.setImmediate);
defineOperation(globalThis, 'clearImmediate', timers.clearImmediate);
defineOperation(globalThis, 'setImmediate', timers.setImmediate);
}

// Set the per-Environment callback that will be called
Expand Down Expand Up @@ -280,7 +282,7 @@ function setupProcessObject() {
value: 'process'
});
// Make process globally available to users by putting it on the global proxy
ObjectDefineProperty(global, 'process', {
ObjectDefineProperty(globalThis, 'process', {
value: process,
enumerable: false,
writable: true,
Expand All @@ -289,7 +291,7 @@ function setupProcessObject() {
}

function setupGlobalProxy() {
ObjectDefineProperty(global, SymbolToStringTag, {
ObjectDefineProperty(globalThis, SymbolToStringTag, {
value: 'global',
writable: false,
enumerable: false,
Expand All @@ -306,7 +308,7 @@ function setupBuffer() {
delete bufferBinding.setBufferPrototype;
delete bufferBinding.zeroFill;

ObjectDefineProperty(global, 'Buffer', {
ObjectDefineProperty(globalThis, 'Buffer', {
value: Buffer,
enumerable: false,
writable: true,
Expand Down
7 changes: 4 additions & 3 deletions lib/internal/bootstrap/pre_execution.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const {
SafeMap,
SafeWeakMap,
StringPrototypeStartsWith,
globalThis,
} = primordials;

const {
Expand Down Expand Up @@ -290,7 +291,7 @@ function initializeDeprecations() {
// deprecation path for these in ES Modules.
// See https://github.com/nodejs/node/pull/26334.
let _process = process;
ObjectDefineProperty(global, 'process', {
ObjectDefineProperty(globalThis, 'process', {
get() {
return _process;
},
Expand All @@ -302,7 +303,7 @@ function initializeDeprecations() {
});

let _Buffer = Buffer;
ObjectDefineProperty(global, 'Buffer', {
ObjectDefineProperty(globalThis, 'Buffer', {
get() {
return _Buffer;
},
Expand All @@ -321,7 +322,7 @@ function initializeAbortController() {
AbortController,
AbortSignal
} = require('internal/abort_controller');
ObjectDefineProperties(global, {
ObjectDefineProperties(globalThis, {
AbortController: {
writable: true,
enumerable: false,
Expand Down
11 changes: 10 additions & 1 deletion lib/internal/freeze_intrinsics.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
// https://github.com/google/caja/blob/HEAD/src/com/google/caja/ses/repairES5.js
// https://github.com/tc39/proposal-ses/blob/e5271cc42a257a05dcae2fd94713ed2f46c08620/shim/src/freeze.js

/* global WebAssembly, SharedArrayBuffer, console */
/* global console */
'use strict';

const {
Expand Down Expand Up @@ -73,6 +73,7 @@ const {
ObjectPrototypeHasOwnProperty,
Promise,
PromisePrototype,
Proxy,
RangeError,
RangeErrorPrototype,
ReferenceError,
Expand Down Expand Up @@ -111,8 +112,16 @@ const {
decodeURIComponent,
encodeURI,
encodeURIComponent,
globalThis,
} = primordials;

const {
Atomics,
Intl,
SharedArrayBuffer,
WebAssembly
} = globalThis;

module.exports = function() {
const {
clearImmediate,
Expand Down
1 change: 1 addition & 0 deletions lib/internal/http2/compat.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const {
ObjectCreate,
ObjectKeys,
ObjectPrototypeHasOwnProperty,
Proxy,
ReflectGetPrototypeOf,
Symbol,
} = primordials;
Expand Down
1 change: 1 addition & 0 deletions lib/internal/http2/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const {
ObjectDefineProperty,
ObjectPrototypeHasOwnProperty,
Promise,
Proxy,
ReflectApply,
ReflectGet,
ReflectGetPrototypeOf,
Expand Down
6 changes: 5 additions & 1 deletion lib/internal/main/eval_string.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
// User passed `-e` or `--eval` arguments to Node without `-i` or
// `--interactive`.

const {
globalThis,
} = primordials;

const {
prepareMainThreadExecution
} = require('internal/bootstrap/pre_execution');
Expand All @@ -12,7 +16,7 @@ const { addBuiltinLibsToObject } = require('internal/modules/cjs/helpers');
const { getOptionValue } = require('internal/options');

prepareMainThreadExecution();
addBuiltinLibsToObject(global);
addBuiltinLibsToObject(globalThis);
markBootstrapComplete();

const source = getOptionValue('--eval');
Expand Down
1 change: 1 addition & 0 deletions lib/internal/main/worker_thread.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const {
ArrayPrototypeSplice,
ObjectDefineProperty,
PromisePrototypeCatch,
globalThis: { Atomics },
} = primordials;

const {
Expand Down
1 change: 1 addition & 0 deletions lib/internal/modules/cjs/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const {
ObjectPrototype,
ObjectPrototypeHasOwnProperty,
ObjectSetPrototypeOf,
Proxy,
ReflectApply,
ReflectSet,
RegExpPrototypeTest,
Expand Down
1 change: 1 addition & 0 deletions lib/internal/modules/esm/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const {
RegExpPrototypeExec,
SafeWeakMap,
StringPrototypeStartsWith,
globalThis,
} = primordials;

const {
Expand Down
4 changes: 2 additions & 2 deletions lib/internal/modules/esm/translators.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict';

/* global WebAssembly */

const {
ArrayPrototypeMap,
Boolean,
Expand All @@ -19,6 +17,7 @@ const {
StringPrototypeSplit,
StringPrototypeStartsWith,
SyntaxErrorPrototype,
globalThis: { WebAssembly },
} = primordials;

let _TYPES = null;
Expand Down Expand Up @@ -61,6 +60,7 @@ const experimentalImportMetaResolve =
getOptionValue('--experimental-import-meta-resolve');
const asyncESM = require('internal/process/esm_loader');
const { emitWarningSync } = require('internal/process/warning');
const { TextDecoder } = require('internal/encoding');

let cjsParse;
async function initCJSParse() {
Expand Down
15 changes: 14 additions & 1 deletion lib/internal/per_context/primordials.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,15 @@ primordials.SafeWeakSet = makeSafe(
}
);

// Create copies of configurable value properties of the global object
[
'Proxy',
'globalThis',
].forEach((name) => {
// eslint-disable-next-line no-restricted-globals
primordials[name] = globalThis[name];
});

// Create copies of URI handling functions
[
decodeURI,
Expand All @@ -185,8 +194,10 @@ primordials.SafeWeakSet = makeSafe(
[
'JSON',
'Math',
'Reflect'
'Proxy',
'Reflect',
].forEach((name) => {
// eslint-disable-next-line no-restricted-globals
copyPropsRenamed(global[name], primordials, name);
});

Expand Down Expand Up @@ -227,6 +238,7 @@ primordials.SafeWeakSet = makeSafe(
'WeakMap',
'WeakSet',
].forEach((name) => {
// eslint-disable-next-line no-restricted-globals
const original = global[name];
primordials[name] = original;
copyPropsRenamed(original, primordials, name);
Expand All @@ -239,6 +251,7 @@ primordials.SafeWeakSet = makeSafe(
[
'Promise',
].forEach((name) => {
// eslint-disable-next-line no-restricted-globals
const original = global[name];
primordials[name] = original;
copyPropsRenamedBound(original, primordials, name);
Expand Down
4 changes: 4 additions & 0 deletions lib/internal/process/execution.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
'use strict';

const {
globalThis,
} = primordials;

const path = require('path');

const {
Expand Down
4 changes: 4 additions & 0 deletions lib/internal/test/binding.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
'use strict';

const {
globalThis,
} = primordials;

process.emitWarning(
'These APIs are for internal testing only. Do not use them.',
'internal/test/binding');
Expand Down