Skip to content

Commit 804982c

Browse files
BridgeARcodebytere
authored andcommitted
tools: add eslint rule to only pass through 'test' to debuglog
This makes sure all usages of `util.debuglog()` must contain the string 'test' as argument. PR-URL: #32161 Refs: #32078 Reviewed-By: Rich Trott <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]> Reviewed-By: Juan José Arboleda <[email protected]> Reviewed-By: Michaël Zasso <[email protected]>
1 parent 1d2c81f commit 804982c

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

test/.eslintrc.yaml

+36
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,42 @@ rules:
1111
prefer-const: error
1212
symbol-description: off
1313

14+
no-restricted-syntax:
15+
# Config copied from .eslintrc.js
16+
- error
17+
- selector: "CallExpression:matches([callee.name='deepStrictEqual'], [callee.property.name='deepStrictEqual'])[arguments.2.type='Literal']"
18+
message: "Do not use a literal for the third argument of assert.deepStrictEqual()"
19+
- selector: "CallExpression:matches([callee.name='doesNotThrow'], [callee.property.name='doesNotThrow'])"
20+
message: "Do not use `assert.doesNotThrow()`. Write the code without the wrapper and add a comment instead."
21+
- selector: "CallExpression:matches([callee.name='doesNotReject'], [callee.property.name='doesNotReject'])"
22+
message: "Do not use `assert.doesNotReject()`. Write the code without the wrapper and add a comment instead."
23+
- selector: "CallExpression:matches([callee.name='rejects'], [callee.property.name='rejects'])[arguments.length<2]"
24+
message: "`assert.rejects()` must be invoked with at least two arguments."
25+
- selector: "CallExpression[callee.property.name='strictEqual'][arguments.2.type='Literal']"
26+
message: "Do not use a literal for the third argument of assert.strictEqual()"
27+
- selector: "CallExpression:matches([callee.name='throws'], [callee.property.name='throws'])[arguments.1.type='Literal']:not([arguments.1.regex])"
28+
message: "Use an object as second argument of `assert.throws()`."
29+
- selector: "CallExpression:matches([callee.name='throws'], [callee.property.name='throws'])[arguments.length<2]"
30+
message: "`assert.throws()` must be invoked with at least two arguments."
31+
- selector: "CallExpression[callee.name='setTimeout'][arguments.length<2]"
32+
message: "`setTimeout()` must be invoked with at least two arguments."
33+
- selector: "CallExpression[callee.name='setInterval'][arguments.length<2]"
34+
message: "`setInterval()` must be invoked with at least two arguments."
35+
- selector: "ThrowStatement > CallExpression[callee.name=/Error$/]"
36+
message: "Use `new` keyword when throwing an `Error`."
37+
- selector: "CallExpression:matches([callee.name='notDeepStrictEqual'], [callee.property.name='notDeepStrictEqual'])[arguments.0.type='Literal']:not([arguments.1.type='Literal']):not([arguments.1.type='ObjectExpression']):not([arguments.1.type='ArrayExpression']):not([arguments.1.type='UnaryExpression'])"
38+
message: "The first argument should be the `actual`, not the `expected` value."
39+
- selector: "CallExpression:matches([callee.name='notStrictEqual'], [callee.property.name='notStrictEqual'])[arguments.0.type='Literal']:not([arguments.1.type='Literal']):not([arguments.1.type='ObjectExpression']):not([arguments.1.type='ArrayExpression']):not([arguments.1.type='UnaryExpression'])"
40+
message: "The first argument should be the `actual`, not the `expected` value."
41+
- selector: "CallExpression:matches([callee.name='deepStrictEqual'], [callee.property.name='deepStrictEqual'])[arguments.0.type='Literal']:not([arguments.1.type='Literal']):not([arguments.1.type='ObjectExpression']):not([arguments.1.type='ArrayExpression']):not([arguments.1.type='UnaryExpression'])"
42+
message: "The first argument should be the `actual`, not the `expected` value."
43+
- selector: "CallExpression:matches([callee.name='strictEqual'], [callee.property.name='strictEqual'])[arguments.0.type='Literal']:not([arguments.1.type='Literal']):not([arguments.1.type='ObjectExpression']):not([arguments.1.type='ArrayExpression']):not([arguments.1.type='UnaryExpression'])"
44+
message: "The first argument should be the `actual`, not the `expected` value."
45+
- selector: "CallExpression[callee.name='isNaN']"
46+
message: "Use Number.isNaN() instead of the global isNaN() function."
47+
- selector: "VariableDeclarator > CallExpression:matches([callee.name='debuglog'], [callee.property.name='debuglog']):not([arguments.0.value='test'])"
48+
message: "Use 'test' as debuglog value in tests."
49+
1450
# Custom rules in tools/eslint-rules
1551
node-core/prefer-assert-iferror: error
1652
node-core/prefer-assert-methods: error

test/sequential/test-util-debug.js

+1
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ function child(section) {
117117
Object.defineProperty(process.stderr, 'hasColors', {
118118
value: tty.WriteStream.prototype.hasColors
119119
});
120+
// eslint-disable-next-line no-restricted-syntax
120121
const debug = util.debuglog(section);
121122
debug('this', { is: 'a' }, /debugging/);
122123
debug('num=%d str=%s obj=%j', 1, 'a', { foo: 'bar' });

0 commit comments

Comments
 (0)