mirror of
https://github.com/nodejs/node.git
synced 2025-05-03 16:34:41 +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>
62 lines
1.7 KiB
JavaScript
62 lines
1.7 KiB
JavaScript
'use strict';
|
|
|
|
require('../common');
|
|
const assert = require('assert');
|
|
|
|
const {
|
|
Worker, MessageChannel
|
|
} = require('worker_threads');
|
|
|
|
const channel = new MessageChannel();
|
|
const workerData = { mesage: channel.port1 };
|
|
const transferList = [channel.port1];
|
|
const meowScript = () => 'meow';
|
|
|
|
{
|
|
// Should receive the transferList param.
|
|
new Worker(`${meowScript}`, { eval: true, workerData, transferList });
|
|
}
|
|
|
|
{
|
|
// Should work with more than one MessagePort.
|
|
const channel1 = new MessageChannel();
|
|
const channel2 = new MessageChannel();
|
|
const workerData = { message: channel1.port1, message2: channel2.port1 };
|
|
const transferList = [channel1.port1, channel2.port1];
|
|
new Worker(`${meowScript}`, { eval: true, workerData, transferList });
|
|
}
|
|
|
|
{
|
|
const uint8Array = new Uint8Array([ 1, 2, 3, 4 ]);
|
|
assert.strictEqual(uint8Array.length, 4);
|
|
new Worker(`
|
|
const { parentPort, workerData } = require('worker_threads');
|
|
parentPort.postMessage(workerData);
|
|
`, {
|
|
eval: true,
|
|
workerData: uint8Array,
|
|
transferList: [uint8Array.buffer]
|
|
}).on(
|
|
'message',
|
|
(message) =>
|
|
assert.deepStrictEqual(message, Uint8Array.of(1, 2, 3, 4))
|
|
);
|
|
assert.strictEqual(uint8Array.length, 0);
|
|
}
|
|
|
|
{
|
|
// Should throw on non valid transferList input.
|
|
const channel1 = new MessageChannel();
|
|
const channel2 = new MessageChannel();
|
|
const workerData = { message: channel1.port1, message2: channel2.port1 };
|
|
assert.throws(() => new Worker(`${meowScript}`, {
|
|
eval: true,
|
|
workerData,
|
|
transferList: []
|
|
}), {
|
|
code: 'ERR_MISSING_TRANSFERABLE_IN_TRANSFER_LIST',
|
|
message: 'Object that needs transfer was found in message but not ' +
|
|
'listed in transferList'
|
|
});
|
|
}
|