UNPKG

1.67 kBJavaScriptView Raw
1/**
2 * use `$timeout` instead of `setTimeout`
3 *
4 * Instead of the default setTimeout function, you should use the AngularJS wrapper service $timeout
5 **
6 * @styleguideReference {johnpapa} `y181` Angular $ Wrapper Services - $timeout and $interval
7 * @version 0.1.0
8 * @category angularWrapper
9 * @sinceAngularVersion 1.x
10 */
11'use strict';
12
13module.exports = {
14 meta: {
15 schema: []
16 },
17 create: function(context) {
18 var message = 'You should use the $timeout service instead of the default window.setTimeout method';
19
20 return {
21
22 MemberExpression: function(node) {
23 if (node.property.name !== 'setTimeout' || !node.object) {
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) {
39 return;
40 }
41
42 if (parentNode.object.type === 'ThisExpression' && parentNode.property.name === '$window') {
43 context.report(node, message, {});
44 }
45 },
46
47 CallExpression: function(node) {
48 if (node.callee.name === 'setTimeout') {
49 context.report(node, message, {});
50 }
51 }
52 };
53 }
54};