mirror of https://github.com/nodejs/node.git
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: https://github.com/nodejs/node/pull/9472 Reviewed-By: Teddy Katz <teddy.katz@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>pull/9290/head
parent
6263c00828
commit
4e5cf85ada
|
@ -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:
|
||||
|
|
|
@ -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`);
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
Loading…
Reference in New Issue