mirror of
https://github.com/nodejs/node.git
synced 2025-04-28 13:40:37 +00:00

Comparing any value to any non-RegExp literal or undefined using strictEqual (or notStrictEqual) passes if and only if deepStrictEqual (or notDeepStrictEqual, respectively) passes. Unnecessarily using deep comparisons adds confusion. This patch adds an ESLint rule that forbids the use of deepStrictEqual and notDeepStrictEqual when the expected value (i.e., the second argument) is a non-RegExp literal or undefined. For reference, an ESTree literal is defined as follows. extend interface Literal <: Expression { type: "Literal"; value: string | boolean | null | number | RegExp | bigint; } The value `undefined` is an `Identifier` with `name: 'undefined'`. PR-URL: https://github.com/nodejs/node/pull/40634 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Voltrex <mohammadkeyvanzade94@gmail.com>
34 lines
1.2 KiB
JavaScript
34 lines
1.2 KiB
JavaScript
'use strict';
|
||
const common = require('../common');
|
||
const assert = require('assert');
|
||
const { MessageChannel, receiveMessageOnPort } = require('worker_threads');
|
||
|
||
const { port1, port2 } = new MessageChannel();
|
||
|
||
const message1 = { hello: 'world' };
|
||
const message2 = { foo: 'bar' };
|
||
|
||
// Make sure receiveMessageOnPort() works in a FIFO way, the same way it does
|
||
// when we’re using events.
|
||
assert.strictEqual(receiveMessageOnPort(port2), undefined);
|
||
port1.postMessage(message1);
|
||
port1.postMessage(message2);
|
||
assert.deepStrictEqual(receiveMessageOnPort(port2), { message: message1 });
|
||
assert.deepStrictEqual(receiveMessageOnPort(port2), { message: message2 });
|
||
assert.strictEqual(receiveMessageOnPort(port2), undefined);
|
||
assert.strictEqual(receiveMessageOnPort(port2), undefined);
|
||
|
||
// Make sure message handlers aren’t called.
|
||
port2.on('message', common.mustNotCall());
|
||
port1.postMessage(message1);
|
||
assert.deepStrictEqual(receiveMessageOnPort(port2), { message: message1 });
|
||
port1.close();
|
||
|
||
for (const value of [null, 0, -1, {}, []]) {
|
||
assert.throws(() => receiveMessageOnPort(value), {
|
||
name: 'TypeError',
|
||
code: 'ERR_INVALID_ARG_TYPE',
|
||
message: 'The "port" argument must be a MessagePort instance'
|
||
});
|
||
}
|