UNPKG

11.8 kBJavaScriptView Raw
1"use strict";
2var __assign = (this && this.__assign) || function () {
3 __assign = Object.assign || function(t) {
4 for (var s, i = 1, n = arguments.length; i < n; i++) {
5 s = arguments[i];
6 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7 t[p] = s[p];
8 }
9 return t;
10 };
11 return __assign.apply(this, arguments);
12};
13var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14 return new (P || (P = Promise))(function (resolve, reject) {
15 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
16 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
17 function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
18 step((generator = generator.apply(thisArg, _arguments || [])).next());
19 });
20};
21var __generator = (this && this.__generator) || function (thisArg, body) {
22 var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
23 return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
24 function verb(n) { return function (v) { return step([n, v]); }; }
25 function step(op) {
26 if (f) throw new TypeError("Generator is already executing.");
27 while (_) try {
28 if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
29 if (y = 0, t) op = [op[0] & 2, t.value];
30 switch (op[0]) {
31 case 0: case 1: t = op; break;
32 case 4: _.label++; return { value: op[1], done: false };
33 case 5: _.label++; y = op[1]; op = [0]; continue;
34 case 7: op = _.ops.pop(); _.trys.pop(); continue;
35 default:
36 if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
37 if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
38 if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
39 if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
40 if (t[2]) _.ops.pop();
41 _.trys.pop(); continue;
42 }
43 op = body.call(thisArg, _);
44 } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
45 if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
46 }
47};
48var __importStar = (this && this.__importStar) || function (mod) {
49 if (mod && mod.__esModule) return mod;
50 var result = {};
51 if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
52 result["default"] = mod;
53 return result;
54};
55Object.defineProperty(exports, "__esModule", { value: true });
56var fs = __importStar(require("fs"));
57var path = __importStar(require("path"));
58var FilesRegister_1 = require("./FilesRegister");
59var resolution_1 = require("./resolution");
60var VueProgram_1 = require("./VueProgram");
61var issue_1 = require("./issue");
62var IncrementalChecker = /** @class */ (function () {
63 function IncrementalChecker(_a) {
64 var typescript = _a.typescript, programConfigFile = _a.programConfigFile, compilerOptions = _a.compilerOptions, eslinter = _a.eslinter, vue = _a.vue, _b = _a.checkSyntacticErrors, checkSyntacticErrors = _b === void 0 ? false : _b, resolveModuleName = _a.resolveModuleName, resolveTypeReferenceDirective = _a.resolveTypeReferenceDirective;
65 this.files = new FilesRegister_1.FilesRegister(function () { return ({
66 // data shape
67 source: undefined,
68 linted: false,
69 eslints: []
70 }); });
71 this.typescript = typescript;
72 this.programConfigFile = programConfigFile;
73 this.compilerOptions = compilerOptions;
74 this.eslinter = eslinter;
75 this.vue = vue;
76 this.checkSyntacticErrors = checkSyntacticErrors;
77 this.resolveModuleName = resolveModuleName;
78 this.resolveTypeReferenceDirective = resolveTypeReferenceDirective;
79 }
80 IncrementalChecker.loadProgramConfig = function (typescript, configFile, compilerOptions) {
81 var tsconfig = typescript.readConfigFile(configFile, typescript.sys.readFile).config;
82 tsconfig.compilerOptions = tsconfig.compilerOptions || {};
83 tsconfig.compilerOptions = __assign({}, tsconfig.compilerOptions, compilerOptions);
84 var parsed = typescript.parseJsonConfigFileContent(tsconfig, typescript.sys, path.dirname(configFile));
85 return parsed;
86 };
87 IncrementalChecker.createProgram = function (typescript, programConfig, files, oldProgram, userResolveModuleName, userResolveTypeReferenceDirective) {
88 var host = typescript.createCompilerHost(programConfig.options);
89 var realGetSourceFile = host.getSourceFile;
90 var _a = resolution_1.makeResolutionFunctions(userResolveModuleName, userResolveTypeReferenceDirective), resolveModuleName = _a.resolveModuleName, resolveTypeReferenceDirective = _a.resolveTypeReferenceDirective;
91 host.resolveModuleNames = function (moduleNames, containingFile) {
92 return moduleNames.map(function (moduleName) {
93 return resolveModuleName(typescript, moduleName, containingFile, programConfig.options, host).resolvedModule;
94 });
95 };
96 host.resolveTypeReferenceDirectives = function (typeDirectiveNames, containingFile) {
97 return typeDirectiveNames.map(function (typeDirectiveName) {
98 return resolveTypeReferenceDirective(typescript, typeDirectiveName, containingFile, programConfig.options, host).resolvedTypeReferenceDirective;
99 });
100 };
101 host.getSourceFile = function (filePath, languageVersion, onError) {
102 try {
103 var stats = fs.statSync(filePath);
104 files.setMtime(filePath, stats.mtime.valueOf());
105 }
106 catch (e) {
107 // probably file does not exists
108 files.remove(filePath);
109 }
110 // get source file only if there is no source in files register
111 if (!files.has(filePath) || !files.getData(filePath).source) {
112 files.mutateData(filePath, function (data) {
113 data.source = realGetSourceFile(filePath, languageVersion, onError);
114 });
115 }
116 return files.getData(filePath).source;
117 };
118 return typescript.createProgram(programConfig.fileNames, programConfig.options, host, oldProgram // re-use old program
119 );
120 };
121 IncrementalChecker.prototype.hasEsLinter = function () {
122 return this.eslinter !== undefined;
123 };
124 IncrementalChecker.isFileExcluded = function (filePath) {
125 return filePath.endsWith('.d.ts');
126 };
127 IncrementalChecker.prototype.nextIteration = function () {
128 this.program = this.vue.enabled
129 ? this.loadVueProgram(this.vue)
130 : this.loadDefaultProgram();
131 };
132 IncrementalChecker.prototype.loadVueProgram = function (vueOptions) {
133 this.programConfig =
134 this.programConfig ||
135 VueProgram_1.VueProgram.loadProgramConfig(this.typescript, this.programConfigFile, this.compilerOptions);
136 return VueProgram_1.VueProgram.createProgram(this.typescript, this.programConfig, path.dirname(this.programConfigFile), this.files, this.program, this.resolveModuleName, this.resolveTypeReferenceDirective, vueOptions);
137 };
138 IncrementalChecker.prototype.loadDefaultProgram = function () {
139 this.programConfig =
140 this.programConfig ||
141 IncrementalChecker.loadProgramConfig(this.typescript, this.programConfigFile, this.compilerOptions);
142 return IncrementalChecker.createProgram(this.typescript, this.programConfig, this.files, this.program, this.resolveModuleName, this.resolveTypeReferenceDirective);
143 };
144 IncrementalChecker.prototype.getTypeScriptIssues = function (cancellationToken) {
145 return __awaiter(this, void 0, void 0, function () {
146 var program, tsDiagnostics, filesToCheck;
147 var _this = this;
148 return __generator(this, function (_a) {
149 program = this.program;
150 if (!program) {
151 throw new Error('Invoked called before program initialized');
152 }
153 tsDiagnostics = [];
154 filesToCheck = program.getSourceFiles();
155 filesToCheck.forEach(function (sourceFile) {
156 if (cancellationToken) {
157 cancellationToken.throwIfCancellationRequested();
158 }
159 var tsDiagnosticsToRegister = _this
160 .checkSyntacticErrors
161 ? program
162 .getSemanticDiagnostics(sourceFile, cancellationToken)
163 .concat(program.getSyntacticDiagnostics(sourceFile, cancellationToken))
164 : program.getSemanticDiagnostics(sourceFile, cancellationToken);
165 tsDiagnostics.push.apply(tsDiagnostics, tsDiagnosticsToRegister);
166 });
167 return [2 /*return*/, issue_1.createIssuesFromTsDiagnostics(tsDiagnostics, this.typescript)];
168 });
169 });
170 };
171 IncrementalChecker.prototype.getEsLintIssues = function (cancellationToken) {
172 return __awaiter(this, void 0, void 0, function () {
173 var filesToLint, currentEsLintErrors, reports;
174 var _this = this;
175 return __generator(this, function (_a) {
176 filesToLint = this.files
177 .keys()
178 .filter(function (filePath) {
179 return !_this.files.getData(filePath).linted &&
180 !IncrementalChecker.isFileExcluded(filePath);
181 });
182 currentEsLintErrors = new Map();
183 filesToLint.forEach(function (fileName) {
184 cancellationToken.throwIfCancellationRequested();
185 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
186 var report = _this.eslinter.getReport(fileName);
187 if (report !== undefined) {
188 currentEsLintErrors.set(fileName, report);
189 }
190 });
191 // set lints in files register
192 currentEsLintErrors.forEach(function (lint, filePath) {
193 _this.files.mutateData(filePath, function (data) {
194 data.linted = true;
195 data.eslints.push(lint);
196 });
197 });
198 // set all files as linted
199 this.files.keys().forEach(function (filePath) {
200 _this.files.mutateData(filePath, function (data) {
201 data.linted = true;
202 });
203 });
204 reports = this.files
205 .keys()
206 .reduce(function (innerLints, filePath) {
207 return innerLints.concat(_this.files.getData(filePath).eslints);
208 }, []);
209 return [2 /*return*/, issue_1.createIssuesFromEsLintReports(reports)];
210 });
211 });
212 };
213 return IncrementalChecker;
214}());
215exports.IncrementalChecker = IncrementalChecker;
216//# sourceMappingURL=IncrementalChecker.js.map
\No newline at end of file