test: use Object.hasOwn() where applicable

Replace Object.prototpye.hasOwnProperty() with Object.hasOwn() where
applicable.

PR-URL: https://github.com/nodejs/node/pull/41664
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Reviewed-By: Mestery <mestery@protonmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Tierney Cyren <hello@bnb.im>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
This commit is contained in:
Rich Trott 2022-01-24 23:03:17 -08:00 committed by GitHub
parent ff5766fc2e
commit e2e2bc83c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
21 changed files with 40 additions and 40 deletions

View File

@ -69,7 +69,7 @@ function _validateContent(report, fields = []) {
checkForUnknownFields(report, sections); checkForUnknownFields(report, sections);
sections.forEach((section) => { sections.forEach((section) => {
assert(report.hasOwnProperty(section)); assert(Object.hasOwn(report, section));
assert(typeof report[section] === 'object' && report[section] !== null); assert(typeof report[section] === 'object' && report[section] !== null);
}); });

View File

@ -51,7 +51,7 @@ for (const version of versions) {
assert.strictEqual(parts[parts.length - 1], 'x', assert.strictEqual(parts[parts.length - 1], 'x',
`'num' from ${tested} doesn't end in '.x'.`); `'num' from ${tested} doesn't end in '.x'.`);
const isEvenRelease = Number.parseInt(parts[expectedLength - 2]) % 2 === 0; const isEvenRelease = Number.parseInt(parts[expectedLength - 2]) % 2 === 0;
const hasLtsProperty = version.hasOwnProperty('lts'); const hasLtsProperty = Object.hasOwn(version, 'lts');
if (hasLtsProperty) { if (hasLtsProperty) {
// Odd-numbered versions of Node.js are never LTS. // Odd-numbered versions of Node.js are never LTS.
assert.ok(isEvenRelease, `${tested} should not be an 'lts' release.`); assert.ok(isEvenRelease, `${tested} should not be an 'lts' release.`);

View File

@ -78,7 +78,7 @@ child.spawn({
stdio: 'pipe' stdio: 'pipe'
}); });
assert.strictEqual(child.hasOwnProperty('pid'), true); assert.strictEqual(Object.hasOwn(child, 'pid'), true);
assert(Number.isInteger(child.pid)); assert(Number.isInteger(child.pid));
// Try killing with invalid signal // Try killing with invalid signal

View File

@ -18,9 +18,9 @@ assert.throws(() => getValidStdio(600), expectedError);
const stdio1 = []; const stdio1 = [];
const result = getValidStdio(stdio1, false); const result = getValidStdio(stdio1, false);
assert.strictEqual(stdio1.length, 3); assert.strictEqual(stdio1.length, 3);
assert.strictEqual(result.hasOwnProperty('stdio'), true); assert.strictEqual(Object.hasOwn(result, 'stdio'), true);
assert.strictEqual(result.hasOwnProperty('ipc'), true); assert.strictEqual(Object.hasOwn(result, 'ipc'), true);
assert.strictEqual(result.hasOwnProperty('ipcFd'), true); assert.strictEqual(Object.hasOwn(result, 'ipcFd'), true);
} }
// Should throw if stdio has ipc and sync is true // Should throw if stdio has ipc and sync is true

View File

