lib,test: improve faulty assert usage detection · nodejs/node@1fa5004 · GitHub
Skip to content

Commit 1fa5004

Browse files
committed
lib,test: improve faulty assert usage detection
This improves our custom eslint rules to detect assertions to detect assertions with only a single argument and fixes false negatives in case unary expressions are used. Some rules were extended to also lint our docs and tools and the lib rule was simplified to prohibit most assertion calls. PR-URL: #26569 Refs: #26565 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Richard Lau <riclau@uk.ibm.com>
1 parent 1a0004d commit 1fa5004

5 files changed

Lines changed: 51 additions & 60 deletions

File tree

.eslintrc.js

Lines changed: 40 additions & 8 deletions

lib/.eslintrc.yaml

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,8 @@ rules:
44
no-restricted-syntax:
55
# Config copied from .eslintrc.js
66
- error
7-
- selector: "CallExpression[callee.object.name='assert'][callee.property.name='deepStrictEqual'][arguments.2.type='Literal']"
8-
message: "Do not use a literal for the third argument of assert.deepStrictEqual()"
9-
- selector: "CallExpression[callee.object.name='assert'][callee.property.name='doesNotThrow']"
10-
message: "Please replace `assert.doesNotThrow()` and add a comment next to the code instead."
11-
- selector: "CallExpression[callee.object.name='assert'][callee.property.name='rejects'][arguments.length<2]"
12-
message: "assert.rejects() must be invoked with at least two arguments."
13-
- selector: "CallExpression[callee.object.name='assert'][callee.property.name='strictEqual'][arguments.2.type='Literal']"
14-
message: "Do not use a literal for the third argument of assert.strictEqual()"
15-
- selector: "CallExpression[callee.object.name='assert'][callee.property.name='throws'][arguments.1.type='Literal']:not([arguments.1.regex])"
16-
message: "Use an object as second argument of assert.throws()"
17-
- selector: "CallExpression[callee.object.name='assert'][callee.property.name='throws'][arguments.length<2]"
18-
message: "assert.throws() must be invoked with at least two arguments."
7+
- selector: "CallExpression[callee.object.name='assert']:not([callee.property.name='ok']):not([callee.property.name='fail']):not([callee.property.name='ifError'])"
8+
message: "Please only use simple assertions in ./lib"
199
- selector: "CallExpression[callee.name='setTimeout'][arguments.length<2]"
2010
message: "setTimeout() must be invoked with at least two arguments."
2111
- selector: "CallExpression[callee.name='setInterval'][arguments.length<2]"

lib/internal/js_stream_socket.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const assert = require('assert');
3+
const assert = require('internal/assert');
44
const util = require('util');
55
const { Socket } = require('net');
66
const { JSStream } = internalBinding('js_stream');
@@ -122,8 +122,8 @@ class JSStreamSocket extends Socket {
122122
this[kPendingShutdownRequest] = req;
123123
return 0;
124124
}
125-
assert.strictEqual(this[kCurrentWriteRequest], null);
126-
assert.strictEqual(this[kCurrentShutdownRequest], null);
125+
assert(this[kCurrentWriteRequest] === null);
126+
assert(this[kCurrentShutdownRequest] === null);
127127
this[kCurrentShutdownRequest] = req;
128128

129129
const handle = this._handle;
@@ -148,8 +148,8 @@ class JSStreamSocket extends Socket {
148148
}
149149

150150
doWrite(req, bufs) {
151-
assert.strictEqual(this[kCurrentWriteRequest], null);
152-
assert.strictEqual(this[kCurrentShutdownRequest], null);
151+
assert(this[kCurrentWriteRequest] === null);
152+
assert(this[kCurrentShutdownRequest] === null);
153153

154154
const handle = this._handle;
155155
const self = this;
@@ -214,7 +214,7 @@ class JSStreamSocket extends Socket {
214214

215215
setImmediate(() => {
216216
// Should be already set by net.js
217-
assert.strictEqual(this._handle, null);
217+
assert(this._handle === null);
218218

219219
this.finishWrite(handle, uv.UV_ECANCELED);
220220
this.finishShutdown(handle, uv.UV_ECANCELED);

test/.eslintrc.yaml

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,36 +20,6 @@ rules:
2020
node-core/required-modules: [error, common]
2121
node-core/no-duplicate-requires: off
2222

23-
no-restricted-syntax:
24-
# Config copied from .eslintrc.js
25-
- error
26-
- selector: "CallExpression[callee.object.name='assert'][callee.property.name='deepStrictEqual'][arguments.2.type='Literal']"
27-
message: "Do not use a literal for the third argument of assert.deepStrictEqual()"
28-
- selector: "CallExpression[callee.object.name='assert'][callee.property.name='doesNotThrow']"
29-
message: "Please replace `assert.doesNotThrow()` and add a comment next to the code instead."
30-
- selector: "CallExpression[callee.object.name='assert'][callee.property.name='rejects'][arguments.length<2]"
31-
message: "assert.rejects() must be invoked with at least two arguments."
32-
- selector: "CallExpression[callee.object.name='assert'][callee.property.name='strictEqual'][arguments.2.type='Literal']"
33-
message: "Do not use a literal for the third argument of assert.strictEqual()"
34-
- selector: "CallExpression[callee.object.name='assert'][callee.property.name='throws'][arguments.1.type='Literal']:not([arguments.1.regex])"
35-
message: "Use an object as second argument of assert.throws()"
36-
- selector: "CallExpression[callee.object.name='assert'][callee.property.name='throws'][arguments.length<2]"
37-
message: "assert.throws() must be invoked with at least two arguments."
38-
- selector: "CallExpression[callee.name='setTimeout'][arguments.length<2]"
39-
message: "setTimeout() must be invoked with at least two arguments."
40-
- selector: "CallExpression[callee.name='setInterval'][arguments.length<2]"
41-
message: "setInterval() must be invoked with at least 2 arguments."
42-
- selector: "ThrowStatement > CallExpression[callee.name=/Error$/]"
43-
message: "Use new keyword when throwing an Error."
44-
# Specific to /test
45-
- selector: "CallExpression[callee.object.name='assert'][callee.property.name='notDeepStrictEqual'][arguments.0.type='Literal']:not([arguments.1.type='Literal']):not([arguments.1.type='ObjectExpression']):not([arguments.1.type='ArrayExpression'])"
46-
message: "The first argument should be the `actual`, not the `expected` value."
47-
- selector: "CallExpression[callee.object.name='assert'][callee.property.name='notStrictEqual'][arguments.0.type='Literal']:not([arguments.1.type='Literal']):not([arguments.1.type='ObjectExpression']):not([arguments.1.type='ArrayExpression'])"
48-
message: "The first argument should be the `actual`, not the `expected` value."
49-
- selector: "CallExpression[callee.object.name='assert'][callee.property.name='deepStrictEqual'][arguments.0.type='Literal']:not([arguments.1.type='Literal']):not([arguments.1.type='ObjectExpression']):not([arguments.1.type='ArrayExpression'])"
50-
message: "The first argument should be the `actual`, not the `expected` value."
51-
- selector: "CallExpression[callee.object.name='assert'][callee.property.name='strictEqual'][arguments.0.type='Literal']:not([arguments.1.type='Literal']):not([arguments.1.type='ObjectExpression']):not([arguments.1.type='ArrayExpression'])"
52-
message: "The first argument should be the `actual`, not the `expected` value."
5323
# Global scoped methods and vars
5424
globals:
5525
WebAssembly: false

test/parallel/test-assert.js

Lines changed: 3 additions & 4 deletions

0 commit comments

Comments
 (0)