diff --git a/docs/timeout-service.md b/docs/timeout-service.md index 6182d605..2df3bae9 100644 --- a/docs/timeout-service.md +++ b/docs/timeout-service.md @@ -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; diff --git a/examples/timeout-service.js b/examples/timeout-service.js index ae3b5fdc..2388cc26 100644 --- a/examples/timeout-service.js +++ b/examples/timeout-service.js @@ -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) + diff --git a/rules/timeout-service.js b/rules/timeout-service.js index 6eddd904..c02272d0 100644 --- a/rules/timeout-service.js +++ b/rules/timeout-service.js @@ -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, {}); } }, diff --git a/test/timeout-service.js b/test/timeout-service.js index eaa23b39..1b1f53f1 100644 --- a/test/timeout-service.js +++ b/test/timeout-service.js @@ -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}]} ] });