Skip to content

Commit

Permalink
feat: update
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangtao25 committed Nov 5, 2024
1 parent 786ad86 commit 4f436a2
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions plugins/babel-plugin-canyon/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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代码
Expand Down

0 comments on commit 4f436a2

Please sign in to comment.