From 0267d27a7d802a559c6dfaeb01f6753329564154 Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Mon, 24 Jan 2022 23:03:17 -0800 Subject: [PATCH] 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 Reviewed-By: Antoine du Hamel Reviewed-By: Darshan Sen Reviewed-By: Mestery Reviewed-By: Anna Henningsen Reviewed-By: Tierney Cyren Reviewed-By: Luigi Pinca --- test/common/report.js | 2 +- test/doctool/test-doctool-versions.mjs | 2 +- .../test-child-process-constructor.js | 2 +- .../test-child-process-validate-stdio.js | 6 ++--- test/parallel/test-cluster-basic.js | 2 +- test/parallel/test-cluster-dgram-1.js | 2 +- test/parallel/test-cluster-dgram-bind-fd.js | 2 +- test/parallel/test-disable-proto-delete.js | 4 +-- test/parallel/test-disable-proto-throw.js | 2 +- ...test-event-emitter-check-listener-leaks.js | 26 +++++++++---------- test/parallel/test-fs-stat.js | 4 +-- .../test-http-client-default-headers-exist.js | 2 +- .../test-http-server-capture-rejections.js | 2 +- test/parallel/test-internal-errors.js | 4 +-- test/parallel/test-module-version.js | 2 +- test/parallel/test-process-config.js | 2 +- test/parallel/test-process-env.js | 2 +- .../test-stream-duplex-writable-finished.js | 2 +- test/parallel/test-stream-readable-ended.js | 2 +- .../parallel/test-stream-writable-finished.js | 2 +- test/parallel/test-stream2-basic.js | 6 ++--- 21 files changed, 40 insertions(+), 40 deletions(-) diff --git a/test/common/report.js b/test/common/report.js index 79d6e97b0f42d2..98e230e769fca9 100644 --- a/test/common/report.js +++ b/test/common/report.js @@ -69,7 +69,7 @@ function _validateContent(report, fields = []) { checkForUnknownFields(report, sections); sections.forEach((section) => { - assert(report.hasOwnProperty(section)); + assert(Object.hasOwn(report, section)); assert(typeof report[section] === 'object' && report[section] !== null); }); diff --git a/test/doctool/test-doctool-versions.mjs b/test/doctool/test-doctool-versions.mjs index 92dedab9cc49ea..87e17ba07277dd 100644 --- a/test/doctool/test-doctool-versions.mjs +++ b/test/doctool/test-doctool-versions.mjs @@ -51,7 +51,7 @@ for (const version of versions) { assert.strictEqual(parts[parts.length - 1], 'x', `'num' from ${tested} doesn't end in '.x'.`); const isEvenRelease = Number.parseInt(parts[expectedLength - 2]) % 2 === 0; - const hasLtsProperty = version.hasOwnProperty('lts'); + const hasLtsProperty = Object.hasOwn(version, 'lts'); if (hasLtsProperty) { // Odd-numbered versions of Node.js are never LTS. assert.ok(isEvenRelease, `${tested} should not be an 'lts' release.`); diff --git a/test/parallel/test-child-process-constructor.js b/test/parallel/test-child-process-constructor.js index 0bd8dd85b0bd84..784304d3ecb8b4 100644 --- a/test/parallel/test-child-process-constructor.js +++ b/test/parallel/test-child-process-constructor.js @@ -78,7 +78,7 @@ child.spawn({ stdio: 'pipe' }); -assert.strictEqual(child.hasOwnProperty('pid'), true); +assert.strictEqual(Object.hasOwn(child, 'pid'), true); assert(Number.isInteger(child.pid)); // Try killing with invalid signal diff --git a/test/parallel/test-child-process-validate-stdio.js b/test/parallel/test-child-process-validate-stdio.js index 6775455fd6538c..d5958c694ff6ff 100644 --- a/test/parallel/test-child-process-validate-stdio.js +++ b/test/parallel/test-child-process-validate-stdio.js @@ -18,9 +18,9 @@ assert.throws(() => getValidStdio(600), expectedError); const stdio1 = []; const result = getValidStdio(stdio1, false); assert.strictEqual(stdio1.length, 3); - assert.strictEqual(result.hasOwnProperty('stdio'), true); - assert.strictEqual(result.hasOwnProperty('ipc'), true); - assert.strictEqual(result.hasOwnProperty('ipcFd'), true); + assert.strictEqual(Object.hasOwn(result, 'stdio'), true); + assert.strictEqual(Object.hasOwn(result, 'ipc'), true); + assert.strictEqual(Object.hasOwn(result, 'ipcFd'), true); } // Should throw if stdio has ipc and sync is true diff --git a/test/parallel/test-cluster-basic.js b/test/parallel/test-cluster-basic.js index 6d788cf363ba90..e53b208ead4963 100644 --- a/test/parallel/test-cluster-basic.js +++ b/test/parallel/test-cluster-basic.js @@ -131,7 +131,7 @@ if (cluster.isWorker) { assert.strictEqual(Object.keys(arguments[0]).length, 4); assert.strictEqual(arguments[0].address, '127.0.0.1'); assert.strictEqual(arguments[0].addressType, 4); - assert(arguments[0].hasOwnProperty('fd')); + assert(Object.hasOwn(arguments[0], 'fd')); assert.strictEqual(arguments[0].fd, undefined); const port = arguments[0].port; assert(Number.isInteger(port)); diff --git a/test/parallel/test-cluster-dgram-1.js b/test/parallel/test-cluster-dgram-1.js index cd96c382c77346..71dcb2accb293c 100644 --- a/test/parallel/test-cluster-dgram-1.js +++ b/test/parallel/test-cluster-dgram-1.js @@ -72,7 +72,7 @@ function primary() { // 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. for (const key in cluster.workers) { - if (cluster.workers.hasOwnProperty(key)) + if (Object.hasOwn(cluster.workers, key)) setupWorker(cluster.workers[key]); } diff --git a/test/parallel/test-cluster-dgram-bind-fd.js b/test/parallel/test-cluster-dgram-bind-fd.js index b2efe58721c818..b819f251633a38 100644 --- a/test/parallel/test-cluster-dgram-bind-fd.js +++ b/test/parallel/test-cluster-dgram-bind-fd.js @@ -63,7 +63,7 @@ function primary() { // 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. for (const key in cluster.workers) { - if (cluster.workers.hasOwnProperty(key)) + if (Object.hasOwn(cluster.workers, key)) setupWorker(cluster.workers[key]); } diff --git a/test/parallel/test-disable-proto-delete.js b/test/parallel/test-disable-proto-delete.js index 90cf9287bb9fc5..3a5b2313225e60 100644 --- a/test/parallel/test-disable-proto-delete.js +++ b/test/parallel/test-disable-proto-delete.js @@ -9,14 +9,14 @@ const { Worker, isMainThread } = require('worker_threads'); // eslint-disable-next-line no-proto assert.strictEqual(Object.prototype.__proto__, undefined); -assert(!Object.prototype.hasOwnProperty('__proto__')); +assert(!Object.hasOwn(Object.prototype, '__proto__')); const ctx = vm.createContext(); const ctxGlobal = vm.runInContext('this', ctx); // eslint-disable-next-line no-proto assert.strictEqual(ctxGlobal.Object.prototype.__proto__, undefined); -assert(!ctxGlobal.Object.prototype.hasOwnProperty('__proto__')); +assert(!Object.hasOwn(ctxGlobal.Object.prototype, '__proto__')); if (isMainThread) { new Worker(__filename); diff --git a/test/parallel/test-disable-proto-throw.js b/test/parallel/test-disable-proto-throw.js index a39cfef0d8bd43..524131a1059db6 100644 --- a/test/parallel/test-disable-proto-throw.js +++ b/test/parallel/test-disable-proto-throw.js @@ -7,7 +7,7 @@ const assert = require('assert'); const vm = require('vm'); const { Worker, isMainThread } = require('worker_threads'); -assert(Object.prototype.hasOwnProperty('__proto__')); +assert(Object.hasOwn(Object.prototype, '__proto__')); assert.throws(() => { // eslint-disable-next-line no-proto,no-unused-expressions diff --git a/test/parallel/test-event-emitter-check-listener-leaks.js b/test/parallel/test-event-emitter-check-listener-leaks.js index 037d090e6332a4..04ef3ddab1b0d8 100644 --- a/test/parallel/test-event-emitter-check-listener-leaks.js +++ b/test/parallel/test-event-emitter-check-listener-leaks.js @@ -32,7 +32,7 @@ const events = require('events'); for (let i = 0; i < 10; i++) { 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()); assert.ok(e._events.default.warned); @@ -40,32 +40,32 @@ const events = require('events'); const symbol = Symbol('symbol'); e.setMaxListeners(1); 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()); - assert.ok(e._events[symbol].hasOwnProperty('warned')); + assert.ok(Object.hasOwn(e._events[symbol], 'warned')); // specific e.setMaxListeners(5); for (let i = 0; i < 5; i++) { 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()); assert.ok(e._events.specific.warned); // only one e.setMaxListeners(1); 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()); - assert.ok(e._events['only one'].hasOwnProperty('warned')); + assert.ok(Object.hasOwn(e._events['only one'], 'warned')); // unlimited e.setMaxListeners(0); for (let i = 0; i < 1000; i++) { e.on('unlimited', common.mustNotCall()); } - assert.ok(!e._events.unlimited.hasOwnProperty('warned')); + assert.ok(!Object.hasOwn(e._events.unlimited, 'warned')); } // process-wide @@ -76,16 +76,16 @@ const events = require('events'); for (let i = 0; i < 42; ++i) { 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()); - assert.ok(e._events.fortytwo.hasOwnProperty('warned')); + assert.ok(Object.hasOwn(e._events.fortytwo, 'warned')); delete e._events.fortytwo.warned; events.EventEmitter.defaultMaxListeners = 44; 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()); - assert.ok(e._events.fortytwo.hasOwnProperty('warned')); + assert.ok(Object.hasOwn(e._events.fortytwo, 'warned')); } // But _maxListeners still has precedence over defaultMaxListeners @@ -94,9 +94,9 @@ const events = require('events'); const e = new events.EventEmitter(); e.setMaxListeners(1); 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()); - assert.ok(e._events.uno.hasOwnProperty('warned')); + assert.ok(Object.hasOwn(e._events.uno, 'warned')); // chainable assert.strictEqual(e, e.setMaxListeners(1)); diff --git a/test/parallel/test-fs-stat.js b/test/parallel/test-fs-stat.js index 92a6ec8fa3280c..dc4fc6df8c8067 100644 --- a/test/parallel/test-fs-stat.js +++ b/test/parallel/test-fs-stat.js @@ -27,8 +27,8 @@ const fs = require('fs'); fs.stat('.', common.mustSucceed(function(stats) { assert.ok(stats.mtime instanceof Date); - assert.ok(stats.hasOwnProperty('blksize')); - assert.ok(stats.hasOwnProperty('blocks')); + assert.ok(Object.hasOwn(stats, 'blksize')); + assert.ok(Object.hasOwn(stats, 'blocks')); // Confirm that we are not running in the context of the internal binding // layer. // Ref: https://github.com/nodejs/node/commit/463d6bac8b349acc462d345a6e298a76f7d06fb1 diff --git a/test/parallel/test-http-client-default-headers-exist.js b/test/parallel/test-http-client-default-headers-exist.js index e19be4b7d70856..4c49bae8009cd5 100644 --- a/test/parallel/test-http-client-default-headers-exist.js +++ b/test/parallel/test-http-client-default-headers-exist.js @@ -41,7 +41,7 @@ const expectedMethods = Object.keys(expectedHeaders); const server = http.createServer(common.mustCall((req, res) => { res.end(); - assert(expectedHeaders.hasOwnProperty(req.method), + assert(Object.hasOwn(expectedHeaders, req.method), `${req.method} was an unexpected method`); const requestHeaders = Object.keys(req.headers); diff --git a/test/parallel/test-http-server-capture-rejections.js b/test/parallel/test-http-server-capture-rejections.js index b437e27b589dfc..b11618a6159d65 100644 --- a/test/parallel/test-http-server-capture-rejections.js +++ b/test/parallel/test-http-server-capture-rejections.js @@ -25,7 +25,7 @@ events.captureRejections = true; req.on('response', common.mustCall((res) => { assert.strictEqual(res.statusCode, 500); - assert.strictEqual(res.headers.hasOwnProperty('content-type'), false); + assert.strictEqual(Object.hasOwn(res.headers, 'content-type'), false); let data = ''; res.setEncoding('utf8'); res.on('data', common.mustCall((chunk) => { diff --git a/test/parallel/test-internal-errors.js b/test/parallel/test-internal-errors.js index 7bcc7dcc330c2c..5f1be13801ef1a 100644 --- a/test/parallel/test-internal-errors.js +++ b/test/parallel/test-internal-errors.js @@ -85,8 +85,8 @@ assert.throws(() => { { const myError = new errors.codes.TEST_ERROR_1('foo'); assert.strictEqual(myError.code, 'TEST_ERROR_1'); - assert.strictEqual(myError.hasOwnProperty('code'), true); - assert.strictEqual(myError.hasOwnProperty('name'), false); + assert.strictEqual(Object.hasOwn(myError, 'code'), true); + assert.strictEqual(Object.hasOwn(myError, 'name'), false); assert.deepStrictEqual(Object.keys(myError), ['code']); const initialName = myError.name; myError.code = 'FHQWHGADS'; diff --git a/test/parallel/test-module-version.js b/test/parallel/test-module-version.js index 61b84914e21677..0a8b2427c6e8b4 100644 --- a/test/parallel/test-module-version.js +++ b/test/parallel/test-module-version.js @@ -3,7 +3,7 @@ require('../common'); const assert = require('assert'); // 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 assert(Number.isInteger(process.config.variables.node_module_version)); diff --git a/test/parallel/test-process-config.js b/test/parallel/test-process-config.js index 0cb7e8e8d590d7..a48e107d4c22a3 100644 --- a/test/parallel/test-process-config.js +++ b/test/parallel/test-process-config.js @@ -31,7 +31,7 @@ const fs = require('fs'); const path = require('path'); // Check for existence of `process.config`. -assert(process.hasOwnProperty('config')); +assert(Object.hasOwn(process, 'config')); // Ensure that `process.config` is an Object. assert.strictEqual(Object(process.config), process.config); diff --git a/test/parallel/test-process-env.js b/test/parallel/test-process-env.js index 506e389d8848aa..f0bff13d4c81f1 100644 --- a/test/parallel/test-process-env.js +++ b/test/parallel/test-process-env.js @@ -39,7 +39,7 @@ if (process.argv[2] === 'you-are-the-child') { assert.strictEqual(Object.prototype.hasOwnProperty, process.env.hasOwnProperty); - const has = process.env.hasOwnProperty('hasOwnProperty'); + const has = Object.hasOwn(process.env, 'hasOwnProperty'); assert.strictEqual(has, false); process.env.hasOwnProperty = 'asdf'; diff --git a/test/parallel/test-stream-duplex-writable-finished.js b/test/parallel/test-stream-duplex-writable-finished.js index 6d9e860b6150d5..20c0781a22273d 100644 --- a/test/parallel/test-stream-duplex-writable-finished.js +++ b/test/parallel/test-stream-duplex-writable-finished.js @@ -7,7 +7,7 @@ const assert = require('assert'); // basic { // Find it on Duplex.prototype - assert(Duplex.prototype.hasOwnProperty('writableFinished')); + assert(Object.hasOwn(Duplex.prototype, 'writableFinished')); } // event diff --git a/test/parallel/test-stream-readable-ended.js b/test/parallel/test-stream-readable-ended.js index f504330cb165fc..bdd714c9554b81 100644 --- a/test/parallel/test-stream-readable-ended.js +++ b/test/parallel/test-stream-readable-ended.js @@ -7,7 +7,7 @@ const assert = require('assert'); // basic { // Find it on Readable.prototype - assert(Readable.prototype.hasOwnProperty('readableEnded')); + assert(Object.hasOwn(Readable.prototype, 'readableEnded')); } // event diff --git a/test/parallel/test-stream-writable-finished.js b/test/parallel/test-stream-writable-finished.js index 862587784a4430..933a80a2f94930 100644 --- a/test/parallel/test-stream-writable-finished.js +++ b/test/parallel/test-stream-writable-finished.js @@ -7,7 +7,7 @@ const assert = require('assert'); // basic { // Find it on Writable.prototype - assert(Writable.prototype.hasOwnProperty('writableFinished')); + assert(Object.hasOwn(Writable.prototype, 'writableFinished')); } // event diff --git a/test/parallel/test-stream2-basic.js b/test/parallel/test-stream2-basic.js index 862e45417626c8..2670deda537c51 100644 --- a/test/parallel/test-stream2-basic.js +++ b/test/parallel/test-stream2-basic.js @@ -422,7 +422,7 @@ class TestWriter extends EE { { // Verify readableEncoding property - assert(R.prototype.hasOwnProperty('readableEncoding')); + assert(Object.hasOwn(R.prototype, 'readableEncoding')); const r = new R({ encoding: 'utf8' }); assert.strictEqual(r.readableEncoding, 'utf8'); @@ -430,7 +430,7 @@ class TestWriter extends EE { { // Verify readableObjectMode property - assert(R.prototype.hasOwnProperty('readableObjectMode')); + assert(Object.hasOwn(R.prototype, 'readableObjectMode')); const r = new R({ objectMode: true }); assert.strictEqual(r.readableObjectMode, true); @@ -438,7 +438,7 @@ class TestWriter extends EE { { // Verify writableObjectMode property - assert(W.prototype.hasOwnProperty('writableObjectMode')); + assert(Object.hasOwn(W.prototype, 'writableObjectMode')); const w = new W({ objectMode: true }); assert.strictEqual(w.writableObjectMode, true);