forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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: nodejs#9472 Reviewed-By: Teddy Katz <[email protected]> Reviewed-By: James M Snell <[email protected]>
- Loading branch information
1 parent
670062d
commit 49bbdbb
Showing
2 changed files
with
26 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`); | ||
} | ||
} | ||
}; | ||
}; |