-
Notifications
You must be signed in to change notification settings - Fork 131
/
directive-restrict.js
162 lines (144 loc) · 5.85 KB
/
directive-restrict.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/**
* disallow any other directive restrict than 'A' or 'E'
*
* Not all directive restrictions may be desirable.
* Also it might be desirable to define default restrictions, or explicitly not.
* The default configuration limits the restrictions `AE` and disallows explicitly specifying a default.
* ("directive-restrict": [0, {"restrict": "AE", "explicit": "never"}])
*
* @styleguideReference {johnpapa} `y074` Restrict to Elements and Attributes
* @version 0.12.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/directive-restrict.md'
},
schema: [{
type: 'object',
properties: {
restrict: {
type: 'string',
pattern: '^A|C|E|(AC)|(CA)|(AE)|(EA)|(EC)|(CE)|(AEC)|(ACE)|(EAC)|(CAE)|(ACE)|(AEC)|(CAE)|(ACE)|(AEC)$'
},
explicit: {
enum: ['always', 'never']
}
}
}]
},
create: function(context) {
var options = context.options[0] || {};
var restrictOpt = options.restrict || 'AE';
var explicitRestrict = options.explicit === 'always';
var restrictChars = restrictOpt.split('');
// Example RegExp for AE: /^A?E?$/
var restrictRegExp = new RegExp('^' + restrictChars.join('?') + '?$');
var foundDirectives = [];
var checkedDirectives = [];
var defaultRestrictions = ['AE', 'EA'];
function checkLiteralNode(node) {
if (node.type !== 'Literal') {
return;
}
var directiveNode;
context.getAncestors().some(function(ancestor) {
if (utils.isAngularDirectiveDeclaration(ancestor)) {
directiveNode = ancestor;
return true;
}
});
if (!directiveNode) {
// Try to find an ancestor function used as definition for one of the found directives
context.getAncestors().some(function(ancestor) {
if (isFunctionDeclaration(ancestor)) {
if (!ancestor.id) {
return false;
}
var fnName = ancestor.id.name;
var correspondingDirective = foundDirectives.find(function(directive) {
var directiveFnName = getDirectiveFunctionName(directive);
return directiveFnName === fnName;
});
directiveNode = correspondingDirective;
return true;
}
});
}
// The restrict property was not defined inside of a directive.
if (!directiveNode) {
return;
}
if (!explicitRestrict && defaultRestrictions.indexOf(node.value) !== -1) {
context.report(node, 'No need to explicitly specify a default directive restriction');
return;
}
if (!restrictRegExp.test(node.value)) {
context.report(directiveNode, 'Disallowed directive restriction. It must be one of {{allowed}} in that order', {
allowed: restrictOpt
});
}
checkedDirectives.push(directiveNode);
}
function isFunctionDeclaration(node) {
return node.type === 'FunctionDeclaration';
}
function getDirectiveFunctionName(node) {
var directiveArg = node.arguments[1];
// Three ways of creating a directive function: function expression,
// variable name that references a function, and an array with a function
// as the last item
if (utils.isFunctionType(directiveArg)) {
return directiveArg.id.name;
}
if (utils.isArrayType(directiveArg)) {
directiveArg = directiveArg.elements[directiveArg.elements.length - 1];
if (utils.isIdentifierType(directiveArg)) {
return directiveArg.name;
}
return directiveArg.id.name;
}
if (utils.isIdentifierType(directiveArg)) {
return directiveArg.name;
}
}
return {
CallExpression: function(node) {
if (utils.isAngularDirectiveDeclaration(node)) {
foundDirectives.push(node);
}
},
AssignmentExpression: function(node) {
// Only check for literal member property assignments.
if (node.left.type !== 'MemberExpression') {
return;
}
// Only check setting properties named 'restrict'.
if (node.left.property.name !== 'restrict') {
return;
}
checkLiteralNode(node.right);
},
Property: function(node) {
// This only checks for objects which have defined a literal restrict property.
if (node.key.name !== 'restrict') {
return;
}
checkLiteralNode(node.value);
},
'Program:exit': function() {
if (explicitRestrict) {
foundDirectives.filter(function(directive) {
return checkedDirectives.indexOf(directive) < 0;
}).forEach(function(directiveNode) {
context.report(directiveNode, 'Missing directive restriction');
});
}
}
};
}
};