Skip to content

Commit

Permalink
tools: improve valid-typeof lint rule
Browse files Browse the repository at this point in the history
Require that `typeof` comparisons be to string literals.

PR-URL: #37924
Reviewed-By: Antoine du Hamel <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Michaël Zasso <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
Trott authored and targos committed May 1, 2021
1 parent 7b0e4e2 commit 4254315
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ module.exports = {
'template-curly-spacing': 'error',
'unicode-bom': 'error',
'use-isnan': 'error',
'valid-typeof': 'error',
'valid-typeof': ['error', { requireStringLiterals: true }],

// Custom rules from eslint-plugin-node-core
'node-core/no-unescaped-regexp-dot': 'error',
Expand Down
1 change: 1 addition & 0 deletions lib/internal/encoding.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ function validateDecoder(obj) {
}

function validateArgument(prop, expected, propName, expectedName) {
// eslint-disable-next-line valid-typeof
if (typeof prop !== expected)
throw new ERR_INVALID_ARG_TYPE(propName, expectedName, prop);
}
Expand Down
12 changes: 3 additions & 9 deletions test/parallel/test-assert-calltracker-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,16 @@ function foo() {}
const callsfoo = tracker.calls(foo, 1);

// Ensures that foo was added to the callChecks array.
if (tracker.report()[0].operator !== 'foo') {
process.exit(1);
}
assert.strictEqual(tracker.report()[0].operator, 'foo');

callsfoo();

// Ensures that foo was removed from the callChecks array after being called the
// expected number of times.
if (typeof tracker.report()[0] === 'undefined') {
process.exit(1);
}
assert.strictEqual(typeof tracker.report()[0], 'undefined');

callsfoo();

// Ensures that foo was added back to the callChecks array after being called
// more than the expected number of times.
if (tracker.report()[0].operator !== 'foo') {
process.exit(1);
}
assert.strictEqual(tracker.report()[0].operator, 'foo');

0 comments on commit 4254315

Please sign in to comment.