Skip to content
Merged
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
5 changes: 5 additions & 0 deletions docs/timeout-service.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ The following patterns are considered problems;
window.setTimeout(function() {
// ...
}, 1000) // error: You should use the $timeout service instead of the default window.setTimeout method

// invalid
$window.setTimeout(function() {
// ...
}, 1000) // error: You should use the $timeout service instead of the default window.setTimeout method

The following patterns are **not** considered problems;

Expand Down
6 changes: 6 additions & 0 deletions examples/timeout-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,9 @@ window.setTimeout(function() {
// ...
}, 1000)


// example - valid: false, errorMessage: "You should use the $timeout service instead of the default window.setTimeout method"
$window.setTimeout(function() {
// ...
}, 1000)

17 changes: 16 additions & 1 deletion rules/timeout-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,22 @@ module.exports = {
return {

MemberExpression: function(node) {
if (node.object.name === 'window' && node.property.name === 'setTimeout') {
if (node.property.name !== 'setTimeout') {
return;
}

if (node.object.type === 'Identifier') {
if ((node.object.name === 'window' || node.object.name === '$window')) {
context.report(node, message, {});
}

return;
}

// Detect expression this.$window.setTimeout which is what we would see in ES6 code when using classes
var parentNode = node.object;

if (parentNode.object.type === 'ThisExpression' && parentNode.property.name === '$window') {
context.report(node, message, {});
}
},
Expand Down
15 changes: 10 additions & 5 deletions test/timeout-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,21 @@ var commonFalsePositives = require('./utils/commonFalsePositives');

var eslintTester = new RuleTester();

var message = 'You should use the $timeout service instead of the default window.setTimeout method';

eslintTester.run('timeout-service', rule, {
valid: [
'$timeout(function() {})',
'$timeout(function() {}, 1000)',
'$timeout(function() {}, 1000, true)'
'$timeout(function() {}, 1000, true)',
'nonWindowObject.setTimeout(function() {})'
].concat(commonFalsePositives),
invalid: [
{code: 'window.setTimeout(function() {}, 1000)', errors: [{message: 'You should use the $timeout service instead of the default window.setTimeout method'}]},
{code: 'window.setTimeout(function() {}, 1000, param1)', errors: [{message: 'You should use the $timeout service instead of the default window.setTimeout method'}]},
{code: 'setTimeout(function() {}, 1000)', errors: [{message: 'You should use the $timeout service instead of the default window.setTimeout method'}]},
{code: 'setTimeout(function() {}, 1000, param1)', errors: [{message: 'You should use the $timeout service instead of the default window.setTimeout method'}]}
{code: 'window.setTimeout(function() {}, 1000)', errors: [{message: message}]},
{code: 'window.setTimeout(function() {}, 1000, param1)', errors: [{message: message}]},
{code: '$window.setTimeout(function() {}, 1000)', errors: [{message: message}]},
{code: 'this.$window.setTimeout(function() {}, 1000)', errors: [{message: message}]},
{code: 'setTimeout(function() {}, 1000)', errors: [{message: message}]},
{code: 'setTimeout(function() {}, 1000, param1)', errors: [{message: message}]}
]
});