!937 语法检查优化

Merge pull request !937 from lihong/master
This commit is contained in:
openharmony_ci 2022-08-16 08:25:50 +00:00 committed by Gitee
commit ad7428abf9
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
4 changed files with 99 additions and 10 deletions

View File

@ -58,13 +58,14 @@ function initProjectConfig(projectConfig) {
projectConfig.aceProfilePath = projectConfig.aceProfilePath || process.env.aceProfilePath;
projectConfig.aceModuleJsonPath = projectConfig.aceModuleJsonPath || process.env.aceModuleJsonPath;
projectConfig.aceSuperVisualPath = projectConfig.aceSuperVisualPath ||
process.env.aceSuperVisualPath
process.env.aceSuperVisualPath;
projectConfig.hashProjectPath = projectConfig.hashProjectPath ||
hashProjectPath(projectConfig.projectPath)
hashProjectPath(projectConfig.projectPath);
projectConfig.aceBuildJson = projectConfig.aceBuildJson || process.env.aceBuildJson;
projectConfig.cachePath = projectConfig.cachePath || process.env.cachePath ||
path.resolve(__dirname, 'node_modules/.cache');
projectConfig.aceSoPath = projectConfig.aceSoPath || process.env.aceSoPath;
projectConfig.xtsMode = /ets_loader_ark$/.test(__dirname);
}
function loadEntryObj(projectConfig) {

View File

@ -32,23 +32,23 @@ import {
useOSFiles,
sourcemapNamesCollection
} from './validate_ui_syntax';
import { projectConfig } from '../main';
import {
circularFile,
mkDir,
writeFileSync
} from './utils';
import {
MODULE_ETS_PATH,
MODULE_SHARE_PATH,
BUILD_SHARE_PATH,
ARK
} from './pre_define';
import {
createLanguageService,
createWatchCompilerHost
} from './ets_checker';
import { globalProgram } from '../main';
import {
globalProgram,
projectConfig
} from '../main';
import cluster from 'cluster';
configure({
@ -68,6 +68,19 @@ interface Info {
};
}
export interface CacheFileName {
mtimeMs: number,
children: string[],
error: boolean
}
interface NeedUpdateFlag {
flag: boolean;
}
export let cache: Cache;
type Cache = Record<string, CacheFileName>;
export class ResultStates {
private mStats: Stats;
private mErrorCount: number = 0;
@ -186,7 +199,16 @@ export class ResultStates {
createWatchCompilerHost(rootFileNames, this.printDiagnostic.bind(this),
this.delayPrintLogCount.bind(this)));
} else {
const languageService: ts.LanguageService = createLanguageService(rootFileNames);
let languageService: ts.LanguageService = null;
let cacheFile: string = null;
if (projectConfig.xtsMode) {
languageService = createLanguageService(rootFileNames);
} else {
cacheFile = path.resolve(projectConfig.cachePath, '../.ts_checker_cache');
cache = fs.existsSync(cacheFile) ? JSON.parse(fs.readFileSync(cacheFile).toString()) : {};
const filterFiles: string[] = filterInput(rootFileNames);
languageService = createLanguageService(filterFiles);
}
globalProgram.program = languageService.getProgram();
const allDiagnostics: ts.Diagnostic[] = globalProgram.program
.getSyntacticDiagnostics()
@ -195,6 +217,9 @@ export class ResultStates {
allDiagnostics.forEach((diagnostic: ts.Diagnostic) => {
this.printDiagnostic(diagnostic);
});
if (process.env.watchMode !== 'true' && !projectConfig.xtsMode) {
fs.writeFileSync(cacheFile, JSON.stringify(cache, null, 2));
}
}
});
@ -219,6 +244,9 @@ export class ResultStates {
private printDiagnostic(diagnostic: ts.Diagnostic): void {
const message: string = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n');
if (this.validateError(message)) {
if (process.env.watchMode !== 'true' && !projectConfig.xtsMode) {
updateErrorFileCache(diagnostic);
}
this.mErrorCount += 1;
if (diagnostic.file) {
const { line, character }: ts.LineAndCharacter =
@ -411,3 +439,38 @@ export class ResultStates {
return message;
}
}
function updateErrorFileCache(diagnostic: ts.Diagnostic): void {
if (diagnostic.file && cache[path.resolve(diagnostic.file.fileName)]) {
cache[path.resolve(diagnostic.file.fileName)].error = true;
}
}
function filterInput(rootFileNames: string[]): string[] {
return rootFileNames.filter((file: string) => {
const needUpdate: NeedUpdateFlag = { flag: false };
checkNeedUpdateFiles(path.resolve(file), needUpdate);
return needUpdate.flag;
});
}
function checkNeedUpdateFiles(file: string, needUpdate: NeedUpdateFlag): void {
if (needUpdate.flag) {
return;
}
const value: CacheFileName = cache[file];
const mtimeMs: number = fs.statSync(file).mtimeMs;
if (value) {
if (value.error || value.mtimeMs !== mtimeMs) {
needUpdate.flag = true;
return;
}
for (let i = 0; i < value.children.length; ++i) {
checkNeedUpdateFiles(value.children[i], needUpdate);
}
} else {
cache[file] = { mtimeMs, children: [], error: false };
needUpdate.flag = true;
}
}

View File

@ -28,7 +28,6 @@ import {
} from './validate_ui_syntax';
import {
INNER_COMPONENT_MEMBER_DECORATORS,
COMPONENT_IF,
COMPONENT_DECORATORS_PARAMS,
COMPONENT_BUILD_FUNCTION,
STYLE_ADD_DOUBLE_DOLLAR,
@ -36,11 +35,14 @@ import {
PROPERTIES_ADD_DOUBLE_DOLLAR,
$$_BLOCK_INTERFACE
} from './pre_define';
import { JS_BIND_COMPONENTS } from './component_map';
import { getName } from './process_component_build';
import { INNER_COMPONENT_NAMES } from './component_map';
import { props } from './compile_info';
import { resolveSourceFile } from './resolve_ohm_url';
import {
CacheFileName,
cache
} from './compile_info';
function readDeaclareFiles(): string[] {
const declarationsFileNames: string[] = [];
@ -182,9 +184,32 @@ function resolveModuleNames(moduleNames: string[], containingFile: string): ts.R
}
}
}
if (process.env.watchMode !== 'true' && !projectConfig.xtsMode) {
createOrUpdateCache(resolvedModules, containingFile);
}
return resolvedModules;
}
function createOrUpdateCache(resolvedModules: ts.ResolvedModuleFull[], containingFile: string): void {
const children: string[] = [];
const error: boolean = false;
resolvedModules.forEach(moduleObj => {
if (moduleObj && moduleObj.resolvedFileName && /(?<!\.d)\.(ets|ts)$/.test(moduleObj.resolvedFileName)) {
const file: string = path.resolve(moduleObj.resolvedFileName);
const mtimeMs: number = fs.statSync(file).mtimeMs;
children.push(file);
const value: CacheFileName = cache[file];
if (value) {
value.mtimeMs = mtimeMs;
value.error = error;
} else {
cache[file] = { mtimeMs, children: [], error };
}
}
});
cache[path.resolve(containingFile)] = { mtimeMs: fs.statSync(containingFile).mtimeMs, children, error };
}
export function createWatchCompilerHost(rootFileNames: string[],
reportDiagnostic: ts.DiagnosticReporter, delayPrintLogCount: Function, isPipe: boolean = false
): ts.WatchCompilerHostOfFilesAndCompilerOptions<ts.BuilderProgram> {

View File

@ -137,7 +137,7 @@ function initConfig(config) {
new ResultStates()
]
});
if (!/ets_loader_ark$/.test(__dirname)) {
if (!projectConfig.xtsMode) {
config.cache = {
type: "filesystem",
cacheDirectory: path.resolve(projectConfig.cachePath, '.ets_cache')