|
1 |
| -const FrameworkObjects = ['Component', 'Route', 'Service', 'Helper']; |
2 |
| -const FrameworkHooks = ['init', 'didReceiveAttrs']; |
3 |
| - |
4 |
| -export default function(context) { |
5 |
| - let services = []; |
6 |
| - let errors = []; |
7 |
| - let members = []; |
8 |
| - |
9 |
| - function isUsingAttrsSnapShots(node) { |
10 |
| - if (node.key.name === 'didReceiveAttrs' && node.value.params.length > 0) { |
11 |
| - node.value.params.forEach((param) => { |
12 |
| - errors.push([param, 'Do not use the previous and new versions of attrs. If you need to do a comparsion on re-render stash the last value and compare the new value.']); |
13 |
| - }) |
14 |
| - } |
15 |
| - } |
16 |
| - |
17 |
| - function isCPMissingDependentKey(node) { |
18 |
| - if (isCP(node)) { |
19 |
| - if (node.value.arguments.length === 1 && node.value.arguments[0].type === 'FunctionExpression') { |
20 |
| - errors.push([node.value.arguments[0], 'Missing dependent key. If you are working around a value being shared acrossed instances use `init`']); |
21 |
| - } |
22 |
| - } |
23 |
| - } |
24 |
| - |
25 |
| - function isCP(node) { |
26 |
| - return node.value.type === 'CallExpression' && node.value.callee.property.name === 'computed'; |
27 |
| - } |
28 |
| - |
29 |
| - function isAttrs(node) { |
30 |
| - if (node.property.name === 'attrs' && node.object.type === 'ThisExpression') { |
31 |
| - errors.push([node.property, 'Do not use this.attrs.']); |
32 |
| - } |
33 |
| - } |
34 |
| - |
35 |
| - function isFrameWorkObject(node) { |
36 |
| - return node.object.type === 'Identifier' && FrameworkObjects.indexOf(node.object.name) === -1; |
37 |
| - } |
38 |
| - |
39 |
| - function isExtendingFrameworkObject(node) { |
40 |
| - if (isExtend(node) && isFrameWorkObject(node)) { |
41 |
| - errors.push([node, 'Do not create subclasses. Please use more compositional patterns described here.']); |
42 |
| - } |
43 |
| - } |
44 |
| - |
45 |
| - function isExtend(node) { |
46 |
| - return node.property.name === 'extend'; |
47 |
| - } |
48 |
| - |
49 |
| - function isGetMethod(node) { |
50 |
| - return node.property.name === 'get' && node.object.type === 'ThisExpression'; |
51 |
| - } |
52 |
| - |
53 |
| - function isService(node) { |
54 |
| - return services.indexOf(node.value) > -1 |
55 |
| - } |
56 |
| - |
57 |
| - function isFrameworkHook() { |
58 |
| - return FrameworkHooks.indexOf(context.getScope().block.parent.key.name) > -1; |
59 |
| - } |
60 |
| - |
61 |
| - function isEagerLazyInjection(node) { |
62 |
| - if (isGetMethod(node) && isService(node.parent.arguments[0]) && isFrameworkHook()) { |
63 |
| - errors.push([node.parent.arguments[0], 'Do not pull on lazy injections in framework hooks. If you need a service eagerly please inject it through an initializer.']); |
64 |
| - } |
65 |
| - } |
66 |
| - |
67 |
| - function isMixin(node) { |
68 |
| - if (isExtend(node)) { |
69 |
| - if (node.parent.arguments.length > 1) { |
70 |
| - node.parent.arguments.filter((arg) => { |
71 |
| - return arg.type === 'Identifier' |
72 |
| - }).forEach((arg) => { |
73 |
| - errors.push([arg, 'Do not use Mixins. Please use more compositional patterns described here.']); |
74 |
| - }); |
75 |
| - } |
76 |
| - } |
77 |
| - } |
78 |
| - |
79 |
| - function collectInjectedProperties(node) { |
80 |
| - if (node.value.type === 'CallExpression' && node.value.callee.property.name === 'service') { |
81 |
| - services.push(node.key.name); |
82 |
| - } |
83 |
| - } |
84 |
| - |
85 |
| - function isInjectedService(node) { |
86 |
| - return node.value.callee && node.value.callee.property.name === 'service'; |
87 |
| - } |
88 |
| - |
89 |
| - function emptyArgs(node) { |
90 |
| - return node.arguments.length === 0; |
91 |
| - } |
92 |
| - |
93 |
| - function isMissingInjectionName(node) { |
94 |
| - if (isInjectedService(node) && emptyArgs(node.value)) { |
95 |
| - errors.push([node.value.callee.property, 'Provide the name as a service to avoid reflection on the property name.']) |
96 |
| - } |
97 |
| - } |
98 |
| - |
99 |
| - function isSettingInCPGetter(node) { |
100 |
| - if (node.parent) { |
101 |
| - // console.log(node.parent.parent.parent) |
102 |
| - } |
103 |
| - } |
104 |
| - |
105 |
| - function isSendAction(node) { |
106 |
| - |
107 |
| - } |
108 |
| - |
109 |
| - function findMethod() { |
110 |
| - let scope = context.getScope().block; |
111 |
| - let top = true; |
112 |
| - let methodName; |
113 |
| - |
114 |
| - while (top) { |
115 |
| - if (scope.parent.type === 'Property') { |
116 |
| - if (scope.parent.parent.parent.type === 'CallExpression' && scope.parent.parent.parent.callee.property.name === 'extend') { |
117 |
| - methodName = scope.parent.key.name; |
118 |
| - top = false; |
119 |
| - } |
120 |
| - } else { |
121 |
| - scope = scope.parent; |
122 |
| - } |
123 |
| - } |
124 |
| - |
125 |
| - return methodName |
126 |
| - } |
127 |
| - |
128 |
| - function isSideEffect(node) { |
129 |
| - if (node.parent.callee && node.object.type === 'ThisExpression' && members.indexOf(node.property.name) === -1) { |
130 |
| - let currentMethod = findMethod(); |
131 |
| - |
132 |
| - if (FrameworkHooks.indexOf(currentMethod) > -1) { |
133 |
| - console.log(node.property.name) |
134 |
| - } |
135 |
| - } |
136 |
| - } |
137 |
| - |
138 |
| - function collectMembers(node) { |
139 |
| - if (node.value.type === 'FunctionExpression') { |
140 |
| - members.push(node.key.name); |
141 |
| - } |
142 |
| - } |
143 |
| - |
144 |
| - return { |
145 |
| - MemberExpression(node) { |
146 |
| - isAttrs(node); |
147 |
| - isExtendingFrameworkObject(node); |
148 |
| - isMixin(node); |
149 |
| - isEagerLazyInjection(node); |
150 |
| - isSendAction(node); |
151 |
| - //isSideEffect(node); |
152 |
| - isSettingInCPGetter(node) |
153 |
| - |
154 |
| - }, |
155 |
| - |
156 |
| - Property(node) { |
157 |
| - collectMembers(node); |
158 |
| - collectInjectedProperties(node); |
159 |
| - isCPMissingDependentKey(node); |
160 |
| - isUsingAttrsSnapShots(node); |
161 |
| - isMissingInjectionName(node); |
162 |
| - |
163 |
| - }, |
164 |
| - |
165 |
| - 'Program:exit'() { |
166 |
| - errors.forEach((err) => { |
167 |
| - context.report(err[0], err[1]); |
168 |
| - }); |
169 |
| - }, |
170 |
| - }; |
| 1 | +module.exports = { |
| 2 | + rules: [ |
| 3 | + './rules/no-observers', |
| 4 | + './rules/no-attrs', |
| 5 | + './rules/no-action-cp', |
| 6 | + './rules/no-component-unit-tests', |
| 7 | + './rules/no-mixins', |
| 8 | + './rules/no-subclassing', |
| 9 | + './rules/no-attrs-snapshots', |
| 10 | + './rules/no-set-interval', |
| 11 | + './rules/no-set-timeout', |
| 12 | + './rules/detect-stateless-service', |
| 13 | + './rules/eager-injections', |
| 14 | + './rules/cp-send-action', |
| 15 | + './rules/missing-dependant-keys', |
| 16 | + './rules/enforce-fastboot' |
| 17 | + ] |
171 | 18 | };
|
172 |
| - |
0 commit comments