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

lib: harden lint checks for globals #38419

Closed
wants to merge 2 commits into from
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 52 additions & 2 deletions lib/.eslintrc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,65 @@ rules:
message: "Use 'overrideStackTrace' from 'lib/internal/errors.js' instead of 'Error.prepareStackTrace'."
no-restricted-globals:
- error
- name: globalThis
message: "Use `const { globalThis } = primordials;` instead of the global."
- 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
node-core/no-array-destructuring: error
node-core/prefer-primordials:
- error
- name: AggregateError
- name: Array
- name: ArrayBuffer
- name: BigInt
Expand Down Expand Up @@ -76,6 +125,7 @@ rules:
into: Number
- name: parseInt
into: Number
- name: Proxy
- name: Promise
- name: RangeError
- name: ReferenceError
Expand Down
2 changes: 2 additions & 0 deletions lib/internal/blob.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ const {
FixedSizeBlobCopyJob,
} = internalBinding('buffer');

const { TextDecoder } = require('internal/encoding');

const {
JSTransferable,
kClone,
Expand Down
1 change: 1 addition & 0 deletions lib/internal/bootstrap/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const {
ObjectGetPrototypeOf,
ObjectPreventExtensions,
ObjectSetPrototypeOf,
Proxy,
ReflectGet,
ReflectSet,
SymbolToStringTag,
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/crypto/webcrypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const {
validateString,
} = require('internal/validators');

const { TextDecoder } = require('internal/encoding');
const { TextDecoder, TextEncoder } = require('internal/encoding');

const {
codes: {
Expand Down
10 changes: 9 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/master/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 @@ -78,6 +78,7 @@ const {
ObjectPrototypeHasOwnProperty,
Promise,
PromisePrototype,
Proxy,
RangeError,
RangeErrorPrototype,
ReferenceError,
Expand Down Expand Up @@ -124,6 +125,13 @@ const {
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 @@ -9,6 +9,7 @@ const {
ObjectCreate,
ObjectKeys,
ObjectPrototypeHasOwnProperty,
Proxy,
ReflectApply,
ReflectGetPrototypeOf,
StringPrototypeIncludes,
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 @@ -18,6 +18,7 @@ const {
ObjectPrototypeHasOwnProperty,
Promise,
PromisePrototypeCatch,
Proxy,
ReflectApply,
ReflectGet,
ReflectGetPrototypeOf,
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 @@ -45,6 +45,7 @@ const {
ObjectPrototype,
ObjectPrototypeHasOwnProperty,
ObjectSetPrototypeOf,
Proxy,
ReflectApply,
ReflectSet,
RegExpPrototypeTest,
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 {
ArrayPrototypeForEach,
ArrayPrototypeMap,
Expand All @@ -21,6 +19,7 @@ const {
StringPrototypeSplit,
StringPrototypeStartsWith,
SyntaxErrorPrototype,
globalThis: { WebAssembly },
} = primordials;

let _TYPES = null;
Expand Down Expand Up @@ -63,6 +62,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
2 changes: 2 additions & 0 deletions lib/internal/per_context/primordials.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ function copyPrototype(src, dest, prefix) {

// Create copies of configurable value properties of the global object
[
'Proxy',
'globalThis',
].forEach((name) => {
// eslint-disable-next-line no-restricted-globals
Expand All @@ -157,6 +158,7 @@ function copyPrototype(src, dest, prefix) {
[
'JSON',
'Math',
'Proxy',
'Reflect',
].forEach((name) => {
// eslint-disable-next-line no-restricted-globals
Expand Down
3 changes: 2 additions & 1 deletion lib/internal/v8_prof_polyfill.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
'use strict';

/* eslint-disable node-core/prefer-primordials */
/* global Buffer, console */
/* global console */

module.exports = { versionCheck };

Expand All @@ -40,6 +40,7 @@ if (module.id === 'internal/v8_prof_polyfill') return;
// Node polyfill
const fs = require('fs');
const cp = require('child_process');
const { Buffer } = require('buffer');
const os = {
system: function(name, args) {
if (process.platform === 'linux' && name === 'nm') {
Expand Down
3 changes: 1 addition & 2 deletions lib/internal/worker.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict';

/* global SharedArrayBuffer */

const {
ArrayIsArray,
ArrayPrototypeForEach,
Expand All @@ -24,6 +22,7 @@ const {
SymbolFor,
TypedArrayPrototypeFill,
Uint32Array,
globalThis: { Atomics, SharedArrayBuffer },
} = primordials;

const EventEmitter = require('events');
Expand Down
1 change: 1 addition & 0 deletions lib/internal/worker/io.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const {
const { Readable, Writable } = require('stream');
const {
Event,
EventTarget,
NodeEventTarget,
defineEventHandler,
initNodeEventTarget,
Expand Down