ts-oak-plugin/lib/core/checker.js

105 lines
3.8 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.OakCustomChecker = void 0;
const asyncContextChecker_1 = require("./asyncContextChecker");
const i18nChecker_1 = require("./i18nChecker");
const jsxLiteralChecker_1 = require("./jsxLiteralChecker");
/**
* 核心检查器类 - 支持增量检查
*/
class OakCustomChecker {
constructor(pwd, config, program, typeChecker, tsLib) {
this.pwd = pwd;
this.config = config;
this.program = program;
this.typeChecker = typeChecker;
this.tsLib = tsLib;
// 缓存:文件名 -> 诊断结果
this.diagnosticsCache = new Map();
// 缓存:文件名 -> 文件版本
this.fileVersionCache = new Map();
this.initializeCheckers();
}
initializeCheckers() {
var _a, _b, _c;
if (((_a = this.config.context) === null || _a === void 0 ? void 0 : _a.checkAsyncContext) !== false) {
this.asyncContextChecker = new asyncContextChecker_1.AsyncContextChecker(this.pwd, this.config, this.program, this.typeChecker, this.tsLib);
}
if (((_b = this.config.locale) === null || _b === void 0 ? void 0 : _b.checkI18nKeys) !== false) {
this.i18nChecker = new i18nChecker_1.I18nChecker(this.pwd, this.config, this.program, this.typeChecker, this.tsLib);
}
if (((_c = this.config.locale) === null || _c === void 0 ? void 0 : _c.checkJsxLiterals) === true) {
this.jsxLiteralChecker = new jsxLiteralChecker_1.JsxLiteralChecker(this.pwd, this.config, this.program, this.typeChecker, this.tsLib);
}
}
/**
* 检查单个文件(用于 TSServer 插件)
*/
checkFile(fileName, fileVersion) {
// 检查缓存
if (fileVersion && this.fileVersionCache.get(fileName) === fileVersion) {
const cached = this.diagnosticsCache.get(fileName);
if (cached) {
return cached;
}
}
const sourceFile = this.program.getSourceFile(fileName);
if (!sourceFile) {
return [];
}
const diagnostics = [];
// 执行各项检查
if (this.asyncContextChecker) {
diagnostics.push(...this.asyncContextChecker.checkFile(sourceFile));
}
if (this.i18nChecker) {
diagnostics.push(...this.i18nChecker.checkFile(sourceFile));
}
if (this.jsxLiteralChecker) {
diagnostics.push(...this.jsxLiteralChecker.checkFile(sourceFile));
}
// 更新缓存
if (fileVersion) {
this.fileVersionCache.set(fileName, fileVersion);
this.diagnosticsCache.set(fileName, diagnostics);
}
return diagnostics;
}
/**
* 检查所有文件(用于 CLI 编译)
*/
checkAllFiles() {
const allDiagnostics = [];
for (const sourceFile of this.program.getSourceFiles()) {
if (sourceFile.isDeclarationFile)
continue;
const diagnostics = this.checkFile(sourceFile.fileName);
allDiagnostics.push(...diagnostics);
}
return allDiagnostics;
}
/**
* 清除缓存
*/
clearCache(fileName) {
if (fileName) {
this.diagnosticsCache.delete(fileName);
this.fileVersionCache.delete(fileName);
}
else {
this.diagnosticsCache.clear();
this.fileVersionCache.clear();
}
}
/**
* 更新 Program当项目重新编译时
*/
updateProgram(program) {
this.program = program;
this.typeChecker = program.getTypeChecker();
this.clearCache(); // 清除所有缓存
this.initializeCheckers(); // 重新初始化检查器
}
}
exports.OakCustomChecker = OakCustomChecker;
//# sourceMappingURL=checker.js.map