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 321193c816f9c0be8ad1be8e72b8a279efe56b28..97a2c37e88e2f0ebbcd8bb4d25e2b74477525fdb 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 6667863a6c55b74e1c383fe5e49dfe30e2c1d448..a998aea8aeb3ad0e4986721dab8ea306d5bc04eb 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,