/
preferInlineDecoratorRule.ts
286 lines (238 loc) · 10.2 KB
/
preferInlineDecoratorRule.ts
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import { IRuleMetadata, RuleFailure, WalkContext } from 'tslint/lib';
import { AbstractRule } from 'tslint/lib/rules';
import { dedent } from 'tslint/lib/utils';
import {
createNodeArray,
Decorator,
forEachChild,
GetAccessorDeclaration,
isGetAccessorDeclaration,
isMethodDeclaration,
isParameter,
isParameterPropertyDeclaration,
isPropertyDeclaration,
isSetAccessorDeclaration,
MethodDeclaration,
Node,
ParameterDeclaration,
ParameterPropertyDeclaration,
PropertyDeclaration,
SetAccessorDeclaration,
SourceFile
} from 'typescript';
import { isNotNullOrUndefined } from './util/isNotNullOrUndefined';
import { objectKeys } from './util/objectKeys';
import { AngularInnerClassDecorators, getDecoratorName, isSameLine } from './util/utils';
const OPTION_GETTERS = 'getters';
const OPTION_METHODS = 'methods';
const OPTION_PARAMETER_PROPERTIES = 'parameter-properties';
const OPTION_PARAMETERS = 'parameters';
const OPTION_PROPERTIES = 'properties';
const OPTION_SETTERS = 'setters';
const OPTION_SAFELIST = 'safelist';
const OPTION_SCHEMA_VALUE = {
oneOf: [
{
type: 'boolean'
},
{
properties: {
items: {
type: 'string'
},
type: 'array',
uniqueItems: true
},
type: 'object'
}
]
};
type OptionKeys =
| typeof OPTION_GETTERS
| typeof OPTION_METHODS
| typeof OPTION_PARAMETER_PROPERTIES
| typeof OPTION_PARAMETERS
| typeof OPTION_PROPERTIES
| typeof OPTION_SETTERS;
type Safelist = Record<typeof OPTION_SAFELIST, ReadonlyArray<string>>;
type OptionValue = boolean | Safelist;
type OptionDictionary = Record<OptionKeys, OptionValue>;
type Declaration =
| GetAccessorDeclaration
| MethodDeclaration
| ParameterDeclaration
| ParameterPropertyDeclaration
| PropertyDeclaration
| SetAccessorDeclaration;
const DEFAULT_OPTIONS: OptionDictionary = {
[OPTION_GETTERS]: true,
[OPTION_METHODS]: true,
[OPTION_PARAMETER_PROPERTIES]: true,
[OPTION_PARAMETERS]: true,
[OPTION_PROPERTIES]: true,
[OPTION_SETTERS]: true
};
const STYLE_GUIDE_LINK = 'https://angular.io/guide/styleguide#style-05-12';
export class Rule extends AbstractRule {
static readonly metadata: IRuleMetadata = {
description: 'Ensures that declarations are on the same line as its decorator(s).',
descriptionDetails: `See more at ${STYLE_GUIDE_LINK}.`,
optionExamples: [
true,
[true, { [OPTION_METHODS]: false }],
[
true,
{
[OPTION_GETTERS]: {
[OPTION_SAFELIST]: [AngularInnerClassDecorators.Input]
},
[OPTION_METHODS]: true,
[OPTION_PARAMETER_PROPERTIES]: false,
[OPTION_PARAMETERS]: false,
[OPTION_PROPERTIES]: {
[OPTION_SAFELIST]: [AngularInnerClassDecorators.Output, 'MyCustomDecorator']
},
[OPTION_SETTERS]: true
}
]
],
options: {
additionalProperties: false,
properties: {
[OPTION_GETTERS]: OPTION_SCHEMA_VALUE,
[OPTION_METHODS]: OPTION_SCHEMA_VALUE,
[OPTION_PARAMETER_PROPERTIES]: OPTION_SCHEMA_VALUE,
[OPTION_PARAMETERS]: OPTION_SCHEMA_VALUE,
[OPTION_PROPERTIES]: OPTION_SCHEMA_VALUE,
[OPTION_SETTERS]: OPTION_SCHEMA_VALUE
},
type: 'object'
},
optionsDescription: dedent`
An optional object with optional \`${OPTION_GETTERS}\`, \`${OPTION_METHODS}\`, \`${OPTION_PARAMETER_PROPERTIES}\`, \`${OPTION_PARAMETERS}\`, \`${OPTION_PROPERTIES}\` and \`${OPTION_SETTERS}\` properties.
The properties can be specifed as booleans or as objects with the property \`${OPTION_SAFELIST}\` containing the names of the decorators that should be ignored. Note that if a declaration is decorated with multiple decorators and at least one of them is present in \`${OPTION_SAFELIST}\`, this declaration is ignored.
* \`${OPTION_GETTERS}\` - requires that ${OPTION_GETTERS} are on the same line as its decorator(s). Defaults to \`true\`.
* \`${OPTION_METHODS}\` - requires that ${OPTION_METHODS} are on the same line as its decorator(s). Defaults to \`true\`.
* \`${OPTION_PARAMETER_PROPERTIES}\` - requires that parameter properties are on the same line as its decorator(s). Defaults to \`true\`.
* \`${OPTION_PARAMETERS}\` - requires that ${OPTION_PARAMETERS} are on the same line as its decorator(s). Defaults to \`true\`.
* \`${OPTION_PROPERTIES}\` - requires that ${OPTION_PROPERTIES} are on the same line as its decorator(s). Defaults to \`true\`.
* \`${OPTION_SETTERS}\` - requires that ${OPTION_SETTERS} are on the same line as its decorator(s). Defaults to \`true\`.
`,
rationale: 'Placing the decorator on the same line usually makes for shorter code and still easily identifies the declarations.',
ruleName: 'prefer-inline-decorator',
type: 'style',
typescriptOnly: true
};
static readonly FAILURE_STRING = `Place declarations on the same line as its decorator(s) (${STYLE_GUIDE_LINK})`;
apply(sourceFile: SourceFile): RuleFailure[] {
const options: OptionDictionary = {
...DEFAULT_OPTIONS,
...this.ruleArguments[0]
};
return this.applyWithFunction(sourceFile, walk, options);
}
isEnabled(): boolean {
return super.isEnabled() && this.areOptionsValid();
}
private areOptionsValid(): boolean {
const { length: ruleArgumentsLength } = this.ruleArguments;
if (ruleArgumentsLength === 0) return true;
if (ruleArgumentsLength > 1) return false;
const {
metadata: { options: ruleOptions }
} = Rule;
const [ruleArgument] = this.ruleArguments as ReadonlyArray<OptionDictionary>;
const ruleArgumentsKeys = objectKeys(ruleArgument);
const propertiesKeys = objectKeys(ruleOptions.properties as OptionDictionary);
return (
ruleArgumentsKeys.every(argumentKey => propertiesKeys.indexOf(argumentKey) !== -1) &&
ruleArgumentsKeys
.map(argumentKey => ruleArgument[argumentKey])
.every(argumentValue => {
if (typeof argumentValue === 'boolean') return true;
if (!argumentValue || typeof argumentValue !== 'object') return false;
const argumentValueKeys = objectKeys(argumentValue);
if (argumentValueKeys.length !== 1) return false;
const safelist = argumentValue[argumentValueKeys[0]];
return Array.isArray(safelist) && safelist.length > 0;
})
);
}
}
const callbackHandler = (walkContext: WalkContext<OptionDictionary>, node: Node): void => {
const {
options: { getters, methods, [OPTION_PARAMETER_PROPERTIES]: parameterProperties, parameters, properties, setters }
} = walkContext;
if (getters && isGetAccessorDeclaration(node)) {
validateGetAccessorDeclaration(walkContext, node);
} else if (methods && isMethodDeclaration(node)) {
validateMethodDeclaration(walkContext, node);
} else if (parameters && isParameter(node) && !isParameterPropertyDeclaration(node, node.parent)) {
validateParameterDeclaration(walkContext, node);
} else if (parameterProperties && isParameterPropertyDeclaration(node, node.parent)) {
validateParameterPropertyDeclaration(walkContext, node);
} else if (properties && isPropertyDeclaration(node)) {
validatePropertyDeclaration(walkContext, node);
} else if (setters && isSetAccessorDeclaration(node)) {
validateSetAccessorDeclaration(walkContext, node);
}
};
const canIgnoreDecorator = (walkContext: WalkContext<OptionDictionary>, decoratorName: string, optionKey: OptionKeys): boolean => {
const {
options: { [optionKey]: optionValue }
} = walkContext;
return optionValue && typeof optionValue === 'object' && optionValue.safelist.indexOf(decoratorName) !== -1;
};
const hasAnyIgnoredDecorator = (
walkContext: WalkContext<OptionDictionary>,
decorators: ReadonlyArray<Decorator>,
optionKey: OptionKeys
): boolean => {
const nonIgnoredDecoratorNames = decorators
.map(getDecoratorName)
.filter(isNotNullOrUndefined)
.filter(decoratorName => !canIgnoreDecorator(walkContext, decoratorName, optionKey));
return decorators.length !== nonIgnoredDecoratorNames.length;
};
const validateDecorators = (
walkContext: WalkContext<OptionDictionary>,
decorators: ReadonlyArray<Decorator>,
declaration: Declaration,
optionKey: OptionKeys
): void => {
if (decorators.length === 0 || hasAnyIgnoredDecorator(walkContext, decorators, optionKey)) return;
const [firstDecorator] = decorators;
const firstDecoratorStartPos = firstDecorator.getStart();
const declarationStartPos = declaration.name.getStart();
if (isSameLine(walkContext.sourceFile, firstDecoratorStartPos, declarationStartPos)) return;
walkContext.addFailureAt(firstDecoratorStartPos, declaration.getWidth(), Rule.FAILURE_STRING);
};
const validateDeclaration = (walkContext: WalkContext<OptionDictionary>, declaration: Declaration, optionKey: OptionKeys): void => {
validateDecorators(walkContext, createNodeArray(declaration.decorators), declaration, optionKey);
};
const validateGetAccessorDeclaration = (walkContext: WalkContext<OptionDictionary>, node: GetAccessorDeclaration): void => {
validateDeclaration(walkContext, node, OPTION_GETTERS);
};
const validateMethodDeclaration = (walkContext: WalkContext<OptionDictionary>, node: MethodDeclaration): void => {
validateDeclaration(walkContext, node, OPTION_METHODS);
};
const validateParameterDeclaration = (walkContext: WalkContext<OptionDictionary>, node: ParameterDeclaration): void => {
validateDeclaration(walkContext, node, OPTION_PARAMETERS);
};
const validateParameterPropertyDeclaration = (walkContext: WalkContext<OptionDictionary>, node: ParameterDeclaration): void => {
validateDeclaration(walkContext, node, OPTION_PARAMETER_PROPERTIES);
};
const validatePropertyDeclaration = (walkContext: WalkContext<OptionDictionary>, node: PropertyDeclaration): void => {
validateDeclaration(walkContext, node, OPTION_PROPERTIES);
};
const validateSetAccessorDeclaration = (walkContext: WalkContext<OptionDictionary>, node: SetAccessorDeclaration): void => {
validateDeclaration(walkContext, node, OPTION_SETTERS);
};
const walk = (walkContext: WalkContext<OptionDictionary>): void => {
const { sourceFile } = walkContext;
const callback = (node: Node): void => {
callbackHandler(walkContext, node);
forEachChild(node, callback);
};
forEachChild(sourceFile, callback);
};