Skip to content

Commit

Permalink
tools: add lint rule to enforce timer arguments
Browse files Browse the repository at this point in the history
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: nodejs#9472
Reviewed-By: Teddy Katz <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
Trott authored and italoacasas committed Jan 18, 2017
1 parent 670062d commit 49bbdbb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ rules:
assert-fail-single-argument: 2
assert-throws-arguments: [2, { requireTwo: false }]
new-with-error: [2, Error, RangeError, TypeError, SyntaxError, ReferenceError]
timer-arguments: 2

# Global scoped method and vars
globals:
Expand Down
25 changes: 25 additions & 0 deletions tools/eslint-rules/timer-arguments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* @fileoverview Require at least two arguments when calling setTimeout() or
* setInterval().
* @author Rich Trott
*/
'use strict';

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------

function isTimer(name) {
return ['setTimeout', 'setInterval'].includes(name);
}

module.exports = function(context) {
return {
'CallExpression': function(node) {
const name = node.callee.name;
if (isTimer(name) && node.arguments.length < 2) {
context.report(node, `${name} must have at least 2 arguments`);
}
}
};
};

0 comments on commit 49bbdbb

Please sign in to comment.