Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
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.
  • Loading branch information
Trott committed Jan 6, 2017
commit 8a4536d3035df430123c00f40c8d57e325e2b05b
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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if it matters, but name will be undefined for calls like foo.bar().

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that's fine but would be happy to add a check for the situation if others feel differently.

if (isTimer(name) && node.arguments.length < 2) {
context.report(node, `${name} must have at least 2 arguments`);
}
}
};
};