-
Notifications
You must be signed in to change notification settings - Fork 23
/
runner.ts
450 lines (425 loc) · 19 KB
/
runner.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
import { Linter, LinterOptions, ProgramFactory } from './linter';
import {
LintResult,
FileSummary,
Configuration,
AbstractProcessor,
DirectoryService,
ConfigurationError,
MessageHandler,
FileFilterFactory,
Severity,
} from '@fimbul/ymir';
import * as path from 'path';
import * as ts from 'typescript';
import * as glob from 'glob';
import { unixifyPath, hasSupportedExtension, addUnique, flatMap, hasParseErrors, invertChangeRange } from './utils';
import { Minimatch, IMinimatch } from 'minimatch';
import { ProcessorLoader } from './services/processor-loader';
import { injectable } from 'inversify';
import { CachedFileSystem, FileKind } from './services/cached-file-system';
import { ConfigurationManager } from './services/configuration-manager';
import { ProjectHost } from './project-host';
import debug = require('debug');
import { normalizeGlob } from 'normalize-glob';
const log = debug('wotan:runner');
export interface LintOptions {
config: string | undefined;
files: ReadonlyArray<string>;
exclude: ReadonlyArray<string>;
project: ReadonlyArray<string>;
references: boolean;
fix: boolean | number;
extensions: ReadonlyArray<string> | undefined;
reportUselessDirectives: Severity | boolean | undefined;
}
interface NormalizedOptions extends Pick<LintOptions, Exclude<keyof LintOptions, 'files'>> {
files: ReadonlyArray<NormalizedGlob>;
}
interface NormalizedGlob {
hasMagic: boolean;
normalized: string[];
}
@injectable()
export class Runner {
constructor(
private fs: CachedFileSystem,
private configManager: ConfigurationManager,
private linter: Linter,
private processorLoader: ProcessorLoader,
private directories: DirectoryService,
private logger: MessageHandler,
private filterFactory: FileFilterFactory,
) {}
public lintCollection(options: LintOptions): LintResult {
const config = options.config !== undefined ? this.configManager.loadLocalOrResolved(options.config) : undefined;
const cwd = this.directories.getCurrentDirectory();
const files = options.files.map(
(pattern) => ({hasMagic: glob.hasMagic(pattern), normalized: Array.from(normalizeGlob(pattern, cwd))}),
);
const exclude = flatMap(options.exclude, (pattern) => normalizeGlob(pattern, cwd));
const linterOptions: LinterOptions = {
reportUselessDirectives: options.reportUselessDirectives
? options.reportUselessDirectives === true
? 'error'
: options.reportUselessDirectives
: undefined,
};
if (options.project.length === 0 && options.files.length !== 0)
return this.lintFiles({...options, files, exclude}, config, linterOptions);
return this.lintProject({...options, files, exclude}, config, linterOptions);
}
private *lintProject(options: NormalizedOptions, config: Configuration | undefined, linterOptions: LinterOptions): LintResult {
const processorHost = new ProjectHost(
this.directories.getCurrentDirectory(),
config,
this.fs,
this.configManager,
this.processorLoader,
);
for (let {files, program} of
this.getFilesAndProgram(options.project, options.files, options.exclude, processorHost, options.references)
) {
let invalidatedProgram = false;
const factory: ProgramFactory = {
getCompilerOptions() {
return program.getCompilerOptions();
},
getProgram() {
if (invalidatedProgram) {
log('updating invalidated program');
program = processorHost.updateProgram(program);
invalidatedProgram = false;
}
return program;
},
};
for (const file of files) {
if (options.config === undefined)
config = this.configManager.find(file);
const mapped = processorHost.getProcessedFileInfo(file);
const originalName = mapped === undefined ? file : mapped.originalName;
const effectiveConfig = config && this.configManager.reduce(config, originalName);
if (effectiveConfig === undefined)
continue;
let sourceFile = program.getSourceFile(file)!;
const originalContent = mapped === undefined ? sourceFile.text : mapped.originalContent;
let summary: FileSummary;
const fix = shouldFix(sourceFile, options, originalName);
if (fix) {
summary = this.linter.lintAndFix(
sourceFile,
originalContent,
effectiveConfig,
(content, range) => {
invalidatedProgram = true;
const oldContent = sourceFile.text;
sourceFile = ts.updateSourceFile(sourceFile, content, range);
const hasErrors = hasParseErrors(sourceFile);
if (hasErrors) {
log("Autofixing caused syntax errors in '%s', rolling back", sourceFile.fileName);
sourceFile = ts.updateSourceFile(sourceFile, oldContent, invertChangeRange(range));
}
// either way we need to store the new SourceFile as the old one is now corrupted
processorHost.updateSourceFile(sourceFile);
return hasErrors ? undefined : sourceFile;
},
fix === true ? undefined : fix,
factory,
mapped === undefined ? undefined : mapped.processor,
linterOptions,
);
} else {
summary = {
findings: this.linter.getFindings(
sourceFile,
effectiveConfig,
factory,
mapped === undefined ? undefined : mapped.processor,
linterOptions,
),
fixes: 0,
content: originalContent,
};
}
yield [originalName, summary];
}
}
}
private *lintFiles(options: NormalizedOptions, config: Configuration | undefined, linterOptions: LinterOptions): LintResult {
let processor: AbstractProcessor | undefined;
for (const file of getFiles(options.files, options.exclude, this.directories.getCurrentDirectory())) {
if (options.config === undefined)
config = this.configManager.find(file);
const effectiveConfig = config && this.configManager.reduce(config, file);
if (effectiveConfig === undefined)
continue;
let originalContent: string | undefined;
let name: string;
let content: string;
if (effectiveConfig.processor) {
const ctor = this.processorLoader.loadProcessor(effectiveConfig.processor);
if (hasSupportedExtension(file, options.extensions)) {
name = file;
} else {
name = file + ctor.getSuffixForFile({
fileName: file,
getSettings: () => effectiveConfig.settings,
readFile: () => originalContent = this.fs.readFile(file),
});
if (!hasSupportedExtension(name, options.extensions))
continue;
}
if (originalContent === undefined) // might be initialized by the processor requesting the file content
originalContent = this.fs.readFile(file);
processor = new ctor({
source: originalContent,
sourceFileName: file,
targetFileName: name,
settings: effectiveConfig.settings,
});
content = processor.preprocess();
} else if (hasSupportedExtension(file, options.extensions)) {
processor = undefined;
name = file;
content = originalContent = this.fs.readFile(file);
} else {
continue;
}
let sourceFile = ts.createSourceFile(name, content, ts.ScriptTarget.ESNext, true);
const fix = shouldFix(sourceFile, options, file);
let summary: FileSummary;
if (fix) {
summary = this.linter.lintAndFix(
sourceFile,
originalContent,
effectiveConfig,
(newContent, range) => {
sourceFile = ts.updateSourceFile(sourceFile, newContent, range);
if (hasParseErrors(sourceFile)) {
log("Autofixing caused syntax errors in '%s', rolling back", sourceFile.fileName);
// Note: 'sourceFile' shouldn't be used after this as it contains invalid code
return;
}
return sourceFile;
},
fix === true ? undefined : fix,
undefined,
processor,
linterOptions,
);
} else {
summary = {
findings: this.linter.getFindings(
sourceFile,
effectiveConfig,
undefined,
processor,
linterOptions,
),
fixes: 0,
content: originalContent,
};
}
yield [file, summary];
}
}
private* getFilesAndProgram(
projects: ReadonlyArray<string>,
patterns: ReadonlyArray<NormalizedGlob>,
exclude: ReadonlyArray<string>,
host: ProjectHost,
references: boolean,
): Iterable<{files: Iterable<string>, program: ts.Program}> {
const cwd = unixifyPath(this.directories.getCurrentDirectory());
if (projects.length !== 0) {
projects = projects.map((configFile) => this.checkConfigDirectory(unixifyPath(path.resolve(cwd, configFile))));
} else if (references) {
projects = [this.checkConfigDirectory(cwd)];
} else {
const project = ts.findConfigFile(cwd, (f) => this.fs.isFile(f));
if (project === undefined)
throw new ConfigurationError(`Cannot find tsconfig.json for directory '${cwd}'.`);
projects = [project];
}
const allMatchedFiles: string [] = [];
const include: IMinimatch[] = [];
const nonMagicGlobs: NonMagicGlob[] = [];
for (const pattern of patterns) {
if (!pattern.hasMagic) {
const mm = new Minimatch(pattern.normalized[0]);
nonMagicGlobs.push({raw: pattern.normalized[0], match: mm});
include.push(mm);
} else {
include.push(...pattern.normalized.map((p) => new Minimatch(p)));
}
}
const ex = exclude.map((p) => new Minimatch(p, {dot: true}));
const projectsSeen: string[] = [];
let filesOfPreviousProject: string[] | undefined;
for (const program of this.createPrograms(projects, host, projectsSeen, references, isFileIncluded)) {
const ownFiles = [];
const files: string[] = [];
const fileFilter = this.filterFactory.create({program, host});
for (const sourceFile of program.getSourceFiles()) {
if (!fileFilter.filter(sourceFile))
continue;
const {fileName} = sourceFile;
ownFiles.push(fileName);
const originalName = host.getFileSystemFile(fileName)!;
if (!isFileIncluded(originalName))
continue;
files.push(fileName);
allMatchedFiles.push(originalName);
}
// uncache all files of the previous project if they are no longer needed
if (filesOfPreviousProject !== undefined)
for (const oldFile of filesOfPreviousProject)
if (!ownFiles.includes(oldFile))
host.uncacheFile(oldFile);
filesOfPreviousProject = ownFiles;
if (files.length !== 0)
yield {files, program};
}
ensurePatternsMatch(nonMagicGlobs, ex, allMatchedFiles, projectsSeen);
function isFileIncluded(fileName: string) {
return (include.length === 0 || include.some((p) => p.match(fileName))) && !ex.some((p) => p.match(fileName));
}
}
private checkConfigDirectory(fileOrDirName: string): string {
switch (this.fs.getKind(fileOrDirName)) {
case FileKind.NonExistent:
throw new ConfigurationError(`The specified path does not exist: '${fileOrDirName}'`);
case FileKind.Directory: {
const file = unixifyPath(path.join(fileOrDirName, 'tsconfig.json'));
if (!this.fs.isFile(file))
throw new ConfigurationError(`Cannot find a tsconfig.json file at the specified directory: '${fileOrDirName}'`);
return file;
}
default:
return fileOrDirName;
}
}
private* createPrograms(
projects: ReadonlyArray<string> | ReadonlyArray<ts.ResolvedProjectReference | undefined>,
host: ProjectHost,
seen: string[],
references: boolean,
isFileIncluded: (fileName: string) => boolean,
): Iterable<ts.Program> {
for (const configFile of projects) {
if (configFile === undefined)
continue;
const configFilePath = typeof configFile === 'string' ? configFile : configFile.sourceFile.fileName;
if (!addUnique(seen, configFilePath))
continue;
let commandLine;
if (typeof configFile !== 'string') {
({commandLine} = configFile);
} else {
commandLine = host.getParsedCommandLine(configFile);
if (commandLine === undefined)
continue;
}
if (commandLine.errors.length !== 0)
this.logger.warn(ts.formatDiagnostics(commandLine.errors, host));
if (commandLine.fileNames.length !== 0) {
if (!commandLine.options.composite || commandLine.fileNames.some((file) => isFileIncluded(host.getFileSystemFile(file)!))) {
log("Using project '%s'", configFilePath);
let resolvedReferences: ts.ResolvedProjectReference['references'];
{
// this is in a nested block to allow garbage collection while recursing
const program =
host.createProgram(commandLine.fileNames, commandLine.options, undefined, commandLine.projectReferences);
yield program;
if (references)
resolvedReferences = program.getResolvedProjectReferences();
}
if (resolvedReferences !== undefined)
yield* this.createPrograms(resolvedReferences, host, seen, true, isFileIncluded);
continue;
}
log("Project '%s' contains no file to lint", configFilePath);
}
if (references) {
if (typeof configFile !== 'string') {
if (configFile.references !== undefined)
yield* this.createPrograms(configFile.references, host, seen, true, isFileIncluded);
} else if (commandLine.projectReferences !== undefined) {
yield* this.createPrograms(
commandLine.projectReferences.map((ref) => this.checkConfigDirectory(ref.path)),
host,
seen,
true,
isFileIncluded,
);
}
}
}
}
}
function getFiles(patterns: ReadonlyArray<NormalizedGlob>, exclude: ReadonlyArray<string>, cwd: string): Iterable<string> {
const result: string[] = [];
const globOptions: glob.IOptions = {
cwd,
nobrace: true, // braces are already expanded
cache: {},
ignore: exclude,
nodir: true,
realpathCache: {},
statCache: {},
symlinks: {},
};
for (const pattern of patterns) {
let matched = pattern.hasMagic;
for (const normalized of pattern.normalized) {
const match = glob.sync(normalized, globOptions);
if (match.length !== 0) {
matched = true;
result.push(...match);
}
}
if (!matched && !isExcluded(pattern.normalized[0], exclude.map((p) => new Minimatch(p, {dot: true}))))
throw new ConfigurationError(`'${pattern.normalized[0]}' does not exist.`);
}
return new Set(result.map(unixifyPath)); // deduplicate files
}
interface NonMagicGlob {
raw: string;
match: IMinimatch;
}
function ensurePatternsMatch(include: NonMagicGlob[], exclude: IMinimatch[], files: string[], projects: ReadonlyArray<string>) {
for (const pattern of include)
if (!isExcluded(pattern.raw, exclude) && !files.some((f) => pattern.match.match(f)))
throw new ConfigurationError(`'${pattern.raw}' is not included in any of the projects: '${projects.join("', '")}'.`);
}
function isExcluded(file: string, exclude: IMinimatch[]): boolean {
for (const e of exclude)
if (e.match(file))
return true;
return false;
}
function shouldFix(sourceFile: ts.SourceFile, options: Pick<LintOptions, 'fix'>, originalName: string) {
if (options.fix && hasParseErrors(sourceFile)) {
log("Not fixing '%s' because of parse errors.", originalName);
return false;
}
return options.fix;
}
declare module 'typescript' {
function matchFiles(
path: string,
extensions: ReadonlyArray<string>,
excludes: ReadonlyArray<string> | undefined,
includes: ReadonlyArray<string>,
useCaseSensitiveFileNames: boolean,
currentDirectory: string,
depth: number | undefined,
getFileSystemEntries: (path: string) => ts.FileSystemEntries,
realpath: (path: string) => string,
): string[];
interface FileSystemEntries {
readonly files: ReadonlyArray<string>;
readonly directories: ReadonlyArray<string>;
}
}