UNPKG

5.68 kBJavaScriptView Raw
1"use strict";
2var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3 return new (P || (P = Promise))(function (resolve, reject) {
4 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6 function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
7 step((generator = generator.apply(thisArg, _arguments || [])).next());
8 });
9};
10Object.defineProperty(exports, "__esModule", { value: true });
11const minimatch = require("minimatch");
12const path = require("path");
13const linterConfigHelpers_1 = require("./linterConfigHelpers");
14const NormalizedMessage_1 = require("./NormalizedMessage");
15const CompilerHost_1 = require("./CompilerHost");
16const FsHelper_1 = require("./FsHelper");
17class ApiIncrementalChecker {
18 constructor(typescript, createNormalizedMessageFromDiagnostic, createNormalizedMessageFromRuleFailure, programConfigFile, compilerOptions, context, linterConfigFile, linterAutoFix, checkSyntacticErrors, resolveModuleName, resolveTypeReferenceDirective) {
19 this.createNormalizedMessageFromDiagnostic = createNormalizedMessageFromDiagnostic;
20 this.createNormalizedMessageFromRuleFailure = createNormalizedMessageFromRuleFailure;
21 this.context = context;
22 this.linterConfigFile = linterConfigFile;
23 this.linterAutoFix = linterAutoFix;
24 this.linterConfigs = {};
25 this.linterExclusions = [];
26 this.currentLintErrors = new Map();
27 this.lastUpdatedFiles = [];
28 this.lastRemovedFiles = [];
29 this.getLinterConfig = linterConfigHelpers_1.makeGetLinterConfig(this.linterConfigs, this.linterExclusions, this.context);
30 this.hasFixedConfig = typeof this.linterConfigFile === 'string';
31 this.initLinterConfig();
32 this.tsIncrementalCompiler = new CompilerHost_1.CompilerHost(typescript, programConfigFile, compilerOptions, checkSyntacticErrors, resolveModuleName, resolveTypeReferenceDirective);
33 }
34 initLinterConfig() {
35 if (!this.linterConfig && this.hasFixedConfig) {
36 this.linterConfig = linterConfigHelpers_1.loadLinterConfig(this.linterConfigFile);
37 if (this.linterConfig.linterOptions &&
38 this.linterConfig.linterOptions.exclude) {
39 // Pre-build minimatch patterns to avoid additional overhead later on.
40 // Note: Resolving the path is required to properly match against the full file paths,
41 // and also deals with potential cross-platform problems regarding path separators.
42 this.linterExclusions = this.linterConfig.linterOptions.exclude.map(pattern => new minimatch.Minimatch(path.resolve(pattern)));
43 }
44 }
45 }
46 createLinter(program) {
47 // tslint:disable-next-line:no-implicit-dependencies
48 const tslint = require('tslint');
49 return new tslint.Linter({ fix: this.linterAutoFix }, program);
50 }
51 hasLinter() {
52 return !!this.linterConfigFile;
53 }
54 isFileExcluded(filePath) {
55 return (filePath.endsWith('.d.ts') ||
56 this.linterExclusions.some(matcher => matcher.match(filePath)));
57 }
58 nextIteration() {
59 // do nothing
60 }
61 getDiagnostics(_cancellationToken) {
62 return __awaiter(this, void 0, void 0, function* () {
63 const diagnostics = yield this.tsIncrementalCompiler.processChanges();
64 this.lastUpdatedFiles = diagnostics.updatedFiles;
65 this.lastRemovedFiles = diagnostics.removedFiles;
66 return NormalizedMessage_1.NormalizedMessage.deduplicate(diagnostics.results.map(this.createNormalizedMessageFromDiagnostic));
67 });
68 }
69 getLints(_cancellationToken) {
70 for (const updatedFile of this.lastUpdatedFiles) {
71 if (this.isFileExcluded(updatedFile)) {
72 continue;
73 }
74 try {
75 const linter = this.createLinter(this.tsIncrementalCompiler.getProgram());
76 const config = this.hasFixedConfig
77 ? this.linterConfig
78 : this.getLinterConfig(updatedFile);
79 if (!config) {
80 continue;
81 }
82 // const source = fs.readFileSync(updatedFile, 'utf-8');
83 linter.lint(updatedFile, undefined, config);
84 const lints = linter.getResult();
85 this.currentLintErrors.set(updatedFile, lints);
86 }
87 catch (e) {
88 if (FsHelper_1.FsHelper.existsSync(updatedFile) &&
89 // check the error type due to file system lag
90 !(e instanceof Error) &&
91 !(e.constructor.name === 'FatalError') &&
92 !(e.message && e.message.trim().startsWith('Invalid source file'))) {
93 // it's not because file doesn't exist - throw error
94 throw e;
95 }
96 }
97 for (const removedFile of this.lastRemovedFiles) {
98 this.currentLintErrors.delete(removedFile);
99 }
100 }
101 const allLints = [];
102 for (const [, value] of this.currentLintErrors) {
103 allLints.push(...value.failures);
104 }
105 return NormalizedMessage_1.NormalizedMessage.deduplicate(allLints.map(this.createNormalizedMessageFromRuleFailure));
106 }
107}
108exports.ApiIncrementalChecker = ApiIncrementalChecker;
109//# sourceMappingURL=ApiIncrementalChecker.js.map
\No newline at end of file