forked from EmmanuelDemey/eslint-plugin-angular
-
Notifications
You must be signed in to change notification settings - Fork 0
/
typecheck-array.js
45 lines (42 loc) · 1.55 KB
/
typecheck-array.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/**
* use `angular.isArray` instead of `typeof` comparisons
*
* You should use the angular.isArray method instead of the default JavaScript implementation (typeof [] === "[object Array]").
*
* @version 0.1.0
* @category angularWrapper
* @sinceAngularVersion 1.x
*/
'use strict';
var utils = require('./utils/utils');
module.exports = {
meta: {
docs: {
url: 'https://github.com/Gillespie59/eslint-plugin-angular/blob/master/docs/rules/typecheck-array.md'
},
schema: []
},
create: function(context) {
function recordError(node, origin) {
if (node.type === 'Literal' && node.value === '[object Array]') {
context.report(origin, 'You should use the angular.isArray method', {});
}
}
return {
MemberExpression: function(node) {
if (node.object.name === 'Array' && node.property.name === 'isArray') {
context.report(node, 'You should use the angular.isArray method', {});
}
},
BinaryExpression: function(node) {
if (node.operator === '===' || node.operator === '!==') {
if (utils.isTypeOfStatement(node.left) || utils.isToStringStatement(node.left)) {
recordError(node.right, node);
} else if (utils.isTypeOfStatement(node.right) || utils.isToStringStatement(node.right)) {
recordError(node.left, node);
}
}
}
};
}
};