Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tools,test: provide duration/interval to timers, require it with lint rule #9472

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
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
2 changes: 1 addition & 1 deletion test/message/timeout_throw.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ require('../common');
setTimeout(function() {
// eslint-disable-next-line no-undef
undefined_reference_error_maker;
});
}, 1);
2 changes: 1 addition & 1 deletion test/parallel/test-handle-wrap-close-abort.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ setTimeout(function() {
setTimeout(function() {
throw new Error('setTimeout');
}, 1);
});
}, 1);
2 changes: 1 addition & 1 deletion test/parallel/test-stream-end-paused.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ stream.pause();
setTimeout(common.mustCall(function() {
stream.on('end', common.mustCall(function() {}));
stream.resume();
}));
}), 1);

process.on('exit', function() {
assert(calledRead);
Expand Down
6 changes: 3 additions & 3 deletions test/parallel/test-stream-readable-event.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const Readable = require('stream').Readable;
// we're testing what we think we are
assert(!r._readableState.reading);
r.on('readable', common.mustCall(function() {}));
});
}, 1);
}

{
Expand All @@ -40,7 +40,7 @@ const Readable = require('stream').Readable;
// assert we're testing what we think we are
assert(r._readableState.reading);
r.on('readable', common.mustCall(function() {}));
});
}, 1);
}

{
Expand All @@ -60,5 +60,5 @@ const Readable = require('stream').Readable;
// assert we're testing what we think we are
assert(!r._readableState.reading);
r.on('readable', common.mustCall(function() {}));
});
}, 1);
}
2 changes: 1 addition & 1 deletion test/parallel/test-stream2-large-read-stall.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function push() {

console.error(' push #%d', pushes);
if (r.push(Buffer.allocUnsafe(PUSHSIZE)))
setTimeout(push);
setTimeout(push, 1);
}

process.on('exit', function() {
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-stream2-readable-non-empty-end.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ test._read = function(size) {
var chunk = chunks[n++];
setTimeout(function() {
test.push(chunk === undefined ? null : chunk);
});
}, 1);
};

test.on('end', thrower);
Expand All @@ -31,7 +31,7 @@ test.on('readable', function() {
if (res) {
bytesread += res.length;
console.error('br=%d len=%d', bytesread, len);
setTimeout(next);
setTimeout(next, 1);
}
test.read(0);
});
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`);
}
}
};
};