From 5e0a086462a588bf332526058fe23355794a02ae Mon Sep 17 00:00:00 2001 From: wuhailong Date: Fri, 29 Dec 2023 08:23:15 +0000 Subject: [PATCH] Adapt coverage plug-in code coverage instrumentation time-consuming optimization Signed-off-by: wuhailong Change-Id: Ibb8219dc513b26825a011e2d43b316b1786a2afb --- .../ark_compiler/module/module_source_file.ts | 38 ++++++++++++++----- .../ark_compiler/rollup-plugin-gen-abc.ts | 4 ++ 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/compiler/src/fast_build/ark_compiler/module/module_source_file.ts b/compiler/src/fast_build/ark_compiler/module/module_source_file.ts index 321193c81..97a2c37e8 100644 --- a/compiler/src/fast_build/ark_compiler/module/module_source_file.ts +++ b/compiler/src/fast_build/ark_compiler/module/module_source_file.ts @@ -306,26 +306,46 @@ export class ModuleSourceFile { const moduleInfo: any = rollupObject.getModuleInfo(this.moduleId); const importMap: any = moduleInfo.importedIdMaps; const code: MagicString = new MagicString(this.source); + // The data collected by moduleNodeMap represents the node dataset of related types. + // The data is processed based on the AST collected during the transform stage. const moduleNodeMap: Map = moduleInfo.getNodeByType(ROLLUP_IMPORT_NODE, ROLLUP_EXPORTNAME_NODE, ROLLUP_EXPORTALL_NODE, ROLLUP_DYNAMICIMPORT_NODE); let hasDynamicImport: boolean = false; - for (let nodeSet of moduleNodeMap.values()) { - nodeSet.forEach(node => { + if (rollupObject.share.projectConfig.needCoverageInsert && moduleInfo.ast.program) { + // In coverage instrumentation scenario, + // ast from rollup because the data of ast and moduleNodeMap are inconsistent. + moduleInfo.ast.program.body.forEach((node) => { if (!hasDynamicImport && node.type === ROLLUP_DYNAMICIMPORT_NODE) { hasDynamicImport = true; } - if (node.source) { - if (node.source.type === ROLLUP_LITERAL_NODE) { - const ohmUrl: string | undefined = - this.getOhmUrl(rollupObject, node.source.value, importMap[node.source.value]); - if (ohmUrl !== undefined) { - code.update(node.source.start, node.source.end, `'${ohmUrl}'`); - } + if ((node.type === ROLLUP_IMPORT_NODE || node.type === ROLLUP_EXPORTNAME_NODE || + node.type === ROLLUP_EXPORTALL_NODE && node.source)) { + const ohmUrl: string | undefined = + this.getOhmUrl(rollupObject, node.source.value, importMap[node.source.value]); + if (ohmUrl !== undefined) { + code.update(node.source.start, node.source.end, `'${ohmUrl}'`); } } }); + } else { + for (let nodeSet of moduleNodeMap.values()) { + nodeSet.forEach(node => { + if (!hasDynamicImport && node.type === ROLLUP_DYNAMICIMPORT_NODE) { + hasDynamicImport = true; + } + if (node.source) { + if (node.source.type === ROLLUP_LITERAL_NODE) { + const ohmUrl: string | undefined = + this.getOhmUrl(rollupObject, node.source.value, importMap[node.source.value]); + if (ohmUrl !== undefined) { + code.update(node.source.start, node.source.end, `'${ohmUrl}'`); + } + } + } + }); + } } if (hasDynamicImport) { diff --git a/compiler/src/fast_build/ark_compiler/rollup-plugin-gen-abc.ts b/compiler/src/fast_build/ark_compiler/rollup-plugin-gen-abc.ts index 6667863a6..a998aea8a 100644 --- a/compiler/src/fast_build/ark_compiler/rollup-plugin-gen-abc.ts +++ b/compiler/src/fast_build/ark_compiler/rollup-plugin-gen-abc.ts @@ -20,6 +20,7 @@ import { transformForModule } from './transform'; import { checkArkCompilerCacheInfo, shouldInvalidCache } from './cache'; import { checkIfJsImportingArkts } from './check_import_module'; import { compilerOptions } from '../../ets_checker'; +import { ModuleSourceFile } from './module/module_source_file'; export function genAbc() { return { @@ -37,6 +38,9 @@ export function genAbc() { if (compilerOptions.needDoArkTsLinter) { checkIfJsImportingArkts(this); } + if (this.share.projectConfig.needCoverageInsert) { + this.share.ModuleSourceFile = ModuleSourceFile.getSourceFiles(); + } } }, buildEnd: generateModuleAbc, -- Gitee