Skip to content

Commit b7661a8

Browse files
committed
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. PR-URL: nodejs#22849 Reviewed-By: Teddy Katz <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent 9c6e23d commit b7661a8

File tree

3 files changed

+13
-0
lines changed

3 files changed

+13
-0
lines changed

.eslintrc.js

+5
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()',

lib/.eslintrc.yaml

+4
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]"

test/.eslintrc.yaml

+4
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]"

0 commit comments

Comments
 (0)