mirror of
https://github.com/nodejs/node.git
synced 2025-04-30 07:19:19 +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>
28 lines
757 B
JavaScript
28 lines
757 B
JavaScript
'use strict';
|
|
|
|
const common = require('../common');
|
|
const assert = require('assert');
|
|
const path = require('path');
|
|
const { open, readFile } = require('fs').promises;
|
|
const tmpdir = require('../common/tmpdir');
|
|
|
|
tmpdir.refresh();
|
|
|
|
async function validateTruncate() {
|
|
const text = 'Hello world';
|
|
const filename = path.resolve(tmpdir.path, 'truncate-file.txt');
|
|
const fileHandle = await open(filename, 'w+');
|
|
|
|
const buffer = Buffer.from(text, 'utf8');
|
|
await fileHandle.write(buffer, 0, buffer.length);
|
|
|
|
assert.strictEqual((await readFile(filename)).toString(), text);
|
|
|
|
await fileHandle.truncate(5);
|
|
assert.strictEqual((await readFile(filename)).toString(), 'Hello');
|
|
|
|
await fileHandle.close();
|
|
}
|
|
|
|
validateTruncate().then(common.mustCall());
|