1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | 'use strict';
|
15 |
|
16 | var utils = require('./utils/utils');
|
17 |
|
18 | module.exports = {
|
19 | meta: {
|
20 | schema: [{
|
21 | type: 'object',
|
22 | properties: {
|
23 | restrict: {
|
24 | type: 'string',
|
25 | pattern: '^A|C|E|(AC)|(CA)|(AE)|(EA)|(EC)|(CE)|(AEC)|(ACE)|(EAC)|(CAE)|(ACE)|(AEC)|(CAE)|(ACE)|(AEC)$'
|
26 | },
|
27 | explicit: {
|
28 | enum: ['always', 'never']
|
29 | }
|
30 | }
|
31 | }]
|
32 | },
|
33 | create: function(context) {
|
34 | var options = context.options[0] || {};
|
35 | var restrictOpt = options.restrict || 'AE';
|
36 | var explicitRestrict = options.explicit === 'always';
|
37 | var restrictChars = restrictOpt.split('');
|
38 |
|
39 |
|
40 | var restrictRegExp = new RegExp('^' + restrictChars.join('?') + '?$');
|
41 | var foundDirectives = [];
|
42 | var checkedDirectives = [];
|
43 | var defaultRestrictions = ['AE', 'EA'];
|
44 |
|
45 | function checkLiteralNode(node) {
|
46 | if (node.type !== 'Literal') {
|
47 | return;
|
48 | }
|
49 | var directiveNode;
|
50 | context.getAncestors().some(function(ancestor) {
|
51 | if (utils.isAngularDirectiveDeclaration(ancestor)) {
|
52 | directiveNode = ancestor;
|
53 | return true;
|
54 | }
|
55 | });
|
56 |
|
57 | if (!directiveNode) {
|
58 | return;
|
59 | }
|
60 | if (!explicitRestrict && defaultRestrictions.indexOf(node.value) !== -1) {
|
61 | context.report(node, 'No need to explicitly specify a default directive restriction');
|
62 | return;
|
63 | }
|
64 |
|
65 | if (!restrictRegExp.test(node.value)) {
|
66 | context.report(directiveNode, 'Disallowed directive restriction. It must be one of {{allowed}} in that order', {
|
67 | allowed: restrictOpt
|
68 | });
|
69 | }
|
70 |
|
71 | checkedDirectives.push(directiveNode);
|
72 | }
|
73 |
|
74 | return {
|
75 | CallExpression: function(node) {
|
76 | if (utils.isAngularDirectiveDeclaration(node)) {
|
77 | foundDirectives.push(node);
|
78 | }
|
79 | },
|
80 | AssignmentExpression: function(node) {
|
81 |
|
82 | if (node.left.type !== 'MemberExpression') {
|
83 | return;
|
84 | }
|
85 |
|
86 | if (node.left.property.name !== 'restrict') {
|
87 | return;
|
88 | }
|
89 | checkLiteralNode(node.right);
|
90 | },
|
91 | Property: function(node) {
|
92 |
|
93 | if (node.key.name !== 'restrict') {
|
94 | return;
|
95 | }
|
96 | checkLiteralNode(node.value);
|
97 | },
|
98 | 'Program:exit': function() {
|
99 | if (explicitRestrict) {
|
100 | foundDirectives.filter(function(directive) {
|
101 | return checkedDirectives.indexOf(directive) < 0;
|
102 | }).forEach(function(directiveNode) {
|
103 | context.report(directiveNode, 'Missing directive restriction');
|
104 | });
|
105 | }
|
106 | }
|
107 | };
|
108 | }
|
109 | };
|