Skip to content

Commit

Permalink
tools: add prefer-proto rule
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Jan 3, 2023
1 parent 57048ac commit 0c92b54
Show file tree
Hide file tree
Showing 36 changed files with 102 additions and 100 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ module.exports = {
// Custom rules from eslint-plugin-node-core
'node-core/no-unescaped-regexp-dot': 'error',
'node-core/no-duplicate-requires': 'error',
'node-core/prefer-proto': 'error',
},
globals: {
ByteLengthQueuingStrategy: 'readable',
Expand Down
7 changes: 3 additions & 4 deletions lib/_http_agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ const {
FunctionPrototypeCall,
NumberIsNaN,
NumberParseInt,
ObjectCreate,
ObjectKeys,
ObjectSetPrototypeOf,
ObjectValues,
Expand Down Expand Up @@ -111,9 +110,9 @@ function Agent(options) {

// Don't confuse net and make it think that we're connecting to a pipe
this.options.path = null;
this.requests = ObjectCreate(null);
this.sockets = ObjectCreate(null);
this.freeSockets = ObjectCreate(null);
this.requests = { __proto__: null };
this.sockets = { __proto__: null };
this.freeSockets = { __proto__: null };
this.keepAliveMsecs = this.options.keepAliveMsecs || 1000;
this.keepAlive = this.options.keepAlive || false;
this.maxSockets = this.options.maxSockets || Agent.defaultMaxSockets;
Expand Down
9 changes: 4 additions & 5 deletions lib/_http_outgoing.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ const {
MathAbs,
MathFloor,
NumberPrototypeToString,
ObjectCreate,
ObjectDefineProperty,
ObjectKeys,
ObjectValues,
Expand Down Expand Up @@ -217,7 +216,7 @@ ObjectDefineProperty(OutgoingMessage.prototype, '_headers', {
if (val == null) {
this[kOutHeaders] = null;
} else if (typeof val === 'object') {
const headers = this[kOutHeaders] = ObjectCreate(null);
const headers = this[kOutHeaders] = { __proto__: null };
const keys = ObjectKeys(val);
// Retain for(;;) loop for performance reasons
// Refs: https://github.com/nodejs/node/pull/30958
Expand All @@ -244,7 +243,7 @@ ObjectDefineProperty(OutgoingMessage.prototype, '_headerNames', {
get: internalUtil.deprecate(function() {
const headers = this[kOutHeaders];
if (headers !== null) {
const out = ObjectCreate(null);
const out = { __proto__: null };
const keys = ObjectKeys(headers);
// Retain for(;;) loop for performance reasons
// Refs: https://github.com/nodejs/node/pull/30958
Expand Down Expand Up @@ -667,7 +666,7 @@ OutgoingMessage.prototype.setHeader = function setHeader(name, value) {

let headers = this[kOutHeaders];
if (headers === null)
this[kOutHeaders] = headers = ObjectCreate(null);
this[kOutHeaders] = headers = { __proto__: null };

headers[StringPrototypeToLowerCase(name)] = [name, value];
return this;
Expand Down Expand Up @@ -742,7 +741,7 @@ OutgoingMessage.prototype.getRawHeaderNames = function getRawHeaderNames() {
// Returns a shallow copy of the current outgoing headers.
OutgoingMessage.prototype.getHeaders = function getHeaders() {
const headers = this[kOutHeaders];
const ret = ObjectCreate(null);
const ret = { __proto__: null };
if (headers) {
const keys = ObjectKeys(headers);
// Retain for(;;) loop for performance reasons
Expand Down
3 changes: 1 addition & 2 deletions lib/_tls_common.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ const tls = require('tls');
const {
ArrayPrototypePush,
JSONParse,
ObjectCreate,
RegExpPrototypeSymbolReplace,
} = primordials;

Expand Down Expand Up @@ -131,7 +130,7 @@ function translatePeerCertificate(c) {
}
if (c.infoAccess != null) {
const info = c.infoAccess;
c.infoAccess = ObjectCreate(null);
c.infoAccess = { __proto__: null };

// XXX: More key validation?
RegExpPrototypeSymbolReplace(/([^\n:]*):([^\n]*)(?:\n|$)/g, info,
Expand Down
5 changes: 2 additions & 3 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ const {
NumberIsNaN,
NumberMAX_SAFE_INTEGER,
NumberMIN_SAFE_INTEGER,
ObjectCreate,
ObjectDefineProperties,
ObjectDefineProperty,
ObjectSetPrototypeOf,
Expand Down Expand Up @@ -146,7 +145,7 @@ const constants = ObjectDefineProperties({}, {
Buffer.poolSize = 8 * 1024;
let poolSize, poolOffset, allocPool;

const encodingsMap = ObjectCreate(null);
const encodingsMap = { __proto__: null };
for (let i = 0; i < encodings.length; ++i)
encodingsMap[encodings[i]] = i;

Expand Down Expand Up @@ -845,7 +844,7 @@ Buffer.prototype[customInspectSymbol] = function inspect(recurseTimes, ctx) {
if (ctx) {
let extras = false;
const filter = ctx.showHidden ? ALL_PROPERTIES : ONLY_ENUMERABLE;
const obj = ObjectCreate(null);
const obj = { __proto__: null };
ArrayPrototypeForEach(getOwnNonIndexProperties(this, filter),
(key) => {
extras = true;
Expand Down
3 changes: 1 addition & 2 deletions lib/diagnostics_channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const {
ArrayPrototypeIndexOf,
ArrayPrototypePush,
ArrayPrototypeSplice,
ObjectCreate,
ObjectGetPrototypeOf,
ObjectSetPrototypeOf,
SymbolHasInstance,
Expand Down Expand Up @@ -92,7 +91,7 @@ class Channel {
publish() {}
}

const channels = ObjectCreate(null);
const channels = { __proto__: null };

function channel(name) {
let channel;
Expand Down
13 changes: 6 additions & 7 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ const {
FunctionPrototypeCall,
NumberIsNaN,
NumberMAX_SAFE_INTEGER,
ObjectCreate,
ObjectDefineProperty,
ObjectDefineProperties,
ObjectGetPrototypeOf,
Expand Down Expand Up @@ -344,7 +343,7 @@ EventEmitter.init = function(opts) {

if (this._events === undefined ||
this._events === ObjectGetPrototypeOf(this)._events) {
this._events = ObjectCreate(null);
this._events = { __proto__: null };
this._eventsCount = 0;
}

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

events = target._events;
if (events === undefined) {
events = target._events = ObjectCreate(null);
events = target._events = { __proto__: null };
target._eventsCount = 0;
} else {
// To avoid recursion in the case that type === "newListener"! Before
Expand Down Expand Up @@ -691,7 +690,7 @@ EventEmitter.prototype.removeListener =

if (list === listener || list.listener === listener) {
if (--this._eventsCount === 0)
this._events = ObjectCreate(null);
this._events = { __proto__: null };
else {
delete events[type];
if (events.removeListener)
Expand Down Expand Up @@ -746,11 +745,11 @@ EventEmitter.prototype.removeAllListeners =
// Not listening for removeListener, no need to emit
if (events.removeListener === undefined) {
if (arguments.length === 0) {
this._events = ObjectCreate(null);
this._events = { __proto__: null };
this._eventsCount = 0;
} else if (events[type] !== undefined) {
if (--this._eventsCount === 0)
this._events = ObjectCreate(null);
this._events = { __proto__: null };
else
delete events[type];
}
Expand All @@ -764,7 +763,7 @@ EventEmitter.prototype.removeAllListeners =
this.removeAllListeners(key);
}
this.removeAllListeners('removeListener');
this._events = ObjectCreate(null);
this._events = { __proto__: null };
this._eventsCount = 0;
return this;
}
Expand Down
5 changes: 2 additions & 3 deletions lib/internal/bootstrap/loaders.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ const {
ArrayPrototypePush,
ArrayPrototypeSlice,
Error,
ObjectCreate,
ObjectDefineProperty,
ObjectKeys,
ObjectPrototypeHasOwnProperty,
Expand Down Expand Up @@ -129,7 +128,7 @@ const schemelessBlockList = new SafeSet([

// Set up process.binding() and process._linkedBinding().
{
const bindingObj = ObjectCreate(null);
const bindingObj = { __proto__: null };

process.binding = function binding(module) {
module = String(module);
Expand Down Expand Up @@ -167,7 +166,7 @@ const schemelessBlockList = new SafeSet([
*/
let internalBinding;
{
const bindingObj = ObjectCreate(null);
const bindingObj = { __proto__: null };
// eslint-disable-next-line no-global-assign
internalBinding = function internalBinding(module) {
let mod = bindingObj[module];
Expand Down
3 changes: 1 addition & 2 deletions lib/internal/cluster/round_robin_handle.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
const {
ArrayIsArray,
Boolean,
ObjectCreate,
SafeMap,
} = primordials;

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

Expand Down
3 changes: 1 addition & 2 deletions lib/internal/console/constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ const {
MathFloor,
Number,
NumberPrototypeToFixed,
ObjectCreate,
ObjectDefineProperties,
ObjectDefineProperty,
ObjectKeys,
Expand Down Expand Up @@ -572,7 +571,7 @@ const consoleMethods = {
return final([iterKey, valuesKey], [getIndexArray(length), values]);
}

const map = ObjectCreate(null);
const map = { __proto__: null };
let hasPrimitives = false;
const valuesKeyArray = [];
const indexKeyArray = ObjectKeys(tabularData);
Expand Down
3 changes: 1 addition & 2 deletions lib/internal/dns/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ const {
NumberParseInt,
RegExpPrototypeExec,
RegExpPrototypeSymbolReplace,
ObjectCreate,
Symbol,
} = primordials;

Expand Down Expand Up @@ -286,7 +285,7 @@ function setDefaultResultOrder(value) {
}

function createResolverClass(resolver) {
const resolveMap = ObjectCreate(null);
const resolveMap = { __proto__: null };

class Resolver extends ResolverBase {}

Expand Down
2 changes: 1 addition & 1 deletion lib/internal/error_serdes.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const errors = {
const errorConstructorNames = new SafeSet(ObjectKeys(errors));

function TryGetAllProperties(object, target = object) {
const all = ObjectCreate(null);
const all = { __proto__: null };
if (object === null)
return all;
ObjectAssign(all,
Expand Down
9 changes: 4 additions & 5 deletions lib/internal/http2/compat.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const {
Boolean,
FunctionPrototypeBind,
ObjectAssign,
ObjectCreate,
ObjectKeys,
ObjectPrototypeHasOwnProperty,
Proxy,
Expand Down Expand Up @@ -483,8 +482,8 @@ class Http2ServerResponse extends Stream {
sendDate: true,
statusCode: HTTP_STATUS_OK,
};
this[kHeaders] = ObjectCreate(null);
this[kTrailers] = ObjectCreate(null);
this[kHeaders] = { __proto__: null };
this[kTrailers] = { __proto__: null };
this[kStream] = stream;
stream[kProxySocket] = null;
stream[kResponse] = this;
Expand Down Expand Up @@ -603,7 +602,7 @@ class Http2ServerResponse extends Stream {
}

getHeaders() {
const headers = ObjectCreate(null);
const headers = { __proto__: null };
return ObjectAssign(headers, this[kHeaders]);
}

Expand Down Expand Up @@ -851,7 +850,7 @@ class Http2ServerResponse extends Stream {
writeEarlyHints(hints) {
validateObject(hints, 'hints');

const headers = ObjectCreate(null);
const headers = { __proto__: null };

const linkHeaderValue = validateLinkHeaderValue(hints.link);

Expand Down
11 changes: 5 additions & 6 deletions lib/internal/http2/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ const {
FunctionPrototypeCall,
MathMin,
ObjectAssign,
ObjectCreate,
ObjectKeys,
ObjectDefineProperty,
ObjectPrototypeHasOwnProperty,
Expand Down Expand Up @@ -1753,7 +1752,7 @@ class ClientHttp2Session extends Http2Session {
assertIsObject(headers, 'headers');
assertIsObject(options, 'options');

headers = ObjectAssign(ObjectCreate(null), headers);
headers = ObjectAssign({ __proto__: null }, headers);
options = { ...options };

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

assertIsObject(headers, 'headers');
headers = ObjectAssign(ObjectCreate(null), headers);
headers = ObjectAssign({ __proto__: null }, headers);

debugStreamObj(this, 'sending trailers');

Expand Down Expand Up @@ -2420,7 +2419,7 @@ function callStreamClose(stream) {

function processHeaders(oldHeaders, options) {
assertIsObject(oldHeaders, 'headers');
const headers = ObjectCreate(null);
const headers = { __proto__: null };

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

assertIsObject(headers, 'headers');
headers = ObjectAssign(ObjectCreate(null), headers);
headers = ObjectAssign({ __proto__: null }, headers);

if (headers[HTTP2_HEADER_METHOD] === undefined)
headers[HTTP2_HEADER_METHOD] = HTTP2_METHOD_GET;
Expand Down Expand Up @@ -2931,7 +2930,7 @@ class ServerHttp2Stream extends Http2Stream {
throw new ERR_HTTP2_HEADERS_AFTER_RESPOND();

assertIsObject(headers, 'headers');
headers = ObjectAssign(ObjectCreate(null), headers);
headers = ObjectAssign({ __proto__: null }, headers);

debugStreamObj(this, 'sending additional headers');

Expand Down
5 changes: 2 additions & 3 deletions lib/internal/http2/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const {
Error,
MathMax,
Number,
ObjectCreate,
ObjectDefineProperty,
ObjectKeys,
SafeSet,
Expand Down Expand Up @@ -279,7 +278,7 @@ function updateOptionsBuffer(options) {
function getDefaultSettings() {
settingsBuffer[IDX_SETTINGS_FLAGS] = 0;
binding.refreshDefaultSettings();
const holder = ObjectCreate(null);
const holder = { __proto__: null };

const flags = settingsBuffer[IDX_SETTINGS_FLAGS];

Expand Down Expand Up @@ -588,7 +587,7 @@ const assertWithinRange = hideStackFrames(
);

function toHeaderObject(headers, sensitiveHeaders) {
const obj = ObjectCreate(null);
const obj = { __proto__: null };
for (let n = 0; n < headers.length; n += 2) {
const name = headers[n];
let value = headers[n + 1];
Expand Down
Loading

0 comments on commit 0c92b54

Please sign in to comment.