mirror of
https://github.com/nodejs/node.git
synced 2025-05-01 08:42:45 +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>
69 lines
2.0 KiB
JavaScript
69 lines
2.0 KiB
JavaScript
// Flags: --expose-internals
|
|
'use strict';
|
|
require('../common');
|
|
const assert = require('assert');
|
|
const { ERR_INVALID_ARG_TYPE } = require('internal/errors').codes;
|
|
const { serializeError, deserializeError } = require('internal/error_serdes');
|
|
|
|
function cycle(err) {
|
|
return deserializeError(serializeError(err));
|
|
}
|
|
|
|
assert.strictEqual(cycle(0), 0);
|
|
assert.strictEqual(cycle(-1), -1);
|
|
assert.strictEqual(cycle(1.4), 1.4);
|
|
assert.strictEqual(cycle(null), null);
|
|
assert.strictEqual(cycle(undefined), undefined);
|
|
assert.strictEqual(cycle('foo'), 'foo');
|
|
|
|
let err = new Error('foo');
|
|
for (let i = 0; i < 10; i++) {
|
|
assert(err instanceof Error);
|
|
assert(Object.prototype.toString.call(err), '[object Error]');
|
|
assert.strictEqual(err.name, 'Error');
|
|
assert.strictEqual(err.message, 'foo');
|
|
assert.match(err.stack, /^Error: foo\n/);
|
|
|
|
const prev = err;
|
|
err = cycle(err);
|
|
assert.deepStrictEqual(err, prev);
|
|
}
|
|
|
|
assert.strictEqual(cycle(new RangeError('foo')).name, 'RangeError');
|
|
assert.strictEqual(cycle(new TypeError('foo')).name, 'TypeError');
|
|
assert.strictEqual(cycle(new ReferenceError('foo')).name, 'ReferenceError');
|
|
assert.strictEqual(cycle(new URIError('foo')).name, 'URIError');
|
|
assert.strictEqual(cycle(new EvalError('foo')).name, 'EvalError');
|
|
assert.strictEqual(cycle(new SyntaxError('foo')).name, 'SyntaxError');
|
|
|
|
class SubError extends Error {}
|
|
|
|
assert.strictEqual(cycle(new SubError('foo')).name, 'Error');
|
|
|
|
assert.deepStrictEqual(cycle({ message: 'foo' }), { message: 'foo' });
|
|
assert.strictEqual(cycle(Function), '[Function: Function]');
|
|
|
|
{
|
|
const err = new ERR_INVALID_ARG_TYPE('object', 'Object', 42);
|
|
assert.match(String(err), /^TypeError \[ERR_INVALID_ARG_TYPE\]:/);
|
|
assert.strictEqual(err.name, 'TypeError');
|
|
assert.strictEqual(err.code, 'ERR_INVALID_ARG_TYPE');
|
|
}
|
|
|
|
{
|
|
let called = false;
|
|
class DynamicError extends Error {
|
|
get type() {
|
|
called = true;
|
|
return 'dynamic';
|
|
}
|
|
|
|
get shouldIgnoreError() {
|
|
throw new Error();
|
|
}
|
|
}
|
|
|
|
serializeError(new DynamicError());
|
|
assert.strictEqual(called, true);
|
|
}
|