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 9482661 commit 3e15a61
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export function extractCoverageData(scriptContent) {
// console.log(scriptContent)
const reg0 = /var\s+coverageData\s*=\s*({[\s\S]*?});/;
const reg1 = /var\s+(\w+)\s*=\s*function\s*\(\)\s*\{([\s\S]*?)\}\(\);/
try {
Expand Down
72 changes: 33 additions & 39 deletions plugins/babel-plugin-canyon/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,46 +64,40 @@ export default declare((api,config) => {

// 生成初始覆盖率数据
const initialCoverageDataForTheCurrentFile = generateInitialCoverage(generate(path.node).code)
if (generate(path.node).code.includes('coverageData')) {


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) {
// 获取 coverageData 对象的 properties
const properties = variablePath.node.init.properties;

// 删除 statementMap、fnMap 和 branchMap 属性
const keysToRemove = ["statementMap", "fnMap", "branchMap","inputSourceMap"];

keysToRemove.forEach(key => {
const index = properties.findIndex(prop =>
t.isIdentifier(prop.key, { name: key })
);

if (index !== -1) {
properties.splice(index, 1); // 删除属性
}
});
}


const t = api.types;
// 遍历 Program 中的所有节点
path.traverse({
VariableDeclarator(variablePath) {
// 直接判断对象的属性是否存在,是否是coverageData
if (variablePath.node?.init?.properties?.some) {
// 查找插桩后的字段
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) {
// 获取 coverageData 对象的 properties
const properties = variablePath.node.init.properties;

// 删除 statementMap、fnMap 和 branchMap 属性
const keysToRemove = ["statementMap", "fnMap", "branchMap","inputSourceMap"];

keysToRemove.forEach(key => {
const index = properties.findIndex(prop =>
t.isIdentifier(prop.key, { name: key })
);

if (index !== -1) {
properties.splice(index, 1); // 删除属性
}
});
}
}})
}
// generateCanyon(__canyon__)
}
}})


// 生成canyon代码
const canyon = canyonTemplate(__canyon__);
Expand Down

0 comments on commit 3e15a61

Please sign in to comment.