-
-
Notifications
You must be signed in to change notification settings - Fork 237
/
ApiIncrementalChecker.ts
93 lines (77 loc) · 2.58 KB
/
ApiIncrementalChecker.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
import {
IncrementalCheckerInterface,
IncrementalCheckerParams
} from './IncrementalCheckerInterface';
import { CancellationToken } from './CancellationToken';
import { CompilerHost } from './CompilerHost';
import { createEslinter } from './createEslinter';
import {
createIssuesFromTsDiagnostics,
createIssuesFromEsLintReports
} from './issue';
import { LintReport } from './types/eslint';
export class ApiIncrementalChecker implements IncrementalCheckerInterface {
protected readonly tsIncrementalCompiler: CompilerHost;
private currentEsLintErrors = new Map<string, LintReport>();
private lastUpdatedFiles: string[] = [];
private lastRemovedFiles: string[] = [];
private readonly eslinter: ReturnType<typeof createEslinter> | undefined;
constructor({
typescript,
programConfigFile,
compilerOptions,
eslinter,
vue,
checkSyntacticErrors = false,
resolveModuleName,
resolveTypeReferenceDirective
}: IncrementalCheckerParams) {
this.eslinter = eslinter;
this.tsIncrementalCompiler = new CompilerHost(
typescript,
vue,
programConfigFile,
compilerOptions,
checkSyntacticErrors,
resolveModuleName,
resolveTypeReferenceDirective
);
}
public hasEsLinter(): boolean {
return this.eslinter !== undefined;
}
public isFileExcluded(filePath: string): boolean {
return filePath.endsWith('.d.ts');
}
public nextIteration() {
// do nothing
}
public async getTypeScriptIssues() {
const tsDiagnostics = await this.tsIncrementalCompiler.processChanges();
this.lastUpdatedFiles = tsDiagnostics.updatedFiles;
this.lastRemovedFiles = tsDiagnostics.removedFiles;
return createIssuesFromTsDiagnostics(tsDiagnostics.results);
}
public async getEsLintIssues(cancellationToken: CancellationToken) {
if (!this.eslinter) {
throw new Error('EsLint is not enabled in the plugin.');
}
for (const removedFile of this.lastRemovedFiles) {
this.currentEsLintErrors.delete(removedFile);
}
for (const updatedFile of this.lastUpdatedFiles) {
cancellationToken.throwIfCancellationRequested();
if (this.isFileExcluded(updatedFile)) {
continue;
}
const report = this.eslinter.getReport(updatedFile);
if (report !== undefined) {
this.currentEsLintErrors.set(updatedFile, report);
} else if (this.currentEsLintErrors.has(updatedFile)) {
this.currentEsLintErrors.delete(updatedFile);
}
}
const reports = Array.from(this.currentEsLintErrors.values());
return createIssuesFromEsLintReports(reports);
}
}