Skip to content

Commit 3b8152a

Browse files
Merge pull request #435 from swar30/issue-434/support-$window-in-timeout-service
issue 434 - $window support in timeout-service
2 parents 818aedb + f1106da commit 3b8152a

File tree

4 files changed

+37
-6
lines changed

4 files changed

+37
-6
lines changed

docs/timeout-service.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ The following patterns are considered problems;
2626
window.setTimeout(function() {
2727
// ...
2828
}, 1000) // error: You should use the $timeout service instead of the default window.setTimeout method
29+
30+
// invalid
31+
$window.setTimeout(function() {
32+
// ...
33+
}, 1000) // error: You should use the $timeout service instead of the default window.setTimeout method
2934

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

examples/timeout-service.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,9 @@ window.setTimeout(function() {
1515
// ...
1616
}, 1000)
1717

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

rules/timeout-service.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,22 @@ module.exports = {
2020
return {
2121

2222
MemberExpression: function(node) {
23-
if (node.object.name === 'window' && node.property.name === 'setTimeout') {
23+
if (node.property.name !== 'setTimeout') {
24+
return;
25+
}
26+
27+
if (node.object.type === 'Identifier') {
28+
if ((node.object.name === 'window' || node.object.name === '$window')) {
29+
context.report(node, message, {});
30+
}
31+
32+
return;
33+
}
34+
35+
// Detect expression this.$window.setTimeout which is what we would see in ES6 code when using classes
36+
var parentNode = node.object;
37+
38+
if (parentNode.object.type === 'ThisExpression' && parentNode.property.name === '$window') {
2439
context.report(node, message, {});
2540
}
2641
},

test/timeout-service.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,21 @@ var commonFalsePositives = require('./utils/commonFalsePositives');
1414

1515
var eslintTester = new RuleTester();
1616

17+
var message = 'You should use the $timeout service instead of the default window.setTimeout method';
18+
1719
eslintTester.run('timeout-service', rule, {
1820
valid: [
1921
'$timeout(function() {})',
2022
'$timeout(function() {}, 1000)',
21-
'$timeout(function() {}, 1000, true)'
23+
'$timeout(function() {}, 1000, true)',
24+
'nonWindowObject.setTimeout(function() {})'
2225
].concat(commonFalsePositives),
2326
invalid: [
24-
{code: 'window.setTimeout(function() {}, 1000)', errors: [{message: 'You should use the $timeout service instead of the default window.setTimeout method'}]},
25-
{code: 'window.setTimeout(function() {}, 1000, param1)', errors: [{message: 'You should use the $timeout service instead of the default window.setTimeout method'}]},
26-
{code: 'setTimeout(function() {}, 1000)', errors: [{message: 'You should use the $timeout service instead of the default window.setTimeout method'}]},
27-
{code: 'setTimeout(function() {}, 1000, param1)', errors: [{message: 'You should use the $timeout service instead of the default window.setTimeout method'}]}
27+
{code: 'window.setTimeout(function() {}, 1000)', errors: [{message: message}]},
28+
{code: 'window.setTimeout(function() {}, 1000, param1)', errors: [{message: message}]},
29+
{code: '$window.setTimeout(function() {}, 1000)', errors: [{message: message}]},
30+
{code: 'this.$window.setTimeout(function() {}, 1000)', errors: [{message: message}]},
31+
{code: 'setTimeout(function() {}, 1000)', errors: [{message: message}]},
32+
{code: 'setTimeout(function() {}, 1000, param1)', errors: [{message: message}]}
2833
]
2934
});

0 commit comments

Comments
 (0)