Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 8ffcb2d

Browse filesBrowse files
Trotttargos
authored andcommitted
tools: prevent string literals in some assertions
String literals provided as the third argument to assert.strictEqual() or assert.deepStrictEqual() will mask the values that are causing issues. Use a lint rule to prevent such usage. Backport-PR-URL: #22912 PR-URL: #22849 Reviewed-By: Teddy Katz <teddy.katz@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
1 parent ff6e4ea commit 8ffcb2d
Copy full SHA for 8ffcb2d

File tree

Expand file treeCollapse file tree

5 files changed

+18
-9
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

5 files changed

+18
-9
lines changed
Open diff view settings
Collapse file

‎.eslintrc.js‎

Copy file name to clipboardExpand all lines: .eslintrc.js
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ module.exports = {
156156
],
157157
/* eslint-disable max-len */
158158
// If this list is modified, please copy the change to lib/.eslintrc.yaml
159+
// and test/.eslintrc.yaml.
159160
'no-restricted-syntax': [
160161
'error',
161162
{
@@ -166,6 +167,10 @@ module.exports = {
166167
selector: "CallExpression[callee.object.name='assert'][callee.property.name='rejects'][arguments.length<2]",
167168
message: 'assert.rejects() must be invoked with at least two arguments.',
168169
},
170+
{
171+
selector: "CallExpression[callee.object.name='assert'][callee.property.name='strictEqual'][arguments.2.type='Literal']",
172+
message: 'Do not use a literal for the third argument of assert.strictEqual()'
173+
},
169174
{
170175
selector: "CallExpression[callee.object.name='assert'][callee.property.name='throws'][arguments.1.type='Literal']:not([arguments.1.regex])",
171176
message: 'Use an object as second argument of assert.throws()',
Collapse file

‎lib/.eslintrc.yaml‎

Copy file name to clipboardExpand all lines: lib/.eslintrc.yaml
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@ rules:
22
no-restricted-syntax:
33
# Config copied from .eslintrc.js
44
- error
5+
- selector: "CallExpression[callee.object.name='assert'][callee.property.name='deepStrictEqual'][arguments.2.type='Literal']"
6+
message: "Do not use a literal for the third argument of assert.deepStrictEqual()"
57
- selector: "CallExpression[callee.object.name='assert'][callee.property.name='doesNotThrow']"
68
message: "Please replace `assert.doesNotThrow()` and add a comment next to the code instead."
79
- selector: "CallExpression[callee.object.name='assert'][callee.property.name='rejects'][arguments.length<2]"
810
message: "assert.rejects() must be invoked with at least two arguments."
11+
- selector: "CallExpression[callee.object.name='assert'][callee.property.name='strictEqual'][arguments.2.type='Literal']"
12+
message: "Do not use a literal for the third argument of assert.strictEqual()"
913
- selector: "CallExpression[callee.object.name='assert'][callee.property.name='throws'][arguments.1.type='Literal']:not([arguments.1.regex])"
1014
message: "Use an object as second argument of assert.throws()"
1115
- selector: "CallExpression[callee.object.name='assert'][callee.property.name='throws'][arguments.length<2]"
Collapse file

‎test/.eslintrc.yaml‎

Copy file name to clipboardExpand all lines: test/.eslintrc.yaml
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,14 @@ rules:
2323
no-restricted-syntax:
2424
# Config copied from .eslintrc.js
2525
- 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()"
2628
- selector: "CallExpression[callee.object.name='assert'][callee.property.name='doesNotThrow']"
2729
message: "Please replace `assert.doesNotThrow()` and add a comment next to the code instead."
2830
- selector: "CallExpression[callee.object.name='assert'][callee.property.name='rejects'][arguments.length<2]"
2931
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()"
3034
- selector: "CallExpression[callee.object.name='assert'][callee.property.name='throws'][arguments.1.type='Literal']:not([arguments.1.regex])"
3135
message: "Use an object as second argument of assert.throws()"
3236
- selector: "CallExpression[callee.object.name='assert'][callee.property.name='throws'][arguments.length<2]"
Collapse file

‎test/parallel/test-timers-same-timeout-wrong-list-deleted.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-timers-same-timeout-wrong-list-deleted.js
+3-5Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ const handle1 = setTimeout(common.mustCall(function() {
3737

3838
// Make sure our clearTimeout succeeded. One timer finished and
3939
// the other was canceled, so none should be active.
40-
assert.strictEqual(activeTimers.length, 0, 'Timers remain.');
40+
assert.strictEqual(activeTimers.length, 0);
4141
}));
4242
}));
4343
}), 1);
@@ -53,11 +53,9 @@ const handle1 = setTimeout(common.mustCall(function() {
5353

5454
// Make sure our clearTimeout succeeded. One timer finished and
5555
// the other was canceled, so none should be active.
56-
assert.strictEqual(activeTimers.length, 3,
57-
'There should be 3 timers in the list.');
56+
assert.strictEqual(activeTimers.length, 3);
5857
assert(shortTimer instanceof Timer, 'The shorter timer is not in the list.');
59-
assert.strictEqual(longTimers.length, 2,
60-
'Both longer timers should be in the list.');
58+
assert.strictEqual(longTimers.length, 2);
6159

6260
// When this callback completes, `listOnTimeout` should now look at the
6361
// correct list and refrain from removing the new TIMEOUT list which
Collapse file

‎test/parallel/test-timers-unref-reuse-no-exposed-list.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-timers-unref-reuse-no-exposed-list.js
+2-4Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@ require('../common');
44
const assert = require('assert');
55

66
const timer1 = setTimeout(() => {}, 1).unref();
7-
assert.strictEqual(timer1._handle._list, undefined,
8-
'timer1._handle._list should be undefined');
7+
assert.strictEqual(timer1._handle._list, undefined);
98

109
// Check that everything works even if the handle was not re-used.
1110
setTimeout(() => {}, 1);
1211
const timer2 = setTimeout(() => {}, 1).unref();
13-
assert.strictEqual(timer2._handle._list, undefined,
14-
'timer2._handle._list should be undefined');
12+
assert.strictEqual(timer2._handle._list, undefined);

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.