Skip to content

Commit e4f7f5c

Browse files
TrottMylesBorins
authored andcommittedMar 9, 2017
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. PR-URL: #9472 Reviewed-By: Teddy Katz <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent e81b1cc commit e4f7f5c

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
@@ -124,6 +124,7 @@ rules:
124124
assert-fail-single-argument: 2
125125
assert-throws-arguments: [2, { requireTwo: false }]
126126
new-with-error: [2, Error, RangeError, TypeError, SyntaxError, ReferenceError]
127+
timer-arguments: 2
127128

128129
# Global scoped method and vars
129130
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)
Please sign in to comment.