Skip to content

Commit ed69a3a

Browse files
ljharbRafaelGSS
authored andcommitted
tools: add prefer-proto rule
fixup: add support for `Object.create(null)` fixup: extend to any 1-argument Object.create call fixup: add tests PR-URL: #46083 Backport-PR-URL: #46239 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Geoffrey Booth <[email protected]> Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Mohammed Keyvanzadeh <[email protected]> Reviewed-By: Darshan Sen <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Jacob Smith <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]>
1 parent 6cd7057 commit ed69a3a

File tree

87 files changed

+308
-232
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+308
-232
lines changed

.eslintrc.js

+1
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@ module.exports = {
314314
// Custom rules from eslint-plugin-node-core
315315
'node-core/no-unescaped-regexp-dot': 'error',
316316
'node-core/no-duplicate-requires': 'error',
317+
'node-core/prefer-proto': 'error',
317318
},
318319
globals: {
319320
ByteLengthQueuingStrategy: 'readable',

benchmark/es/map-bench.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function runObject(n) {
2525
}
2626

2727
function runNullProtoObject(n) {
28-
const m = Object.create(null);
28+
const m = { __proto__: null };
2929
bench.start();
3030
for (let i = 0; i < n; i++) {
3131
m[`i${i}`] = i;
@@ -51,7 +51,7 @@ function runNullProtoLiteralObject(n) {
5151
}
5252

5353
function StorageObject() {}
54-
StorageObject.prototype = Object.create(null);
54+
StorageObject.prototype = { __proto__: null };
5555

5656
function runStorageObject(n) {
5757
const m = new StorageObject();

doc/api/assert.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,7 @@ const obj3 = {
664664
b: 1,
665665
},
666666
};
667-
const obj4 = Object.create(obj1);
667+
const obj4 = { __proto__: obj1 };
668668

669669
assert.deepEqual(obj1, obj1);
670670
// OK
@@ -699,7 +699,7 @@ const obj3 = {
699699
b: 1,
700700
},
701701
};
702-
const obj4 = Object.create(obj1);
702+
const obj4 = { __proto__: obj1 };
703703

704704
assert.deepEqual(obj1, obj1);
705705
// OK
@@ -1623,7 +1623,7 @@ const obj3 = {
16231623
b: 1,
16241624
},
16251625
};
1626-
const obj4 = Object.create(obj1);
1626+
const obj4 = { __proto__: obj1 };
16271627

16281628
assert.notDeepEqual(obj1, obj1);
16291629
// AssertionError: { a: { b: 1 } } notDeepEqual { a: { b: 1 } }
@@ -1656,7 +1656,7 @@ const obj3 = {
16561656
b: 1,
16571657
},
16581658
};
1659-
const obj4 = Object.create(obj1);
1659+
const obj4 = { __proto__: obj1 };
16601660

16611661
assert.notDeepEqual(obj1, obj1);
16621662
// AssertionError: { a: { b: 1 } } notDeepEqual { a: { b: 1 } }

