This repository has been archived by the owner on Mar 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 889
/
rule.ts
363 lines (303 loc) · 9.6 KB
/
rule.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/**
* @license
* Copyright 2013 Palantir Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import * as ts from "typescript";
import { arrayify, flatMap } from "../../utils";
import { IWalker } from "../walker";
export interface RuleConstructor {
metadata: IRuleMetadata;
new (options: IOptions): IRule;
}
export interface IRuleMetadata {
/**
* The kebab-case name of the rule.
*/
ruleName: string;
/**
* The type of the rule - its overall purpose
*/
type: RuleType;
/**
* A rule deprecation message, if applicable.
*/
deprecationMessage?: string;
/**
* A short, one line description of what the rule does.
*/
description: string;
/**
* More elaborate details about the rule.
*/
descriptionDetails?: string;
/**
* Whether or not the rule will provide fix suggestions.
*/
hasFix?: boolean;
/**
* An explanation of the available options for the rule.
*/
optionsDescription: string;
/**
* Schema of the options the rule accepts.
* The first boolean for whether the rule is enabled or not is already implied.
* This field describes the options after that boolean.
* If null, this rule has no options and is not configurable.
*/
options: any;
/**
* Examples of what a standard config for the rule might look like.
* Using a string[] here is deprecated. Write the options as a JSON object instead.
*/
optionExamples?: Array<true | any[]> | string[];
/**
* An explanation of why the rule is useful.
*/
rationale?: string;
/**
* Whether or not the rule requires type info to run.
*/
requiresTypeInfo?: boolean;
/**
* Whether or not the rule use for TypeScript only. If `false`, this rule may be used with .js files.
*/
typescriptOnly: boolean;
/**
* Examples demonstrating what the lint rule will pass and fail
*/
codeExamples?: ICodeExample[];
}
export type RuleType = "functionality" | "maintainability" | "style" | "typescript";
export type RuleSeverity = "warning" | "error" | "off";
export interface ICodeExample {
config: string;
description: string;
pass: string;
fail?: string;
}
export interface IOptions {
ruleArguments: any[];
ruleSeverity: RuleSeverity;
ruleName: string;
/**
* @deprecated
* Tslint now handles disables itself.
* This will be empty.
*/
disabledIntervals: IDisabledInterval[]; // tslint:disable-line deprecation
}
/**
* @deprecated
* These are now handled internally.
*/
export interface IDisabledInterval {
startPosition: number;
endPosition: number;
}
export interface IRule {
getOptions(): IOptions;
isEnabled(): boolean;
apply(sourceFile: ts.SourceFile): RuleFailure[];
applyWithWalker(walker: IWalker): RuleFailure[];
}
export interface ITypedRule extends IRule {
applyWithProgram(sourceFile: ts.SourceFile, program: ts.Program): RuleFailure[];
}
export interface IRuleFailureJson {
endPosition: IRuleFailurePositionJson;
failure: string;
fix?: FixJson;
name: string;
ruleSeverity: string;
ruleName: string;
startPosition: IRuleFailurePositionJson;
}
export interface IRuleFailurePositionJson {
character: number;
line: number;
position: number;
}
export function isTypedRule(rule: IRule): rule is ITypedRule {
return "applyWithProgram" in rule;
}
export interface ReplacementJson {
innerStart: number;
innerLength: number;
innerText: string;
}
export class Replacement {
public static applyFixes(content: string, fixes: Fix[]): string {
return this.applyAll(content, flatMap(fixes, arrayify));
}
public static applyAll(content: string, replacements: Replacement[]) {
// sort in reverse so that diffs are properly applied
replacements.sort((a, b) => (b.end !== a.end ? b.end - a.end : b.start - a.start));
return replacements.reduce((text, r) => r.apply(text), content);
}
public static replaceNode(
node: ts.Node,
text: string,
sourceFile?: ts.SourceFile,
): Replacement {
return this.replaceFromTo(node.getStart(sourceFile), node.getEnd(), text);
}
public static replaceFromTo(start: number, end: number, text: string) {
return new Replacement(start, end - start, text);
}
public static deleteText(start: number, length: number) {
return new Replacement(start, length, "");
}
public static deleteFromTo(start: number, end: number) {
return new Replacement(start, end - start, "");
}
public static appendText(start: number, text: string) {
return new Replacement(start, 0, text);
}
constructor(readonly start: number, readonly length: number, readonly text: string) {}
get end() {
return this.start + this.length;
}
public apply(content: string) {
return (
content.substring(0, this.start) +
this.text +
content.substring(this.start + this.length)
);
}
public toJson(): ReplacementJson {
// tslint:disable object-literal-sort-keys
return {
innerStart: this.start,
innerLength: this.length,
innerText: this.text,
};
// tslint:enable object-literal-sort-keys
}
}
export class RuleFailurePosition {
constructor(
private readonly position: number,
private readonly lineAndCharacter: ts.LineAndCharacter,
) {}
public getPosition() {
return this.position;
}
public getLineAndCharacter() {
return this.lineAndCharacter;
}
public toJson(): IRuleFailurePositionJson {
return {
character: this.lineAndCharacter.character,
line: this.lineAndCharacter.line,
position: this.position,
};
}
public equals(ruleFailurePosition: RuleFailurePosition) {
const ll = this.lineAndCharacter;
const rr = ruleFailurePosition.lineAndCharacter;
return (
this.position === ruleFailurePosition.position &&
ll.line === rr.line &&
ll.character === rr.character
);
}
}
export type Fix = Replacement | Replacement[];
export type FixJson = ReplacementJson | ReplacementJson[];
export class RuleFailure {
public static compare(a: RuleFailure, b: RuleFailure): number {
if (a.fileName !== b.fileName) {
return a.fileName < b.fileName ? -1 : 1;
}
return a.startPosition.getPosition() - b.startPosition.getPosition();
}
private readonly fileName: string;
private readonly startPosition: RuleFailurePosition;
private readonly endPosition: RuleFailurePosition;
private readonly rawLines: string;
private ruleSeverity: RuleSeverity;
constructor(
private readonly sourceFile: ts.SourceFile,
start: number,
end: number,
private readonly failure: string,
private readonly ruleName: string,
private readonly fix?: Fix,
) {
this.fileName = sourceFile.fileName;
this.startPosition = this.createFailurePosition(start);
this.endPosition = this.createFailurePosition(end);
this.rawLines = sourceFile.text;
this.ruleSeverity = "error";
}
public getFileName() {
return this.fileName;
}
public getRuleName() {
return this.ruleName;
}
public getStartPosition(): RuleFailurePosition {
return this.startPosition;
}
public getEndPosition(): RuleFailurePosition {
return this.endPosition;
}
public getFailure() {
return this.failure;
}
public hasFix() {
return this.fix !== undefined;
}
public getFix() {
return this.fix;
}
public getRawLines() {
return this.rawLines;
}
public getRuleSeverity() {
return this.ruleSeverity;
}
public setRuleSeverity(value: RuleSeverity) {
this.ruleSeverity = value;
}
public toJson(): IRuleFailureJson {
return {
endPosition: this.endPosition.toJson(),
failure: this.failure,
fix:
this.fix === undefined
? undefined
: Array.isArray(this.fix)
? this.fix.map(r => r.toJson())
: this.fix.toJson(),
name: this.fileName,
ruleName: this.ruleName,
ruleSeverity: this.ruleSeverity.toUpperCase(),
startPosition: this.startPosition.toJson(),
};
}
public equals(ruleFailure: RuleFailure) {
return (
this.failure === ruleFailure.getFailure() &&
this.fileName === ruleFailure.getFileName() &&
this.startPosition.equals(ruleFailure.getStartPosition()) &&
this.endPosition.equals(ruleFailure.getEndPosition())
);
}
private createFailurePosition(position: number) {
const lineAndCharacter = this.sourceFile.getLineAndCharacterOfPosition(position);
return new RuleFailurePosition(position, lineAndCharacter);
}
}