Skip to content

Commit

Permalink
lib: harden lint checks for globals
Browse files Browse the repository at this point in the history
PR-URL: #38419
Backport-PR-URL: #39448
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
  • Loading branch information
aduh95 authored and richardlau committed Jul 20, 2021
1 parent d4f96bb commit e3223ed
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 9 deletions.
53 changes: 51 additions & 2 deletions lib/.eslintrc.yaml
Expand Up @@ -27,10 +27,58 @@ 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
Expand Down Expand Up @@ -73,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
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
11 changes: 10 additions & 1 deletion lib/internal/freeze_intrinsics.js
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
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
Expand Up @@ -12,6 +12,7 @@ const {
ObjectDefineProperty,
ObjectPrototypeHasOwnProperty,
Promise,
Proxy,
ReflectApply,
ReflectGet,
ReflectGetPrototypeOf,
Expand Down
1 change: 1 addition & 0 deletions lib/internal/main/worker_thread.js
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
Expand Up @@ -44,6 +44,7 @@ const {
ObjectPrototype,
ObjectPrototypeHasOwnProperty,
ObjectSetPrototypeOf,
Proxy,
ReflectApply,
ReflectSet,
RegExpPrototypeTest,
Expand Down
4 changes: 2 additions & 2 deletions lib/internal/modules/esm/translators.js
@@ -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
4 changes: 3 additions & 1 deletion lib/internal/per_context/primordials.js
Expand Up @@ -173,6 +173,7 @@ primordials.SafeWeakSet = makeSafe(

// Create copies of configurable value properties of the global object
[
'Proxy',
'globalThis',
].forEach((name) => {
// eslint-disable-next-line no-restricted-globals
Expand All @@ -193,7 +194,8 @@ primordials.SafeWeakSet = makeSafe(
[
'JSON',
'Math',
'Reflect'
'Proxy',
'Reflect',
].forEach((name) => {
// eslint-disable-next-line no-restricted-globals
copyPropsRenamed(global[name], primordials, name);
Expand Down
3 changes: 2 additions & 1 deletion lib/internal/v8_prof_polyfill.js
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 = { // eslint-disable-line no-unused-vars
system: function(name, args) {
if (process.platform === 'linux' && name === 'nm') {
Expand Down
3 changes: 1 addition & 2 deletions lib/internal/worker.js
@@ -1,7 +1,5 @@
'use strict';

/* global SharedArrayBuffer */

const {
ArrayIsArray,
ArrayPrototypeMap,
Expand All @@ -20,6 +18,7 @@ const {
SymbolFor,
TypedArrayPrototypeFill,
Uint32Array,
globalThis: { Atomics, SharedArrayBuffer },
} = primordials;

const EventEmitter = require('events');
Expand Down

0 comments on commit e3223ed

Please sign in to comment.