lib/_http_agent.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ const {
3232
FunctionPrototypeCall,
3333
NumberIsNaN,
3434
NumberParseInt,
35-
ObjectCreate,
3635
ObjectKeys,
3736
ObjectSetPrototypeOf,
3837
ObjectValues,
@@ -111,9 +110,9 @@ function Agent(options) {
111110

112111
// Don't confuse net and make it think that we're connecting to a pipe
113112
this.options.path = null;
114-
this.requests = ObjectCreate(null);
115-
this.sockets = ObjectCreate(null);
116-
this.freeSockets = ObjectCreate(null);
113+
this.requests = { __proto__: null };
114+
this.sockets = { __proto__: null };
115+
this.freeSockets = { __proto__: null };
117116
this.keepAliveMsecs = this.options.keepAliveMsecs || 1000;
118117
this.keepAlive = this.options.keepAlive || false;
119118
this.maxSockets = this.options.maxSockets || Agent.defaultMaxSockets;

lib/_http_outgoing.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ const {
2828
MathAbs,
2929
MathFloor,
3030
NumberPrototypeToString,
31-
ObjectCreate,
3231
ObjectDefineProperty,
3332
ObjectKeys,
3433
ObjectValues,
@@ -217,7 +216,7 @@ ObjectDefineProperty(OutgoingMessage.prototype, '_headers', {
217216
if (val == null) {
218217
this[kOutHeaders] = null;
219218
} else if (typeof val === 'object') {
220-
const headers = this[kOutHeaders] = ObjectCreate(null);
219+
const headers = this[kOutHeaders] = { __proto__: null };
221220
const keys = ObjectKeys(val);
222221
// Retain for(;;) loop for performance reasons
223222
// Refs: https://github.com/nodejs/node/pull/30958
@@ -244,7 +243,7 @@ ObjectDefineProperty(OutgoingMessage.prototype, '_headerNames', {
244243
get: internalUtil.deprecate(function() {
245244
const headers = this[kOutHeaders];
246245
if (headers !== null) {
247-
const out = ObjectCreate(null);
246+
const out = { __proto__: null };
248247
const keys = ObjectKeys(headers);
249248
// Retain for(;;) loop for performance reasons
250249
// Refs: https://github.com/nodejs/node/pull/30958
@@ -667,7 +666,7 @@ OutgoingMessage.prototype.setHeader = function setHeader(name, value) {
667666

668667
let headers = this[kOutHeaders];
669668
if (headers === null)
670-
this[kOutHeaders] = headers = ObjectCreate(null);
669+
this[kOutHeaders] = headers = { __proto__: null };
671670

672671
headers[StringPrototypeToLowerCase(name)] = [name, value];
673672
return this;
@@ -742,7 +741,7 @@ OutgoingMessage.prototype.getRawHeaderNames = function getRawHeaderNames() {
742741
// Returns a shallow copy of the current outgoing headers.
743742
OutgoingMessage.prototype.getHeaders = function getHeaders() {
744743
const headers = this[kOutHeaders];
745-
const ret = ObjectCreate(null);
744+
const ret = { __proto__: null };
746745
if (headers) {
747746
const keys = ObjectKeys(headers);
748747
// Retain for(;;) loop for performance reasons

lib/_tls_common.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ const tls = require('tls');
2626
const {
2727
ArrayPrototypePush,
2828
JSONParse,
29-
ObjectCreate,
3029
RegExpPrototypeSymbolReplace,
3130
} = primordials;
3231

@@ -131,7 +130,7 @@ function translatePeerCertificate(c) {
131130
}
132131
if (c.infoAccess != null) {
133132
const info = c.infoAccess;
134-
c.infoAccess = ObjectCreate(null);
133+
c.infoAccess = { __proto__: null };
135134

136135
// XXX: More key validation?
137136
RegExpPrototypeSymbolReplace(/([^\n:]*):([^\n]*)(?:\n|$)/g, info,

lib/buffer.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ const {
3333
NumberIsNaN,
3434
NumberMAX_SAFE_INTEGER,
3535
NumberMIN_SAFE_INTEGER,
36-
ObjectCreate,
3736
ObjectDefineProperties,
3837
ObjectDefineProperty,
3938
ObjectSetPrototypeOf,
@@ -146,7 +145,7 @@ const constants = ObjectDefineProperties({}, {
146145
Buffer.poolSize = 8 * 1024;
147146
let poolSize, poolOffset, allocPool;
148147

149-
const encodingsMap = ObjectCreate(null);
148+
const encodingsMap = { __proto__: null };
150149
for (let i = 0; i < encodings.length; ++i)
151150
encodingsMap[encodings[i]] = i;
152151

@@ -845,7 +844,7 @@ Buffer.prototype[customInspectSymbol] = function inspect(recurseTimes, ctx) {
845844
if (ctx) {
846845
let extras = false;
847846
const filter = ctx.showHidden ? ALL_PROPERTIES : ONLY_ENUMERABLE;
848-
const obj = ObjectCreate(null);
847+
const obj = { __proto__: null };
849848
ArrayPrototypeForEach(getOwnNonIndexProperties(this, filter),
850849
(key) => {
851850
extras = true;

lib/diagnostics_channel.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ const {
44
ArrayPrototypeIndexOf,
55
ArrayPrototypePush,
66
ArrayPrototypeSplice,
7-
ObjectCreate,
87
ObjectGetPrototypeOf,
98
ObjectSetPrototypeOf,
109
SymbolHasInstance,
@@ -92,7 +91,7 @@ class Channel {
9291
publish() {}
9392
}
9493

95-
const channels = ObjectCreate(null);
94+
const channels = { __proto__: null };
9695

9796
function channel(name) {
9897
let channel;

lib/events.js

+6-7
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ const {
3333
FunctionPrototypeBind,
3434
FunctionPrototypeCall,
3535
NumberIsNaN,
36-
ObjectCreate,
3736
ObjectDefineProperty,
3837
ObjectDefineProperties,
3938
ObjectGetPrototypeOf,
@@ -338,7 +337,7 @@ EventEmitter.init = function(opts) {
338337

339338
if (this._events === undefined ||
340339
this._events === ObjectGetPrototypeOf(this)._events) {
341-
this._events = ObjectCreate(null);
340+
this._events = { __proto__: null };
342341
this._eventsCount = 0;
343342
}
344343

@@ -547,7 +546,7 @@ function _addListener(target, type, listener, prepend) {
547546

548547
events = target._events;
549548
if (events === undefined) {
550-
events = target._events = ObjectCreate(null);
549+
events = target._events = { __proto__: null };
551550
target._eventsCount = 0;
552551
} else {
553552
// To avoid recursion in the case that type === "newListener"! Before
@@ -685,7 +684,7 @@ EventEmitter.prototype.removeListener =
685684

686685
if (list === listener || list.listener === listener) {
687686
if (--this._eventsCount === 0)
688-
this._events = ObjectCreate(null);
687+
this._events = { __proto__: null };
689688
else {
690689
delete events[type];
691690
if (events.removeListener)
@@ -740,11 +739,11 @@ EventEmitter.prototype.removeAllListeners =
740739
// Not listening for removeListener, no need to emit
741740
if (events.removeListener === undefined) {
742741
if (arguments.length === 0) {
743-
this._events = ObjectCreate(null);
742+
this._events = { __proto__: null };
744743
this._eventsCount = 0;
745744
} else if (events[type] !== undefined) {
746745
if (--this._eventsCount === 0)
747-
this._events = ObjectCreate(null);
746+
this._events = { __proto__: null };
748747
else
749748
delete events[type];
750749
}
@@ -758,7 +757,7 @@ EventEmitter.prototype.removeAllListeners =
758757
this.removeAllListeners(key);
759758
}
760759
this.removeAllListeners('removeListener');
761-
this._events = ObjectCreate(null);
760+
this._events = { __proto__: null };
762761
this._eventsCount = 0;
763762
return this;
764763
}

lib/internal/assert/assertion_error.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ const {
66
Error,
77
ErrorCaptureStackTrace,
88
MathMax,
9-
ObjectCreate,
109
ObjectDefineProperty,
1110
ObjectGetPrototypeOf,
1211
ObjectKeys,
@@ -48,7 +47,7 @@ const kMaxShortLength = 12;
4847

4948
function copyError(source) {
5049
const keys = ObjectKeys(source);
51-
const target = ObjectCreate(ObjectGetPrototypeOf(source));
50+
const target = { __proto__: ObjectGetPrototypeOf(source) };
5251
for (const key of keys) {
5352
target[key] = source[key];
5453
}

lib/internal/bootstrap/loaders.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ const {
4949
ArrayPrototypePush,
5050
ArrayPrototypeSlice,
5151
Error,
52-
ObjectCreate,
5352
ObjectDefineProperty,
5453
ObjectKeys,
5554
ObjectPrototypeHasOwnProperty,
@@ -129,7 +128,7 @@ const schemelessBlockList = new SafeSet([
129128

130129
// Set up process.binding() and process._linkedBinding().
131130
{
132-
const bindingObj = ObjectCreate(null);
131+
const bindingObj = { __proto__: null };
133132

134133
process.binding = function binding(module) {
135134
module = String(module);
@@ -167,7 +166,7 @@ const schemelessBlockList = new SafeSet([
167166
*/
168167
let internalBinding;
169168
{
170-
const bindingObj = ObjectCreate(null);
169+
const bindingObj = { __proto__: null };
171170
// eslint-disable-next-line no-global-assign
172171
internalBinding = function internalBinding(module) {
173172
let mod = bindingObj[module];

lib/internal/cluster/round_robin_handle.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
const {
44
ArrayIsArray,
55
Boolean,
6-
ObjectCreate,
76
SafeMap,
87
} = primordials;
98

@@ -19,7 +18,7 @@ function RoundRobinHandle(key, address, { port, fd, flags, backlog, readableAll,
1918
this.key = key;
2019
this.all = new SafeMap();
2120
this.free = new SafeMap();
22-
this.handles = init(ObjectCreate(null));
21+
this.handles = init({ __proto__: null });
2322
this.handle = null;
2423
this.server = net.createServer(assert.fail);
2524

lib/internal/console/constructor.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ const {
1515
MathFloor,
1616
Number,
1717
NumberPrototypeToFixed,
18-
ObjectCreate,
1918
ObjectDefineProperties,
2019
ObjectDefineProperty,
2120
ObjectKeys,
@@ -572,7 +571,7 @@ const consoleMethods = {
572571
return final([iterKey, valuesKey], [getIndexArray(length), values]);
573572
}
574573

575-
const map = ObjectCreate(null);
574+
const map = { __proto__: null };
576575
let hasPrimitives = false;
577576
const valuesKeyArray = [];
578577
const indexKeyArray = ObjectKeys(tabularData);

lib/internal/console/global.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
const {
1616
FunctionPrototypeBind,
17-
ObjectCreate,
1817
ReflectDefineProperty,
1918
ReflectGetOwnPropertyDescriptor,
2019
ReflectOwnKeys,
@@ -24,7 +23,7 @@ const {
2423
Console
2524
} = require('internal/console/constructor');
2625

27-
const globalConsole = ObjectCreate({});
26+
const globalConsole = { __proto__: {} };
2827

2928
// Since Console is not on the prototype chain of the global console,
3029
// the symbol properties on Console.prototype have to be looked up from

lib/internal/dns/utils.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ const {
99
NumberParseInt,
1010
RegExpPrototypeExec,
1111
RegExpPrototypeSymbolReplace,
12-
ObjectCreate,
1312
Symbol,
1413
} = primordials;
1514

@@ -286,7 +285,7 @@ function setDefaultResultOrder(value) {
286285
}
287286

288287
function createResolverClass(resolver) {
289-
const resolveMap = ObjectCreate(null);
288+
const resolveMap = { __proto__: null };
290289

291290
class Resolver extends ResolverBase {}
292291

lib/internal/encoding.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
const {
77
Boolean,
8-
ObjectCreate,
98
ObjectDefineProperties,
109
ObjectGetOwnPropertyDescriptors,
1110
ObjectSetPrototypeOf,
@@ -350,9 +349,9 @@ class TextEncoder {
350349
if (typeof depth === 'number' && depth < 0)
351350
return this;
352351
const ctor = getConstructorOf(this);
353-
const obj = ObjectCreate({
352+
const obj = { __proto__: {
354353
constructor: ctor === null ? TextEncoder : ctor
355-
});
354+
} };
356355
obj.encoding = this.encoding;
357356
// Lazy to avoid circular dependency
358357
return require('internal/util/inspect').inspect(obj, opts);
@@ -571,7 +570,7 @@ const sharedProperties = ObjectGetOwnPropertyDescriptors({
571570
if (typeof depth === 'number' && depth < 0)
572571
return this;
573572
const constructor = getConstructorOf(this) || TextDecoder;
574-
const obj = ObjectCreate({ constructor });
573+
const obj = { __proto__: { constructor } };
575574
obj.encoding = this.encoding;
576575
obj.fatal = this.fatal;
577576
obj.ignoreBOM = this.ignoreBOM;

lib/internal/error_serdes.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const errors = {
3333
const errorConstructorNames = new SafeSet(ObjectKeys(errors));
3434

3535
function TryGetAllProperties(object, target = object) {
36-
const all = ObjectCreate(null);
36+
const all = { __proto__: null };
3737
if (object === null)
3838
return all;
3939
ObjectAssign(all,

0 commit comments

Comments
 (0)