Skip to content

Commit c24517d

Browse files
committed
tools: add prefer-proto rule
1 parent 57048ac commit c24517d

37 files changed

+102
-106
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',

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
@@ -35,7 +35,6 @@ const {
3535
FunctionPrototypeCall,
3636
NumberIsNaN,
3737
NumberMAX_SAFE_INTEGER,
38-
ObjectCreate,
3938
ObjectDefineProperty,
4039
ObjectDefineProperties,
4140
ObjectGetPrototypeOf,
@@ -344,7 +343,7 @@ EventEmitter.init = function(opts) {
344343

345344
if (this._events === undefined ||
346345
this._events === ObjectGetPrototypeOf(this)._events) {
347-
this._events = ObjectCreate(null);
346+
this._events = { __proto__: null };
348347
this._eventsCount = 0;
349348
}
350349

@@ -553,7 +552,7 @@ function _addListener(target, type, listener, prepend) {
553552

554553
events = target._events;
555554
if (events === undefined) {
556-
events = target._events = ObjectCreate(null);
555+
events = target._events = { __proto__: null };
557556
target._eventsCount = 0;
558557
} else {
559558
// To avoid recursion in the case that type === "newListener"! Before
@@ -691,7 +690,7 @@ EventEmitter.prototype.removeListener =
691690

692691
if (list === listener || list.listener === listener) {
693692
if (--this._eventsCount === 0)
694-
this._events = ObjectCreate(null);
693+
this._events = { __proto__: null };
695694
else {
696695
delete events[type];
697696
if (events.removeListener)
@@ -746,11 +745,11 @@ EventEmitter.prototype.removeAllListeners =
746745
// Not listening for removeListener, no need to emit
747746
if (events.removeListener === undefined) {
748747
if (arguments.length === 0) {
749-
this._events = ObjectCreate(null);
748+
this._events = { __proto__: null };
750749
this._eventsCount = 0;
751750
} else if (events[type] !== undefined) {
752751
if (--this._eventsCount === 0)
753-
this._events = ObjectCreate(null);
752+
this._events = { __proto__: null };
754753
else
755754
delete events[type];
756755
}
@@ -764,7 +763,7 @@ EventEmitter.prototype.removeAllListeners =
764763
this.removeAllListeners(key);
765764
}
766765
this.removeAllListeners('removeListener');
767-
this._events = ObjectCreate(null);
766+
this._events = { __proto__: null };
768767
this._eventsCount = 0;
769768
return this;
770769
}

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/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/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,

lib/internal/http2/compat.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ const {
66
Boolean,
77
FunctionPrototypeBind,
88
ObjectAssign,
9-
ObjectCreate,
109
ObjectKeys,
1110
ObjectPrototypeHasOwnProperty,
1211
Proxy,
@@ -483,8 +482,8 @@ class Http2ServerResponse extends Stream {
483482
sendDate: true,
484483
statusCode: HTTP_STATUS_OK,
485484
};
486-
this[kHeaders] = ObjectCreate(null);
487-
this[kTrailers] = ObjectCreate(null);
485+
this[kHeaders] = { __proto__: null };
486+
this[kTrailers] = { __proto__: null };
488487
this[kStream] = stream;
489488
stream[kProxySocket] = null;
490489
stream[kResponse] = this;
@@ -603,7 +602,7 @@ class Http2ServerResponse extends Stream {
603602
}
604603

605604
getHeaders() {
606-
const headers = ObjectCreate(null);
605+
const headers = { __proto__: null };
607606
return ObjectAssign(headers, this[kHeaders]);
608607
}
609608

@@ -851,7 +850,7 @@ class Http2ServerResponse extends Stream {
851850
writeEarlyHints(hints) {
852851
validateObject(hints, 'hints');
853852

854-
const headers = ObjectCreate(null);
853+
const headers = { __proto__: null };
855854

856855
const linkHeaderValue = validateLinkHeaderValue(hints.link);
857856

lib/internal/http2/core.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ const {
1212
FunctionPrototypeCall,
1313
MathMin,
1414
ObjectAssign,
15-
ObjectCreate,
1615
ObjectKeys,
1716
ObjectDefineProperty,
1817
ObjectPrototypeHasOwnProperty,
@@ -1753,7 +1752,7 @@ class ClientHttp2Session extends Http2Session {
17531752
assertIsObject(headers, 'headers');
17541753
assertIsObject(options, 'options');
17551754

1756-
headers = ObjectAssign(ObjectCreate(null), headers);
1755+
headers = ObjectAssign({ __proto__: null }, headers);
17571756
options = { ...options };
17581757

17591758
if (headers[HTTP2_HEADER_METHOD] === undefined)
@@ -2252,7 +2251,7 @@ class Http2Stream extends Duplex {
22522251
throw new ERR_HTTP2_TRAILERS_NOT_READY();
22532252

22542253
assertIsObject(headers, 'headers');
2255-
headers = ObjectAssign(ObjectCreate(null), headers);
2254+
headers = ObjectAssign({ __proto__: null }, headers);
22562255

22572256
debugStreamObj(this, 'sending trailers');
22582257

@@ -2420,7 +2419,7 @@ function callStreamClose(stream) {
24202419

24212420
function processHeaders(oldHeaders, options) {
24222421
assertIsObject(oldHeaders, 'headers');
2423-
const headers = ObjectCreate(null);
2422+
const headers = { __proto__: null };
24242423

24252424
if (oldHeaders !== null && oldHeaders !== undefined) {
24262425
// This loop is here for performance reason. Do not change.
@@ -2696,7 +2695,7 @@ class ServerHttp2Stream extends Http2Stream {
26962695
options.endStream = !!options.endStream;
26972696

26982697
assertIsObject(headers, 'headers');
2699-
headers = ObjectAssign(ObjectCreate(null), headers);
2698+
headers = ObjectAssign({ __proto__: null }, headers);
27002699

27012700
if (headers[HTTP2_HEADER_METHOD] === undefined)
27022701
headers[HTTP2_HEADER_METHOD] = HTTP2_METHOD_GET;
@@ -2931,7 +2930,7 @@ class ServerHttp2Stream extends Http2Stream {
29312930
throw new ERR_HTTP2_HEADERS_AFTER_RESPOND();
29322931

29332932
assertIsObject(headers, 'headers');
2934-
headers = ObjectAssign(ObjectCreate(null), headers);
2933+
headers = ObjectAssign({ __proto__: null }, headers);
29352934

29362935
debugStreamObj(this, 'sending additional headers');
29372936

lib/internal/http2/util.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ const {
88
Error,
99
MathMax,
1010
Number,
11-
ObjectCreate,
1211
ObjectDefineProperty,
1312
ObjectKeys,
1413
SafeSet,
@@ -279,7 +278,7 @@ function updateOptionsBuffer(options) {
279278
function getDefaultSettings() {
280279
settingsBuffer[IDX_SETTINGS_FLAGS] = 0;
281280
binding.refreshDefaultSettings();
282-
const holder = ObjectCreate(null);
281+
const holder = { __proto__: null };
283282

284283
const flags = settingsBuffer[IDX_SETTINGS_FLAGS];
285284

@@ -588,7 +587,7 @@ const assertWithinRange = hideStackFrames(
588587
);
589588

590589
function toHeaderObject(headers, sensitiveHeaders) {
591-
const obj = ObjectCreate(null);
590+
const obj = { __proto__: null };
592591
for (let n = 0; n < headers.length; n += 2) {
593592
const name = headers[n];
594593
let value = headers[n + 1];

0 commit comments

Comments
 (0)