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: https://github.com/nodejs/node/pull/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>
This commit is contained in:
parent
9c6e23d10f
commit
b7661a8311
@ -156,6 +156,7 @@ module.exports = {
|
|||||||
],
|
],
|
||||||
/* eslint-disable max-len */
|
/* eslint-disable max-len */
|
||||||
// If this list is modified, please copy the change to lib/.eslintrc.yaml
|
// If this list is modified, please copy the change to lib/.eslintrc.yaml
|
||||||
|
// and test/.eslintrc.yaml.
|
||||||
'no-restricted-syntax': [
|
'no-restricted-syntax': [
|
||||||
'error',
|
'error',
|
||||||
{
|
{
|
||||||
@ -166,6 +167,10 @@ module.exports = {
|
|||||||
selector: "CallExpression[callee.object.name='assert'][callee.property.name='rejects'][arguments.length<2]",
|
selector: "CallExpression[callee.object.name='assert'][callee.property.name='rejects'][arguments.length<2]",
|
||||||
message: 'assert.rejects() must be invoked with at least two arguments.',
|
message: 'assert.rejects() must be invoked with at least two arguments.',
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
selector: "CallExpression[callee.object.name='assert'][callee.property.name='strictEqual'][arguments.2.type='Literal']",
|
||||||
|
message: 'Do not use a literal for the third argument of assert.strictEqual()'
|
||||||
|
},
|
||||||
{
|
{
|
||||||
selector: "CallExpression[callee.object.name='assert'][callee.property.name='throws'][arguments.1.type='Literal']:not([arguments.1.regex])",
|
selector: "CallExpression[callee.object.name='assert'][callee.property.name='throws'][arguments.1.type='Literal']:not([arguments.1.regex])",
|
||||||
message: 'Use an object as second argument of assert.throws()',
|
message: 'Use an object as second argument of assert.throws()',
|
||||||
|
@ -2,10 +2,14 @@ rules:
|
|||||||
no-restricted-syntax:
|
no-restricted-syntax:
|
||||||
# Config copied from .eslintrc.js
|
# Config copied from .eslintrc.js
|
||||||
- error
|
- error
|
||||||
|
- selector: "CallExpression[callee.object.name='assert'][callee.property.name='deepStrictEqual'][arguments.2.type='Literal']"
|
||||||
|
message: "Do not use a literal for the third argument of assert.deepStrictEqual()"
|
||||||
- selector: "CallExpression[callee.object.name='assert'][callee.property.name='doesNotThrow']"
|
- selector: "CallExpression[callee.object.name='assert'][callee.property.name='doesNotThrow']"
|
||||||
message: "Please replace `assert.doesNotThrow()` and add a comment next to the code instead."
|
message: "Please replace `assert.doesNotThrow()` and add a comment next to the code instead."
|
||||||
- selector: "CallExpression[callee.object.name='assert'][callee.property.name='rejects'][arguments.length<2]"
|
- selector: "CallExpression[callee.object.name='assert'][callee.property.name='rejects'][arguments.length<2]"
|
||||||
message: "assert.rejects() must be invoked with at least two arguments."
|
message: "assert.rejects() must be invoked with at least two arguments."
|
||||||
|
- selector: "CallExpression[callee.object.name='assert'][callee.property.name='strictEqual'][arguments.2.type='Literal']"
|
||||||
|
message: "Do not use a literal for the third argument of assert.strictEqual()"
|
||||||
- selector: "CallExpression[callee.object.name='assert'][callee.property.name='throws'][arguments.1.type='Literal']:not([arguments.1.regex])"
|
- selector: "CallExpression[callee.object.name='assert'][callee.property.name='throws'][arguments.1.type='Literal']:not([arguments.1.regex])"
|
||||||
message: "Use an object as second argument of assert.throws()"
|
message: "Use an object as second argument of assert.throws()"
|
||||||
- selector: "CallExpression[callee.object.name='assert'][callee.property.name='throws'][arguments.length<2]"
|
- selector: "CallExpression[callee.object.name='assert'][callee.property.name='throws'][arguments.length<2]"
|
||||||
|
@ -23,10 +23,14 @@ rules:
|
|||||||
no-restricted-syntax:
|
no-restricted-syntax:
|
||||||
# Config copied from .eslintrc.js
|
# Config copied from .eslintrc.js
|
||||||
- error
|
- error
|
||||||
|
- selector: "CallExpression[callee.object.name='assert'][callee.property.name='deepStrictEqual'][arguments.2.type='Literal']"
|
||||||
|
message: "Do not use a literal for the third argument of assert.deepStrictEqual()"
|
||||||
- selector: "CallExpression[callee.object.name='assert'][callee.property.name='doesNotThrow']"
|
- selector: "CallExpression[callee.object.name='assert'][callee.property.name='doesNotThrow']"
|
||||||
message: "Please replace `assert.doesNotThrow()` and add a comment next to the code instead."
|
message: "Please replace `assert.doesNotThrow()` and add a comment next to the code instead."
|
||||||
- selector: "CallExpression[callee.object.name='assert'][callee.property.name='rejects'][arguments.length<2]"
|
- selector: "CallExpression[callee.object.name='assert'][callee.property.name='rejects'][arguments.length<2]"
|
||||||
message: "assert.rejects() must be invoked with at least two arguments."
|
message: "assert.rejects() must be invoked with at least two arguments."
|
||||||
|
- selector: "CallExpression[callee.object.name='assert'][callee.property.name='strictEqual'][arguments.2.type='Literal']"
|
||||||
|
message: "Do not use a literal for the third argument of assert.strictEqual()"
|
||||||
- selector: "CallExpression[callee.object.name='assert'][callee.property.name='throws'][arguments.1.type='Literal']:not([arguments.1.regex])"
|
- selector: "CallExpression[callee.object.name='assert'][callee.property.name='throws'][arguments.1.type='Literal']:not([arguments.1.regex])"
|
||||||
message: "Use an object as second argument of assert.throws()"
|
message: "Use an object as second argument of assert.throws()"
|
||||||
- selector: "CallExpression[callee.object.name='assert'][callee.property.name='throws'][arguments.length<2]"
|
- selector: "CallExpression[callee.object.name='assert'][callee.property.name='throws'][arguments.length<2]"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user