@ -131,7 +131,7 @@ if (cluster.isWorker) {
assert.strictEqual(Object.keys(arguments[0]).length, 4); assert.strictEqual(Object.keys(arguments[0]).length, 4);
assert.strictEqual(arguments[0].address, '127.0.0.1'); assert.strictEqual(arguments[0].address, '127.0.0.1');
assert.strictEqual(arguments[0].addressType, 4); assert.strictEqual(arguments[0].addressType, 4);
assert(arguments[0].hasOwnProperty('fd')); assert(Object.hasOwn(arguments[0], 'fd'));
assert.strictEqual(arguments[0].fd, undefined); assert.strictEqual(arguments[0].fd, undefined);
const port = arguments[0].port; const port = arguments[0].port;
assert(Number.isInteger(port)); assert(Number.isInteger(port));

View File

@ -72,7 +72,7 @@ function primary() {
// Set up event handlers for every worker. Each worker sends a message when // Set up event handlers for every worker. Each worker sends a message when
// it has received the expected number of packets. After that it disconnects. // it has received the expected number of packets. After that it disconnects.
for (const key in cluster.workers) { for (const key in cluster.workers) {
if (cluster.workers.hasOwnProperty(key)) if (Object.hasOwn(cluster.workers, key))
setupWorker(cluster.workers[key]); setupWorker(cluster.workers[key]);
} }

View File

@ -63,7 +63,7 @@ function primary() {
// Set up event handlers for every worker. Each worker sends a message when // Set up event handlers for every worker. Each worker sends a message when
// it has received the expected number of packets. After that it disconnects. // it has received the expected number of packets. After that it disconnects.
for (const key in cluster.workers) { for (const key in cluster.workers) {
if (cluster.workers.hasOwnProperty(key)) if (Object.hasOwn(cluster.workers, key))
setupWorker(cluster.workers[key]); setupWorker(cluster.workers[key]);
} }

View File

@ -9,14 +9,14 @@ const { Worker, isMainThread } = require('worker_threads');
// eslint-disable-next-line no-proto // eslint-disable-next-line no-proto
assert.strictEqual(Object.prototype.__proto__, undefined); assert.strictEqual(Object.prototype.__proto__, undefined);
assert(!Object.prototype.hasOwnProperty('__proto__')); assert(!Object.hasOwn(Object.prototype, '__proto__'));
const ctx = vm.createContext(); const ctx = vm.createContext();
const ctxGlobal = vm.runInContext('this', ctx); const ctxGlobal = vm.runInContext('this', ctx);
// eslint-disable-next-line no-proto // eslint-disable-next-line no-proto
assert.strictEqual(ctxGlobal.Object.prototype.__proto__, undefined); assert.strictEqual(ctxGlobal.Object.prototype.__proto__, undefined);
assert(!ctxGlobal.Object.prototype.hasOwnProperty('__proto__')); assert(!Object.hasOwn(ctxGlobal.Object.prototype, '__proto__'));
if (isMainThread) { if (isMainThread) {
new Worker(__filename); new Worker(__filename);

View File

@ -7,7 +7,7 @@ const assert = require('assert');
const vm = require('vm'); const vm = require('vm');
const { Worker, isMainThread } = require('worker_threads'); const { Worker, isMainThread } = require('worker_threads');
assert(Object.prototype.hasOwnProperty('__proto__')); assert(Object.hasOwn(Object.prototype, '__proto__'));
assert.throws(() => { assert.throws(() => {
// eslint-disable-next-line no-proto,no-unused-expressions // eslint-disable-next-line no-proto,no-unused-expressions

View File

@ -32,7 +32,7 @@ const events = require('events');
for (let i = 0; i < 10; i++) { for (let i = 0; i < 10; i++) {
e.on('default', common.mustNotCall()); e.on('default', common.mustNotCall());
} }
assert.ok(!e._events.default.hasOwnProperty('warned')); assert.ok(!Object.hasOwn(e._events.default, 'warned'));
e.on('default', common.mustNotCall()); e.on('default', common.mustNotCall());
assert.ok(e._events.default.warned); assert.ok(e._events.default.warned);
@ -40,32 +40,32 @@ const events = require('events');
const symbol = Symbol('symbol'); const symbol = Symbol('symbol');
e.setMaxListeners(1); e.setMaxListeners(1);
e.on(symbol, common.mustNotCall()); e.on(symbol, common.mustNotCall());
assert.ok(!e._events[symbol].hasOwnProperty('warned')); assert.ok(!Object.hasOwn(e._events[symbol], 'warned'));
e.on(symbol, common.mustNotCall()); e.on(symbol, common.mustNotCall());
assert.ok(e._events[symbol].hasOwnProperty('warned')); assert.ok(Object.hasOwn(e._events[symbol], 'warned'));
// specific // specific
e.setMaxListeners(5); e.setMaxListeners(5);
for (let i = 0; i < 5; i++) { for (let i = 0; i < 5; i++) {
e.on('specific', common.mustNotCall()); e.on('specific', common.mustNotCall());
} }
assert.ok(!e._events.specific.hasOwnProperty('warned')); assert.ok(!Object.hasOwn(e._events.specific, 'warned'));
e.on('specific', common.mustNotCall()); e.on('specific', common.mustNotCall());
assert.ok(e._events.specific.warned); assert.ok(e._events.specific.warned);
// only one // only one
e.setMaxListeners(1); e.setMaxListeners(1);
e.on('only one', common.mustNotCall()); e.on('only one', common.mustNotCall());
assert.ok(!e._events['only one'].hasOwnProperty('warned')); assert.ok(!Object.hasOwn(e._events['only one'], 'warned'));
e.on('only one', common.mustNotCall()); e.on('only one', common.mustNotCall());
assert.ok(e._events['only one'].hasOwnProperty('warned')); assert.ok(Object.hasOwn(e._events['only one'], 'warned'));
// unlimited // unlimited
e.setMaxListeners(0); e.setMaxListeners(0);
for (let i = 0; i < 1000; i++) { for (let i = 0; i < 1000; i++) {
e.on('unlimited', common.mustNotCall()); e.on('unlimited', common.mustNotCall());
} }
assert.ok(!e._events.unlimited.hasOwnProperty('warned')); assert.ok(!Object.hasOwn(e._events.unlimited, 'warned'));
} }
// process-wide // process-wide
@ -76,16 +76,16 @@ const events = require('events');
for (let i = 0; i < 42; ++i) { for (let i = 0; i < 42; ++i) {
e.on('fortytwo', common.mustNotCall()); e.on('fortytwo', common.mustNotCall());
} }
assert.ok(!e._events.fortytwo.hasOwnProperty('warned')); assert.ok(!Object.hasOwn(e._events.fortytwo, 'warned'));
e.on('fortytwo', common.mustNotCall()); e.on('fortytwo', common.mustNotCall());
assert.ok(e._events.fortytwo.hasOwnProperty('warned')); assert.ok(Object.hasOwn(e._events.fortytwo, 'warned'));
delete e._events.fortytwo.warned; delete e._events.fortytwo.warned;
events.EventEmitter.defaultMaxListeners = 44; events.EventEmitter.defaultMaxListeners = 44;
e.on('fortytwo', common.mustNotCall()); e.on('fortytwo', common.mustNotCall());
assert.ok(!e._events.fortytwo.hasOwnProperty('warned')); assert.ok(!Object.hasOwn(e._events.fortytwo, 'warned'));
e.on('fortytwo', common.mustNotCall()); e.on('fortytwo', common.mustNotCall());
assert.ok(e._events.fortytwo.hasOwnProperty('warned')); assert.ok(Object.hasOwn(e._events.fortytwo, 'warned'));
} }
// But _maxListeners still has precedence over defaultMaxListeners // But _maxListeners still has precedence over defaultMaxListeners
@ -94,9 +94,9 @@ const events = require('events');
const e = new events.EventEmitter(); const e = new events.EventEmitter();
e.setMaxListeners(1); e.setMaxListeners(1);
e.on('uno', common.mustNotCall()); e.on('uno', common.mustNotCall());
assert.ok(!e._events.uno.hasOwnProperty('warned')); assert.ok(!Object.hasOwn(e._events.uno, 'warned'));
e.on('uno', common.mustNotCall()); e.on('uno', common.mustNotCall());
assert.ok(e._events.uno.hasOwnProperty('warned')); assert.ok(Object.hasOwn(e._events.uno, 'warned'));
// chainable // chainable
assert.strictEqual(e, e.setMaxListeners(1)); assert.strictEqual(e, e.setMaxListeners(1));

View File

@ -27,8 +27,8 @@ const fs = require('fs');
fs.stat('.', common.mustSucceed(function(stats) { fs.stat('.', common.mustSucceed(function(stats) {
assert.ok(stats.mtime instanceof Date); assert.ok(stats.mtime instanceof Date);
assert.ok(stats.hasOwnProperty('blksize')); assert.ok(Object.hasOwn(stats, 'blksize'));
assert.ok(stats.hasOwnProperty('blocks')); assert.ok(Object.hasOwn(stats, 'blocks'));
// Confirm that we are not running in the context of the internal binding // Confirm that we are not running in the context of the internal binding
// layer. // layer.
// Ref: https://github.com/nodejs/node/commit/463d6bac8b349acc462d345a6e298a76f7d06fb1 // Ref: https://github.com/nodejs/node/commit/463d6bac8b349acc462d345a6e298a76f7d06fb1

View File

@ -41,7 +41,7 @@ const expectedMethods = Object.keys(expectedHeaders);
const server = http.createServer(common.mustCall((req, res) => { const server = http.createServer(common.mustCall((req, res) => {
res.end(); res.end();
assert(expectedHeaders.hasOwnProperty(req.method), assert(Object.hasOwn(expectedHeaders, req.method),
`${req.method} was an unexpected method`); `${req.method} was an unexpected method`);
const requestHeaders = Object.keys(req.headers); const requestHeaders = Object.keys(req.headers);

View File

@ -25,7 +25,7 @@ events.captureRejections = true;
req.on('response', common.mustCall((res) => { req.on('response', common.mustCall((res) => {
assert.strictEqual(res.statusCode, 500); assert.strictEqual(res.statusCode, 500);
assert.strictEqual(res.headers.hasOwnProperty('content-type'), false); assert.strictEqual(Object.hasOwn(res.headers, 'content-type'), false);
let data = ''; let data = '';
res.setEncoding('utf8'); res.setEncoding('utf8');
res.on('data', common.mustCall((chunk) => { res.on('data', common.mustCall((chunk) => {

View File

@ -85,8 +85,8 @@ assert.throws(() => {
{ {
const myError = new errors.codes.TEST_ERROR_1('foo'); const myError = new errors.codes.TEST_ERROR_1('foo');
assert.strictEqual(myError.code, 'TEST_ERROR_1'); assert.strictEqual(myError.code, 'TEST_ERROR_1');
assert.strictEqual(myError.hasOwnProperty('code'), true); assert.strictEqual(Object.hasOwn(myError, 'code'), true);
assert.strictEqual(myError.hasOwnProperty('name'), false); assert.strictEqual(Object.hasOwn(myError, 'name'), false);
assert.deepStrictEqual(Object.keys(myError), ['code']); assert.deepStrictEqual(Object.keys(myError), ['code']);
const initialName = myError.name; const initialName = myError.name;
myError.code = 'FHQWHGADS'; myError.code = 'FHQWHGADS';

View File

@ -3,7 +3,7 @@ require('../common');
const assert = require('assert'); const assert = require('assert');
// check for existence // check for existence
assert(process.config.variables.hasOwnProperty('node_module_version')); assert(Object.hasOwn(process.config.variables, 'node_module_version'));
// Ensure that `node_module_version` is an Integer > 0 // Ensure that `node_module_version` is an Integer > 0
assert(Number.isInteger(process.config.variables.node_module_version)); assert(Number.isInteger(process.config.variables.node_module_version));

View File

@ -31,7 +31,7 @@ const fs = require('fs');
const path = require('path'); const path = require('path');
// Check for existence of `process.config`. // Check for existence of `process.config`.
assert(process.hasOwnProperty('config')); assert(Object.hasOwn(process, 'config'));
// Ensure that `process.config` is an Object. // Ensure that `process.config` is an Object.
assert.strictEqual(Object(process.config), process.config); assert.strictEqual(Object(process.config), process.config);

View File

@ -39,7 +39,7 @@ if (process.argv[2] === 'you-are-the-child') {
assert.strictEqual(Object.prototype.hasOwnProperty, assert.strictEqual(Object.prototype.hasOwnProperty,
process.env.hasOwnProperty); process.env.hasOwnProperty);
const has = process.env.hasOwnProperty('hasOwnProperty'); const has = Object.hasOwn(process.env, 'hasOwnProperty');
assert.strictEqual(has, false); assert.strictEqual(has, false);
process.env.hasOwnProperty = 'asdf'; process.env.hasOwnProperty = 'asdf';

View File

@ -7,7 +7,7 @@ const assert = require('assert');
// basic // basic
{ {
// Find it on Duplex.prototype // Find it on Duplex.prototype
assert(Duplex.prototype.hasOwnProperty('writableFinished')); assert(Object.hasOwn(Duplex.prototype, 'writableFinished'));
} }
// event // event

View File

@ -7,7 +7,7 @@ const assert = require('assert');
// basic // basic
{ {
// Find it on Readable.prototype // Find it on Readable.prototype
assert(Readable.prototype.hasOwnProperty('readableEnded')); assert(Object.hasOwn(Readable.prototype, 'readableEnded'));
} }
// event // event

View File

@ -7,7 +7,7 @@ const assert = require('assert');
// basic // basic
{ {
// Find it on Writable.prototype // Find it on Writable.prototype
assert(Writable.prototype.hasOwnProperty('writableFinished')); assert(Object.hasOwn(Writable.prototype, 'writableFinished'));
} }
// event // event

View File

@ -422,7 +422,7 @@ class TestWriter extends EE {
{ {
// Verify readableEncoding property // Verify readableEncoding property
assert(R.prototype.hasOwnProperty('readableEncoding')); assert(Object.hasOwn(R.prototype, 'readableEncoding'));
const r = new R({ encoding: 'utf8' }); const r = new R({ encoding: 'utf8' });
assert.strictEqual(r.readableEncoding, 'utf8'); assert.strictEqual(r.readableEncoding, 'utf8');
@ -430,7 +430,7 @@ class TestWriter extends EE {
{ {
// Verify readableObjectMode property // Verify readableObjectMode property
assert(R.prototype.hasOwnProperty('readableObjectMode')); assert(Object.hasOwn(R.prototype, 'readableObjectMode'));
const r = new R({ objectMode: true }); const r = new R({ objectMode: true });
assert.strictEqual(r.readableObjectMode, true); assert.strictEqual(r.readableObjectMode, true);
@ -438,7 +438,7 @@ class TestWriter extends EE {
{ {
// Verify writableObjectMode property // Verify writableObjectMode property
assert(W.prototype.hasOwnProperty('writableObjectMode')); assert(Object.hasOwn(W.prototype, 'writableObjectMode'));
const w = new W({ objectMode: true }); const w = new W({ objectMode: true });
assert.strictEqual(w.writableObjectMode, true); assert.strictEqual(w.writableObjectMode, true);