forked from palantir/tslint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
linter.ts
274 lines (245 loc) · 11.2 KB
/
linter.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
/**
* @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 fs from "fs";
import * as path from "path";
import * as ts from "typescript";
import {
convertRuleOptions,
DEFAULT_CONFIG,
findConfiguration,
findConfigurationPath,
getRulesDirectories,
IConfigurationFile,
loadConfigurationFromPath,
} from "./configuration";
import { removeDisabledFailures } from "./enableDisableRules";
import { FatalError, isError, showWarningOnce } from "./error";
import { findFormatter } from "./formatterLoader";
import { ILinterOptions, LintResult } from "./index";
import { IRule, isTypedRule, Replacement, RuleFailure, RuleSeverity } from "./language/rule/rule";
import * as utils from "./language/utils";
import { loadRules } from "./ruleLoader";
import { arrayify, dedent, flatMap, mapDefined } from "./utils";
/**
* Linter that can lint multiple files in consecutive runs.
*/
export class Linter {
public static VERSION = "5.9.1";
public static findConfiguration = findConfiguration;
public static findConfigurationPath = findConfigurationPath;
public static getRulesDirectories = getRulesDirectories;
public static loadConfigurationFromPath = loadConfigurationFromPath;
private failures: RuleFailure[] = [];
private fixes: RuleFailure[] = [];
/**
* Creates a TypeScript program object from a tsconfig.json file path and optional project directory.
*/
public static createProgram(configFile: string, projectDirectory: string = path.dirname(configFile)): ts.Program {
const config = ts.readConfigFile(configFile, ts.sys.readFile);
if (config.error !== undefined) {
throw new FatalError(ts.formatDiagnostics([config.error], {
getCanonicalFileName: (f) => f,
getCurrentDirectory: process.cwd,
getNewLine: () => "\n",
}));
}
const parseConfigHost: ts.ParseConfigHost = {
fileExists: fs.existsSync,
readDirectory: ts.sys.readDirectory,
readFile: (file) => fs.readFileSync(file, "utf8"),
useCaseSensitiveFileNames: true,
};
const parsed = ts.parseJsonConfigFileContent(config.config, parseConfigHost, path.resolve(projectDirectory), {noEmit: true});
if (parsed.errors !== undefined) {
// ignore warnings and 'TS18003: No inputs were found in config file ...'
const errors = parsed.errors.filter((d) => d.category === ts.DiagnosticCategory.Error && d.code !== 18003);
if (errors.length !== 0) {
throw new FatalError(ts.formatDiagnostics(errors, {
getCanonicalFileName: (f) => f,
getCurrentDirectory: process.cwd,
getNewLine: () => "\n",
}));
}
}
const host = ts.createCompilerHost(parsed.options, true);
const program = ts.createProgram(parsed.fileNames, parsed.options, host);
return program;
}
/**
* Returns a list of source file names from a TypeScript program. This includes all referenced
* files and excludes declaration (".d.ts") files.
*/
public static getFileNames(program: ts.Program): string[] {
return mapDefined(
program.getSourceFiles(),
(file) =>
file.fileName.endsWith(".d.ts") || program.isSourceFileFromExternalLibrary(file)
? undefined
: file.fileName,
);
}
constructor(private readonly options: ILinterOptions, private program?: ts.Program) {
if (typeof options !== "object") {
throw new Error(`Unknown Linter options type: ${typeof options}`);
}
if ((options as any).configuration != undefined) {
throw new Error("ILinterOptions does not contain the property `configuration` as of version 4. " +
"Did you mean to pass the `IConfigurationFile` object to lint() ? ");
}
}
public lint(fileName: string, source: string, configuration: IConfigurationFile = DEFAULT_CONFIG): void {
const sourceFile = this.getSourceFile(fileName, source);
const isJs = /\.jsx?$/i.test(fileName);
const enabledRules = this.getEnabledRules(configuration, isJs);
let fileFailures = this.getAllFailures(sourceFile, enabledRules);
if (fileFailures.length === 0) {
// Usual case: no errors.
return;
}
if (this.options.fix && fileFailures.some((f) => f.hasFix())) {
fileFailures = this.applyAllFixes(enabledRules, fileFailures, sourceFile, fileName);
}
// add rule severity to failures
const ruleSeverityMap = new Map(enabledRules.map(
(rule): [string, RuleSeverity] => [rule.getOptions().ruleName, rule.getOptions().ruleSeverity]));
for (const failure of fileFailures) {
const severity = ruleSeverityMap.get(failure.getRuleName());
if (severity === undefined) {
throw new Error(`Severity for rule '${failure.getRuleName()}' not found`);
}
failure.setRuleSeverity(severity);
}
this.failures = this.failures.concat(fileFailures);
}
public getResult(): LintResult {
const formatterName = this.options.formatter !== undefined ? this.options.formatter : "prose";
const Formatter = findFormatter(formatterName, this.options.formattersDirectory);
if (Formatter === undefined) {
throw new Error(`formatter '${formatterName}' not found`);
}
const formatter = new Formatter();
const output = formatter.format(this.failures, this.fixes);
const errorCount = this.failures.filter((failure) => failure.getRuleSeverity() === "error").length;
return {
errorCount,
failures: this.failures,
fixes: this.fixes,
format: formatterName,
output,
warningCount: this.failures.length - errorCount,
};
}
private getAllFailures(sourceFile: ts.SourceFile, enabledRules: IRule[]): RuleFailure[] {
const failures = flatMap(enabledRules, (rule) => this.applyRule(rule, sourceFile));
return removeDisabledFailures(sourceFile, failures);
}
private applyAllFixes(
enabledRules: IRule[], fileFailures: RuleFailure[], sourceFile: ts.SourceFile, sourceFileName: string): RuleFailure[] {
// When fixing, we need to be careful as a fix in one rule may affect other rules.
// So fix each rule separately.
let source: string = sourceFile.text;
for (const rule of enabledRules) {
const hasFixes = fileFailures.some((f) => f.hasFix() && f.getRuleName() === rule.getOptions().ruleName);
if (hasFixes) {
// Get new failures in case the file changed.
const updatedFailures = removeDisabledFailures(sourceFile, this.applyRule(rule, sourceFile));
const fixableFailures = updatedFailures.filter((f) => f.hasFix());
this.fixes = this.fixes.concat(fixableFailures);
source = this.applyFixes(sourceFileName, source, fixableFailures);
sourceFile = this.getSourceFile(sourceFileName, source);
}
}
// If there were fixes, get the *new* list of failures.
return this.getAllFailures(sourceFile, enabledRules);
}
// Only "protected" because a test directly accesses it.
// tslint:disable-next-line member-ordering
protected applyFixes(sourceFilePath: string, source: string, fixableFailures: RuleFailure[]): string {
const fixesByFile = createMultiMap(fixableFailures, (f) => [f.getFileName(), f.getFix()!]);
fixesByFile.forEach((fileFixes, filePath) => {
let fileNewSource: string;
if (path.resolve(filePath) === path.resolve(sourceFilePath)) {
source = Replacement.applyFixes(source, fileFixes);
fileNewSource = source;
} else {
const oldSource = fs.readFileSync(filePath, "utf-8");
fileNewSource = Replacement.applyFixes(oldSource, fileFixes);
}
fs.writeFileSync(filePath, fileNewSource);
this.updateProgram(filePath);
});
return source;
}
private updateProgram(sourceFilePath: string) {
if (this.program !== undefined && this.program.getSourceFile(sourceFilePath) !== undefined) {
const options = this.program.getCompilerOptions();
this.program = ts.createProgram(this.program.getRootFileNames(), options, ts.createCompilerHost(options, true), this.program);
}
}
private applyRule(rule: IRule, sourceFile: ts.SourceFile): RuleFailure[] {
try {
if (this.program !== undefined && isTypedRule(rule)) {
return rule.applyWithProgram(sourceFile, this.program);
} else {
return rule.apply(sourceFile);
}
} catch (error) {
if (isError(error) && error.stack !== undefined) {
showWarningOnce(error.stack);
} else {
showWarningOnce(String(error));
}
return [];
}
}
private getEnabledRules(configuration: IConfigurationFile = DEFAULT_CONFIG, isJs: boolean): IRule[] {
const ruleOptionsList = convertRuleOptions(isJs ? configuration.jsRules : configuration.rules);
const rulesDirectories = arrayify(this.options.rulesDirectory)
.concat(arrayify(configuration.rulesDirectory));
return loadRules(ruleOptionsList, rulesDirectories, isJs);
}
private getSourceFile(fileName: string, source: string) {
if (this.program !== undefined) {
const sourceFile = this.program.getSourceFile(fileName);
if (sourceFile === undefined) {
const INVALID_SOURCE_ERROR = dedent`
Invalid source file: ${fileName}. Ensure that the files supplied to lint have a .ts, .tsx, .d.ts, .js or .jsx extension.
`;
throw new FatalError(INVALID_SOURCE_ERROR);
}
return sourceFile;
} else {
return utils.getSourceFile(fileName, source);
}
}
}
function createMultiMap<T, K, V>(inputs: T[], getPair: (input: T) => [K, V] | undefined): Map<K, V[]> {
const map = new Map<K, V[]>();
for (const input of inputs) {
const pair = getPair(input);
if (pair !== undefined) {
const [k, v] = pair;
const vs = map.get(k);
if (vs !== undefined) {
vs.push(v);
} else {
map.set(k, [v]);
}
}
}
return map;
}