Skip to content

Commit

Permalink
tools: add eslint rule to only pass through 'test' to debuglog
Browse files Browse the repository at this point in the history
This makes sure all usages of `util.debuglog()` must contain the
string 'test' as argument.

PR-URL: #32161
Refs: #32078
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
  • Loading branch information
BridgeAR authored and codebytere committed Jun 7, 2020
1 parent cd92052 commit 5d5e66c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
36 changes: 36 additions & 0 deletions test/.eslintrc.yaml
Expand Up @@ -11,6 +11,42 @@ rules:
prefer-const: error
symbol-description: off

no-restricted-syntax:
# Config copied from .eslintrc.js
- error
- selector: "CallExpression:matches([callee.name='deepStrictEqual'], [callee.property.name='deepStrictEqual'])[arguments.2.type='Literal']"
message: "Do not use a literal for the third argument of assert.deepStrictEqual()"
- selector: "CallExpression:matches([callee.name='doesNotThrow'], [callee.property.name='doesNotThrow'])"
message: "Do not use `assert.doesNotThrow()`. Write the code without the wrapper and add a comment instead."
- selector: "CallExpression:matches([callee.name='doesNotReject'], [callee.property.name='doesNotReject'])"
message: "Do not use `assert.doesNotReject()`. Write the code without the wrapper and add a comment instead."
- selector: "CallExpression:matches([callee.name='rejects'], [callee.property.name='rejects'])[arguments.length<2]"
message: "`assert.rejects()` must be invoked with at least two arguments."
- selector: "CallExpression[callee.property.name='strictEqual'][arguments.2.type='Literal']"
message: "Do not use a literal for the third argument of assert.strictEqual()"
- selector: "CallExpression:matches([callee.name='throws'], [callee.property.name='throws'])[arguments.1.type='Literal']:not([arguments.1.regex])"
message: "Use an object as second argument of `assert.throws()`."
- selector: "CallExpression:matches([callee.name='throws'], [callee.property.name='throws'])[arguments.length<2]"
message: "`assert.throws()` must be invoked with at least two arguments."
- selector: "CallExpression[callee.name='setTimeout'][arguments.length<2]"
message: "`setTimeout()` must be invoked with at least two arguments."
- selector: "CallExpression[callee.name='setInterval'][arguments.length<2]"
message: "`setInterval()` must be invoked with at least two arguments."
- selector: "ThrowStatement > CallExpression[callee.name=/Error$/]"
message: "Use `new` keyword when throwing an `Error`."
- 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'])"
message: "The first argument should be the `actual`, not the `expected` value."
- 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'])"
message: "The first argument should be the `actual`, not the `expected` value."
- 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'])"
message: "The first argument should be the `actual`, not the `expected` value."
- 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'])"
message: "The first argument should be the `actual`, not the `expected` value."
- selector: "CallExpression[callee.name='isNaN']"
message: "Use Number.isNaN() instead of the global isNaN() function."
- selector: "VariableDeclarator > CallExpression:matches([callee.name='debuglog'], [callee.property.name='debuglog']):not([arguments.0.value='test'])"
message: "Use 'test' as debuglog value in tests."

# Custom rules in tools/eslint-rules
node-core/prefer-assert-iferror: error
node-core/prefer-assert-methods: error
Expand Down
1 change: 1 addition & 0 deletions test/sequential/test-util-debug.js
Expand Up @@ -117,6 +117,7 @@ function child(section) {
Object.defineProperty(process.stderr, 'hasColors', {
value: tty.WriteStream.prototype.hasColors
});
// eslint-disable-next-line no-restricted-syntax
const debug = util.debuglog(section);
debug('this', { is: 'a' }, /debugging/);
debug('num=%d str=%s obj=%j', 1, 'a', { foo: 'bar' });
Expand Down

0 comments on commit 5d5e66c

Please sign in to comment.