diff --git a/plugins/babel-plugin-canyon/src/index.js b/plugins/babel-plugin-canyon/src/index.js index a57cf395..d3d96b52 100644 --- a/plugins/babel-plugin-canyon/src/index.js +++ b/plugins/babel-plugin-canyon/src/index.js @@ -64,6 +64,54 @@ export default declare((api,config) => { // 生成初始覆盖率数据 const initialCoverageDataForTheCurrentFile = generateInitialCoverage(generate(path.node).code) + if (generate(path.node).code.includes('coverageData')) { + // console.log(path.node) + + + const t = api.types; + // 遍历 Program 中的所有节点 + path.traverse({ + VariableDeclarator(variablePath) { +// 检查是否是 coverageData + if ( + t.isIdentifier(variablePath.node.id, { name: "coverageData" }) && + t.isObjectExpression(variablePath.node.init) + ) { + // 查找插桩后的字段 + const hasInstrumentation = variablePath.node.init.properties.some((prop) => + t.isIdentifier(prop.key, { name: "_coverageSchema" }) || // 确保是已插桩的字段 + t.isIdentifier(prop.key, { name: "s" }) || + t.isIdentifier(prop.key, { name: "f" }) + ); + + if (hasInstrumentation) { + console.log("发现已插桩的 coverageData 节点,进行进一步修改..."); + + // 获取 coverageData 对象的 properties + const properties = variablePath.node.init.properties; + + // 删除 statementMap、fnMap 和 branchMap 属性 + const keysToRemove = ["statementMap", "fnMap", "branchMap"]; + + keysToRemove.forEach(key => { + const index = properties.findIndex(prop => + t.isIdentifier(prop.key, { name: key }) + ); + + if (index !== -1) { + properties.splice(index, 1); // 删除属性 + console.log(`已删除 ${key} 属性。`); + } + }); + + // 打印修改后的代码 + const newCode = generate(variablePath.node).code; + console.log("修改后的代码:", newCode); + } + } + }}) + + } // generateCanyon(__canyon__) // 生成canyon代码