-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
Add new ESLint rule: prefer-primordials #35448
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,21 +20,61 @@ | |
// https://github.com/tc39/proposal-ses/blob/e5271cc42a257a05dcae2fd94713ed2f46c08620/shim/src/freeze.js | ||
|
||
/* global WebAssembly, SharedArrayBuffer, console */ | ||
/* eslint-disable no-restricted-globals */ | ||
Leko marked this conversation as resolved.
Show resolved
Hide resolved
|
||
'use strict'; | ||
|
||
const { | ||
Array, | ||
ArrayBuffer, | ||
ArrayPrototypeForEach, | ||
BigInt, | ||
BigInt64Array, | ||
BigUint64Array, | ||
Boolean, | ||
DataView, | ||
Date, | ||
Error, | ||
EvalError, | ||
Float32Array, | ||
Float64Array, | ||
Function, | ||
Int16Array, | ||
Int32Array, | ||
Int8Array, | ||
JSON, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I’m fixing that in #36016. |
||
Map, | ||
Math, | ||
Number, | ||
Object, | ||
ObjectDefineProperty, | ||
ObjectFreeze, | ||
ObjectGetOwnPropertyDescriptor, | ||
ObjectGetOwnPropertyDescriptors, | ||
ObjectGetOwnPropertyNames, | ||
ObjectGetOwnPropertySymbols, | ||
ObjectGetPrototypeOf, | ||
ObjectPrototypeHasOwnProperty, | ||
Promise, | ||
RangeError, | ||
ReferenceError, | ||
Reflect, | ||
ReflectOwnKeys, | ||
RegExp, | ||
Set, | ||
String, | ||
Symbol, | ||
SymbolIterator, | ||
SyntaxError, | ||
TypeError, | ||
Uint16Array, | ||
Uint32Array, | ||
Uint8Array, | ||
Uint8ClampedArray, | ||
URIError, | ||
WeakMap, | ||
WeakSet, | ||
} = primordials; | ||
|
||
module.exports = function() { | ||
const { | ||
defineProperty, | ||
freeze, | ||
getOwnPropertyDescriptor, | ||
getOwnPropertyDescriptors, | ||
getOwnPropertyNames, | ||
getOwnPropertySymbols, | ||
getPrototypeOf, | ||
} = Object; | ||
const objectHasOwnProperty = Object.prototype.hasOwnProperty; | ||
const { ownKeys } = Reflect; | ||
const { | ||
clearImmediate, | ||
clearInterval, | ||
|
@@ -47,25 +87,25 @@ module.exports = function() { | |
const intrinsicPrototypes = [ | ||
// Anonymous Intrinsics | ||
// IteratorPrototype | ||
getPrototypeOf( | ||
getPrototypeOf(new Array()[Symbol.iterator]()) | ||
ObjectGetPrototypeOf( | ||
ObjectGetPrototypeOf(new Array()[SymbolIterator]()) | ||
), | ||
// ArrayIteratorPrototype | ||
getPrototypeOf(new Array()[Symbol.iterator]()), | ||
ObjectGetPrototypeOf(new Array()[SymbolIterator]()), | ||
// StringIteratorPrototype | ||
getPrototypeOf(new String()[Symbol.iterator]()), | ||
ObjectGetPrototypeOf(new String()[SymbolIterator]()), | ||
// MapIteratorPrototype | ||
getPrototypeOf(new Map()[Symbol.iterator]()), | ||
ObjectGetPrototypeOf(new Map()[SymbolIterator]()), | ||
// SetIteratorPrototype | ||
getPrototypeOf(new Set()[Symbol.iterator]()), | ||
ObjectGetPrototypeOf(new Set()[SymbolIterator]()), | ||
// GeneratorFunction | ||
getPrototypeOf(function* () {}), | ||
ObjectGetPrototypeOf(function* () {}), | ||
// AsyncFunction | ||
getPrototypeOf(async function() {}), | ||
ObjectGetPrototypeOf(async function() {}), | ||
// AsyncGeneratorFunction | ||
getPrototypeOf(async function* () {}), | ||
ObjectGetPrototypeOf(async function* () {}), | ||
// TypedArray | ||
getPrototypeOf(Uint8Array), | ||
ObjectGetPrototypeOf(Uint8Array), | ||
|
||
// 19 Fundamental Objects | ||
Object.prototype, // 19.1 | ||
|
@@ -129,33 +169,37 @@ module.exports = function() { | |
const intrinsics = [ | ||
// Anonymous Intrinsics | ||
// ThrowTypeError | ||
getOwnPropertyDescriptor(Function.prototype, 'caller').get, | ||
ObjectGetOwnPropertyDescriptor(Function.prototype, 'caller').get, | ||
// IteratorPrototype | ||
getPrototypeOf( | ||
getPrototypeOf(new Array()[Symbol.iterator]()) | ||
ObjectGetPrototypeOf( | ||
ObjectGetPrototypeOf(new Array()[SymbolIterator]()) | ||
), | ||
// ArrayIteratorPrototype | ||
getPrototypeOf(new Array()[Symbol.iterator]()), | ||
ObjectGetPrototypeOf(new Array()[SymbolIterator]()), | ||
// StringIteratorPrototype | ||
getPrototypeOf(new String()[Symbol.iterator]()), | ||
ObjectGetPrototypeOf(new String()[SymbolIterator]()), | ||
// MapIteratorPrototype | ||
getPrototypeOf(new Map()[Symbol.iterator]()), | ||
ObjectGetPrototypeOf(new Map()[SymbolIterator]()), | ||
// SetIteratorPrototype | ||
getPrototypeOf(new Set()[Symbol.iterator]()), | ||
ObjectGetPrototypeOf(new Set()[SymbolIterator]()), | ||
// GeneratorFunction | ||
getPrototypeOf(function* () {}), | ||
ObjectGetPrototypeOf(function* () {}), | ||
// AsyncFunction | ||
getPrototypeOf(async function() {}), | ||
ObjectGetPrototypeOf(async function() {}), | ||
// AsyncGeneratorFunction | ||
getPrototypeOf(async function* () {}), | ||
ObjectGetPrototypeOf(async function* () {}), | ||
// TypedArray | ||
getPrototypeOf(Uint8Array), | ||
ObjectGetPrototypeOf(Uint8Array), | ||
|
||
// 18 The Global Object | ||
eval, | ||
// eslint-disable-next-line node-core/prefer-primordials | ||
isFinite, | ||
// eslint-disable-next-line node-core/prefer-primordials | ||
isNaN, | ||
// eslint-disable-next-line node-core/prefer-primordials | ||
parseFloat, | ||
// eslint-disable-next-line node-core/prefer-primordials | ||
parseInt, | ||
decodeURI, | ||
decodeURIComponent, | ||
|
@@ -289,16 +333,16 @@ module.exports = function() { | |
// Object are verified before being enqueued, | ||
// therefore this is a valid candidate. | ||
// Throws if this fails (strict mode). | ||
freeze(obj); | ||
ObjectFreeze(obj); | ||
|
||
// We rely upon certain commitments of Object.freeze and proxies here | ||
|
||
// Get stable/immutable outbound links before a Proxy has a chance to do | ||
// something sneaky. | ||
const proto = getPrototypeOf(obj); | ||
const descs = getOwnPropertyDescriptors(obj); | ||
const proto = ObjectGetPrototypeOf(obj); | ||
const descs = ObjectGetOwnPropertyDescriptors(obj); | ||
enqueue(proto); | ||
ownKeys(descs).forEach((name) => { | ||
ArrayPrototypeForEach(ReflectOwnKeys(descs), (name) => { | ||
// TODO: Uncurried form | ||
// TODO: getOwnPropertyDescriptors is guaranteed to return well-formed | ||
// descriptors, but they still inherit from Object.prototype. If | ||
|
@@ -378,10 +422,10 @@ module.exports = function() { | |
`Cannot assign to read only property '${prop}' of object '${obj}'` | ||
); | ||
} | ||
if (objectHasOwnProperty.call(this, prop)) { | ||
if (ObjectPrototypeHasOwnProperty(this, prop)) { | ||
this[prop] = newValue; | ||
} else { | ||
defineProperty(this, prop, { | ||
ObjectDefineProperty(this, prop, { | ||
value: newValue, | ||
writable: true, | ||
enumerable: true, | ||
|
@@ -390,7 +434,7 @@ module.exports = function() { | |
} | ||
} | ||
|
||
defineProperty(obj, prop, { | ||
ObjectDefineProperty(obj, prop, { | ||
get: getter, | ||
set: setter, | ||
enumerable: desc.enumerable, | ||
|
@@ -403,14 +447,14 @@ module.exports = function() { | |
if (!obj) { | ||
return; | ||
} | ||
const descs = getOwnPropertyDescriptors(obj); | ||
const descs = ObjectGetOwnPropertyDescriptors(obj); | ||
if (!descs) { | ||
return; | ||
} | ||
getOwnPropertyNames(obj).forEach((prop) => { | ||
ArrayPrototypeForEach(ObjectGetOwnPropertyNames(obj), (prop) => { | ||
return enableDerivedOverride(obj, prop, descs[prop]); | ||
}); | ||
getOwnPropertySymbols(obj).forEach((prop) => { | ||
ArrayPrototypeForEach(ObjectGetOwnPropertySymbols(obj), (prop) => { | ||
return enableDerivedOverride(obj, prop, descs[prop]); | ||
}); | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.