-
Notifications
You must be signed in to change notification settings - Fork 131
/
controller-as-route.js
95 lines (85 loc) · 4.03 KB
/
controller-as-route.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/**
* require the use of controllerAs in routes or states
*
* You should use Angular's controllerAs syntax when defining routes or states.
*
* @styleguideReference {johnpapa} `y031` controllerAs Controller Syntax
* @version 0.1.0
* @category bestPractice
* @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/controller-as-route.md'
},
schema: []
},
create: function(context) {
return {
CallExpression: function(node) {
var routeObject = null;
var stateObject = null;
var hasControllerAs = false;
var controllerProp = null;
var stateName = null;
if (utils.isRouteDefinition(node)) {
// second argument in $routeProvider.when('route', {...})
routeObject = node.arguments[1];
if (routeObject.properties) {
routeObject.properties.forEach(function(prop) {
if (prop.key.name === 'controller') {
controllerProp = prop;
if (new RegExp('\\sas\\s').test(prop.value.value)) {
hasControllerAs = true;
}
}
if (prop.key.name === 'controllerAs') {
if (hasControllerAs) {
context.report(node, 'The controllerAs syntax is defined twice for the route "{{route}}"', {
route: node.arguments[0].value
});
}
hasControllerAs = true;
}
});
// if it's a route without a controller, we shouldn't warn about controllerAs
if (controllerProp && !hasControllerAs) {
context.report(node, 'Route "{{route}}" should use controllerAs syntax', {
route: node.arguments[0].value
});
}
}
} else if (utils.isUIRouterStateDefinition(node)) {
// state can be defined like .state({...}) or .state('name', {...})
var isObjectState = node.arguments.length === 1;
stateObject = isObjectState ? node.arguments[0] : node.arguments[1];
if (stateObject && stateObject.properties) {
stateObject.properties.forEach(function(prop) {
if (prop.key.name === 'controller') {
controllerProp = prop;
}
if (prop.key.name === 'controllerAs') {
hasControllerAs = true;
}
// grab the name from the object for when they aren't using .state('name',...)
if (prop.key.name === 'name') {
stateName = prop.value.value;
}
});
if (!hasControllerAs && controllerProp) {
// if the controller is a string, controllerAs can be set like 'controller as vm'
if (controllerProp.value.type !== 'Literal' || controllerProp.value.value.indexOf(' as ') < 0) {
context.report(node, 'State "{{state}}" should use controllerAs syntax', {
state: isObjectState ? stateName : node.arguments[0].value
});
}
}
}
}
}
};
}
};