Skip to content

Commit 8a4536d

Browse files
committed
tools: add lint rule to enforce timer arguments
Add a custom ESLint rule to require that setTimeout() and setInterval() get called with at least two arguments. This prevents omitting the duration or interval.
1 parent defefba commit 8a4536d

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

.eslintrc

+1
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ rules:
128128
assert-fail-single-argument: 2
129129
assert-throws-arguments: [2, { requireTwo: false }]
130130
new-with-error: [2, Error, RangeError, TypeError, SyntaxError, ReferenceError]
131+
timer-arguments: 2
131132

132133
# Global scoped method and vars
133134
globals:

tools/eslint-rules/timer-arguments.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @fileoverview Require at least two arguments when calling setTimeout() or
3+
* setInterval().
4+
* @author Rich Trott
5+
*/
6+
'use strict';
7+
8+
//------------------------------------------------------------------------------
9+
// Rule Definition
10+
//------------------------------------------------------------------------------
11+
12+
function isTimer(name) {
13+
return ['setTimeout', 'setInterval'].includes(name);
14+
}
15+
16+
module.exports = function(context) {
17+
return {
18+
'CallExpression': function(node) {
19+
const name = node.callee.name;
20+
if (isTimer(name) && node.arguments.length < 2) {
21+
context.report(node, `${name} must have at least 2 arguments`);
22+
}
23+
}
24+
};
25+
};

0 commit comments

Comments
 (0)