From 703f154baee78487651698ee9d6d973aa81781d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Zasso?= Date: Sun, 31 Mar 2019 13:30:12 +0200 Subject: [PATCH] lib: force using primordials for JSON, Math and Reflect Use the "no-restricted-globals" ESLint rule to lint for it. PR-URL: https://github.com/nodejs/node/pull/27027 Reviewed-By: Anna Henningsen Reviewed-By: Joyee Cheung Reviewed-By: Colin Ihrig Reviewed-By: Ruben Bridgewater Reviewed-By: Gus Caplan Reviewed-By: Luigi Pinca --- lib/.eslintrc.yaml | 1 + lib/_http_common.js | 2 ++ lib/async_hooks.js | 2 ++ lib/buffer.js | 2 ++ lib/domain.js | 2 ++ lib/events.js | 2 ++ lib/fs.js | 2 ++ lib/inspector.js | 2 ++ lib/internal/assert/assertion_error.js | 2 ++ lib/internal/async_hooks.js | 2 ++ lib/internal/bootstrap/node.js | 3 +-- lib/internal/bootstrap/primordials.js | 2 +- lib/internal/buffer.js | 2 ++ lib/internal/child_process.js | 2 ++ lib/internal/cli_table.js | 2 ++ lib/internal/console/constructor.js | 2 ++ lib/internal/console/global.js | 2 ++ lib/internal/crypto/random.js | 2 ++ lib/internal/freeze_intrinsics.js | 2 ++ lib/internal/fs/promises.js | 2 ++ lib/internal/fs/read_file_context.js | 2 ++ lib/internal/fs/streams.js | 2 ++ lib/internal/fs/utils.js | 2 ++ lib/internal/http2/compat.js | 2 ++ lib/internal/http2/core.js | 2 ++ lib/internal/http2/util.js | 2 ++ lib/internal/main/print_help.js | 2 ++ lib/internal/modules/cjs/loader.js | 2 ++ lib/internal/process/execution.js | 2 ++ lib/internal/process/policy.js | 2 ++ lib/internal/process/task_queues.js | 2 ++ lib/internal/profiler.js | 2 ++ lib/internal/streams/state.js | 2 ++ lib/internal/timers.js | 2 ++ lib/internal/url.js | 2 ++ lib/internal/util.js | 2 ++ lib/internal/util/inspect.js | 2 ++ lib/internal/v8_prof_processor.js | 2 ++ lib/readline.js | 2 ++ lib/repl.js | 2 ++ lib/timers.js | 2 ++ lib/util.js | 2 ++ lib/zlib.js | 2 ++ 43 files changed, 83 insertions(+), 3 deletions(-) diff --git a/lib/.eslintrc.yaml b/lib/.eslintrc.yaml index 0e869e0958de12..029d7ad177f468 100644 --- a/lib/.eslintrc.yaml +++ b/lib/.eslintrc.yaml @@ -1,6 +1,7 @@ rules: prefer-object-spread: error no-buffer-constructor: error + no-restricted-globals: ["error", "JSON", "Math", "Reflect"] no-restricted-syntax: # Config copied from .eslintrc.js - error diff --git a/lib/_http_common.js b/lib/_http_common.js index bf885161571be1..a6efa5a832aeb5 100644 --- a/lib/_http_common.js +++ b/lib/_http_common.js @@ -21,6 +21,8 @@ 'use strict'; +const { Math } = primordials; + const { getOptionValue } = require('internal/options'); const { methods, HTTPParser } = diff --git a/lib/async_hooks.js b/lib/async_hooks.js index 107c92c97d670c..88b8fd705a0efa 100644 --- a/lib/async_hooks.js +++ b/lib/async_hooks.js @@ -1,5 +1,7 @@ 'use strict'; +const { Reflect } = primordials; + const { ERR_ASYNC_CALLBACK, ERR_INVALID_ASYNC_ID diff --git a/lib/buffer.js b/lib/buffer.js index b5d89625b2bc69..3cc9e77a6c926a 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -21,6 +21,8 @@ 'use strict'; +const { Math } = primordials; + const { byteLengthUtf8, copy: _copy, diff --git a/lib/domain.js b/lib/domain.js index 27d72eb20c8dfb..608d5dacebee9a 100644 --- a/lib/domain.js +++ b/lib/domain.js @@ -26,6 +26,8 @@ // No new pull requests targeting this module will be accepted // unless they address existing, critical bugs. +const { Reflect } = primordials; + const util = require('util'); const EventEmitter = require('events'); const { diff --git a/lib/events.js b/lib/events.js index 1eb4fc50f61997..847074206e2056 100644 --- a/lib/events.js +++ b/lib/events.js @@ -21,6 +21,8 @@ 'use strict'; +const { Math, Reflect } = primordials; + var spliceOne; const { diff --git a/lib/fs.js b/lib/fs.js index b27b7f8916657a..fbdddb2f8ceb32 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -24,6 +24,8 @@ 'use strict'; +const { Math, Reflect } = primordials; + const { fs: constants } = internalBinding('constants'); const { S_IFIFO, diff --git a/lib/inspector.js b/lib/inspector.js index 43cdd53bf0a4b8..793e63ff3942aa 100644 --- a/lib/inspector.js +++ b/lib/inspector.js @@ -1,5 +1,7 @@ 'use strict'; +const { JSON } = primordials; + const { ERR_INSPECTOR_ALREADY_CONNECTED, ERR_INSPECTOR_CLOSED, diff --git a/lib/internal/assert/assertion_error.js b/lib/internal/assert/assertion_error.js index 18c6e8cf99e66a..5cefbf568fddb4 100644 --- a/lib/internal/assert/assertion_error.js +++ b/lib/internal/assert/assertion_error.js @@ -1,5 +1,7 @@ 'use strict'; +const { Math } = primordials; + const { inspect } = require('internal/util/inspect'); const { codes: { ERR_INVALID_ARG_TYPE diff --git a/lib/internal/async_hooks.js b/lib/internal/async_hooks.js index fac887d0a5daac..a9bf1a49ac01ad 100644 --- a/lib/internal/async_hooks.js +++ b/lib/internal/async_hooks.js @@ -1,5 +1,7 @@ 'use strict'; +const { Reflect } = primordials; + const { ERR_ASYNC_TYPE, ERR_INVALID_ASYNC_ID diff --git a/lib/internal/bootstrap/node.js b/lib/internal/bootstrap/node.js index abd88afce05a06..a94dcfc4d12302 100644 --- a/lib/internal/bootstrap/node.js +++ b/lib/internal/bootstrap/node.js @@ -34,9 +34,8 @@ // This file is compiled as if it's wrapped in a function with arguments // passed by node::RunBootstrapping() /* global process, require, internalBinding, isMainThread, ownsProcessState */ -/* global primordials */ -const { Object, Symbol } = primordials; +const { JSON, Object, Symbol } = primordials; const config = internalBinding('config'); const { deprecate } = require('internal/util'); diff --git a/lib/internal/bootstrap/primordials.js b/lib/internal/bootstrap/primordials.js index df399dc1736a54..e1fef7f8860b2d 100644 --- a/lib/internal/bootstrap/primordials.js +++ b/lib/internal/bootstrap/primordials.js @@ -1,6 +1,6 @@ 'use strict'; -/* global primordials */ +/* eslint-disable no-restricted-globals */ // This file subclasses and stores the JS builtins that come from the VM // so that Node.js's builtin modules do not need to later look these up from diff --git a/lib/internal/buffer.js b/lib/internal/buffer.js index 56b73f5489ec35..64c026002cc189 100644 --- a/lib/internal/buffer.js +++ b/lib/internal/buffer.js @@ -1,5 +1,7 @@ 'use strict'; +const { Math } = primordials; + const { ERR_BUFFER_OUT_OF_BOUNDS, ERR_INVALID_ARG_TYPE, diff --git a/lib/internal/child_process.js b/lib/internal/child_process.js index 840a1f16e04da9..86736b1bd80132 100644 --- a/lib/internal/child_process.js +++ b/lib/internal/child_process.js @@ -1,5 +1,7 @@ 'use strict'; +const { JSON } = primordials; + const { errnoException, codes: { diff --git a/lib/internal/cli_table.js b/lib/internal/cli_table.js index 71d322ee9eeed1..b2d2779e5f9cdc 100644 --- a/lib/internal/cli_table.js +++ b/lib/internal/cli_table.js @@ -1,5 +1,7 @@ 'use strict'; +const { Math } = primordials; + const { Buffer } = require('buffer'); const { removeColors } = require('internal/util'); const HasOwnProperty = Function.call.bind(Object.prototype.hasOwnProperty); diff --git a/lib/internal/console/constructor.js b/lib/internal/console/constructor.js index 86ef83994e9ca6..9e75dce3af1bb8 100644 --- a/lib/internal/console/constructor.js +++ b/lib/internal/console/constructor.js @@ -3,6 +3,8 @@ // The Console constructor is not actually used to construct the global // console. It's exported for backwards compatibility. +const { Reflect } = primordials; + const { trace } = internalBinding('trace_events'); const { isStackOverflowError, diff --git a/lib/internal/console/global.js b/lib/internal/console/global.js index 614941eba6da2d..aac67fde15e253 100644 --- a/lib/internal/console/global.js +++ b/lib/internal/console/global.js @@ -12,6 +12,8 @@ // Therefore, the console.Console.prototype is not // in the global console prototype chain anymore. +const { Reflect } = primordials; + const { Console, kBindStreamsLazy, diff --git a/lib/internal/crypto/random.js b/lib/internal/crypto/random.js index d6b6f47b20ccef..d26eab7559efec 100644 --- a/lib/internal/crypto/random.js +++ b/lib/internal/crypto/random.js @@ -1,5 +1,7 @@ 'use strict'; +const { Math } = primordials; + const { AsyncWrap, Providers } = internalBinding('async_wrap'); const { Buffer, kMaxLength } = require('buffer'); const { randomBytes: _randomBytes } = internalBinding('crypto'); diff --git a/lib/internal/freeze_intrinsics.js b/lib/internal/freeze_intrinsics.js index 5d58121a25511c..ef32a8dc356c2e 100644 --- a/lib/internal/freeze_intrinsics.js +++ b/lib/internal/freeze_intrinsics.js @@ -20,7 +20,9 @@ // https://github.com/tc39/proposal-frozen-realms/blob/91ac390e3451da92b5c27e354b39e52b7636a437/shim/src/deep-freeze.js /* global WebAssembly, SharedArrayBuffer, console */ +/* eslint-disable no-restricted-globals */ 'use strict'; + module.exports = function() { const intrinsics = [ diff --git a/lib/internal/fs/promises.js b/lib/internal/fs/promises.js index 93c8fc0f889d88..ae7d616503259f 100644 --- a/lib/internal/fs/promises.js +++ b/lib/internal/fs/promises.js @@ -1,5 +1,7 @@ 'use strict'; +const { Math } = primordials; + const { F_OK, O_SYMLINK, diff --git a/lib/internal/fs/read_file_context.js b/lib/internal/fs/read_file_context.js index 4961ee820a7e84..a4e7a3563bf821 100644 --- a/lib/internal/fs/read_file_context.js +++ b/lib/internal/fs/read_file_context.js @@ -1,5 +1,7 @@ 'use strict'; +const { Math } = primordials; + const { Buffer } = require('buffer'); const { FSReqCallback, close, read } = internalBinding('fs'); diff --git a/lib/internal/fs/streams.js b/lib/internal/fs/streams.js index 24c79dffb54ee5..3b797edc582f02 100644 --- a/lib/internal/fs/streams.js +++ b/lib/internal/fs/streams.js @@ -1,5 +1,7 @@ 'use strict'; +const { Math } = primordials; + const { FSReqCallback, writeBuffers diff --git a/lib/internal/fs/utils.js b/lib/internal/fs/utils.js index 99e820d2944b69..a6157f768434bf 100644 --- a/lib/internal/fs/utils.js +++ b/lib/internal/fs/utils.js @@ -1,5 +1,7 @@ 'use strict'; +const { Reflect } = primordials; + const { Buffer, kMaxLength } = require('buffer'); const { codes: { diff --git a/lib/internal/http2/compat.js b/lib/internal/http2/compat.js index 84630a37176e8a..2ba1f497574a91 100644 --- a/lib/internal/http2/compat.js +++ b/lib/internal/http2/compat.js @@ -1,5 +1,7 @@ 'use strict'; +const { Reflect } = primordials; + const assert = require('internal/assert'); const Stream = require('stream'); const Readable = Stream.Readable; diff --git a/lib/internal/http2/core.js b/lib/internal/http2/core.js index 3c4e6b55944fde..51385156d1ad5c 100644 --- a/lib/internal/http2/core.js +++ b/lib/internal/http2/core.js @@ -2,6 +2,8 @@ /* eslint-disable no-use-before-define */ +const { Math, Reflect } = primordials; + const { assertCrypto, customInspectSymbol: kInspect, diff --git a/lib/internal/http2/util.js b/lib/internal/http2/util.js index 80422cdc8b8dc0..d22310f19d8a92 100644 --- a/lib/internal/http2/util.js +++ b/lib/internal/http2/util.js @@ -1,5 +1,7 @@ 'use strict'; +const { Math } = primordials; + const binding = internalBinding('http2'); const { codes: { diff --git a/lib/internal/main/print_help.js b/lib/internal/main/print_help.js index 451f0c13835092..406c13eeffd6e6 100644 --- a/lib/internal/main/print_help.js +++ b/lib/internal/main/print_help.js @@ -1,5 +1,7 @@ 'use strict'; +/* eslint-disable no-restricted-globals */ + const { types } = internalBinding('options'); const hasCrypto = Boolean(process.versions.openssl); diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index 03fa4220d0d2bf..100c8fb772849a 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -21,6 +21,8 @@ 'use strict'; +const { JSON, Reflect } = primordials; + const { NativeModule } = require('internal/bootstrap/loaders'); const { pathToFileURL } = require('internal/url'); const { deprecate } = require('internal/util'); diff --git a/lib/internal/process/execution.js b/lib/internal/process/execution.js index 070410ef6faf21..5712b80eafb619 100644 --- a/lib/internal/process/execution.js +++ b/lib/internal/process/execution.js @@ -1,5 +1,7 @@ 'use strict'; +const { JSON } = primordials; + const path = require('path'); const { diff --git a/lib/internal/process/policy.js b/lib/internal/process/policy.js index 823130e3c1d74c..98c535f8704f79 100644 --- a/lib/internal/process/policy.js +++ b/lib/internal/process/policy.js @@ -1,5 +1,7 @@ 'use strict'; +const { JSON, Reflect } = primordials; + const { ERR_MANIFEST_TDZ, } = require('internal/errors').codes; diff --git a/lib/internal/process/task_queues.js b/lib/internal/process/task_queues.js index 9e4fba6d9df4f0..f06a4de535ba5c 100644 --- a/lib/internal/process/task_queues.js +++ b/lib/internal/process/task_queues.js @@ -1,5 +1,7 @@ 'use strict'; +const { Reflect } = primordials; + const { // For easy access to the nextTick state in the C++ land, // and to avoid unnecessary calls into JS land. diff --git a/lib/internal/profiler.js b/lib/internal/profiler.js index 1042f126dd6e25..5e8e13038be0f2 100644 --- a/lib/internal/profiler.js +++ b/lib/internal/profiler.js @@ -3,6 +3,8 @@ // Implements coverage collection exposed by the `NODE_V8_COVERAGE` // environment variable which can also be used in the user land. +const { JSON } = primordials; + let coverageDirectory; function writeCoverage() { diff --git a/lib/internal/streams/state.js b/lib/internal/streams/state.js index c54c8230c72227..c2fa2d364cb73a 100644 --- a/lib/internal/streams/state.js +++ b/lib/internal/streams/state.js @@ -1,5 +1,7 @@ 'use strict'; +const { Math } = primordials; + const { ERR_INVALID_OPT_VALUE } = require('internal/errors').codes; function highWaterMarkFrom(options, isDuplex, duplexKey) { diff --git a/lib/internal/timers.js b/lib/internal/timers.js index 9a1b0dea0b0dfb..239b00129fecb2 100644 --- a/lib/internal/timers.js +++ b/lib/internal/timers.js @@ -72,6 +72,8 @@ // timers within (or creation of a new list). However, these operations combined // have shown to be trivial in comparison to other timers architectures. +const { Math, Reflect } = primordials; + const { scheduleTimer, toggleTimerRef, diff --git a/lib/internal/url.js b/lib/internal/url.js index 0771dab13a17a6..6ef2ad3017f293 100644 --- a/lib/internal/url.js +++ b/lib/internal/url.js @@ -1,5 +1,7 @@ 'use strict'; +const { Reflect } = primordials; + const { inspect } = require('internal/util/inspect'); const { encodeStr, diff --git a/lib/internal/util.js b/lib/internal/util.js index 2800f6122f8441..ba332f7e852f1a 100644 --- a/lib/internal/util.js +++ b/lib/internal/util.js @@ -1,5 +1,7 @@ 'use strict'; +const { Reflect } = primordials; + const { ERR_INVALID_ARG_TYPE, ERR_NO_CRYPTO, diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js index 87e53d62f64f05..52924a4f706945 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -1,5 +1,7 @@ 'use strict'; +const { JSON, Math } = primordials; + const { getOwnNonIndexProperties, getPromiseDetails, diff --git a/lib/internal/v8_prof_processor.js b/lib/internal/v8_prof_processor.js index 6c741ea330d5b9..a39fbf4743e76f 100644 --- a/lib/internal/v8_prof_processor.js +++ b/lib/internal/v8_prof_processor.js @@ -1,5 +1,7 @@ 'use strict'; +const { JSON } = primordials; + const vm = require('vm'); const scriptFiles = [ diff --git a/lib/readline.js b/lib/readline.js index 2247796fe29b34..e621cde3092fd2 100644 --- a/lib/readline.js +++ b/lib/readline.js @@ -27,6 +27,8 @@ 'use strict'; +const { Math } = primordials; + const { ERR_INVALID_CURSOR_POS, ERR_INVALID_OPT_VALUE diff --git a/lib/repl.js b/lib/repl.js index 288278687cf75a..c22d450539d270 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -42,6 +42,8 @@ 'use strict'; +const { Math } = primordials; + const { builtinLibs, makeRequireFunction, diff --git a/lib/timers.js b/lib/timers.js index 8dc8b8877bed98..1e34b0388aa9b4 100644 --- a/lib/timers.js +++ b/lib/timers.js @@ -21,6 +21,8 @@ 'use strict'; +const { Math } = primordials; + const { immediateInfo, toggleImmediateRef diff --git a/lib/util.js b/lib/util.js index f58f0e7a3e7605..a508c3cead113c 100644 --- a/lib/util.js +++ b/lib/util.js @@ -21,6 +21,8 @@ 'use strict'; +const { Reflect } = primordials; + const { codes: { ERR_FALSY_VALUE_REJECTION, diff --git a/lib/zlib.js b/lib/zlib.js index 2d73b0e55289a5..1d19210d70c512 100644 --- a/lib/zlib.js +++ b/lib/zlib.js @@ -21,6 +21,8 @@ 'use strict'; +const { Math } = primordials; + const { codes: { ERR_BROTLI_INVALID_PARAM,