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 4f436a2 commit 9482661
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 25 deletions.
2 changes: 1 addition & 1 deletion plugins/babel-plugin-canyon/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "babel-plugin-canyon",
"version": "1.8.81",
"version": "1.8.78-beta.22",
"description": "A Babel plugin cooperates with istanbul to report the coverage",
"author": "Allen Zhang <[email protected]>",
"license": "MIT",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
// import fs from 'fs';
// import path from 'path'
import fs from 'fs';
import path from 'path'
import {extractCoverageData} from "./extract-coverage-data";


export const generateInitialCoverage = (paramsPath) => {
const initialCoverageDataForTheCurrentFile = extractCoverageData(paramsPath)
// const filePath = './.canyon_output/coverage-final.json';
// const dir = path.dirname(filePath);
// if (!fs.existsSync(dir)) {
// fs.mkdirSync(dir, {recursive: true});
// }
// // 防止返回的数据为空
// if (initialCoverageDataForTheCurrentFile && initialCoverageDataForTheCurrentFile.path) {
// fs.writeFileSync(`./.canyon_output/coverage-${Math.random()}.json`, JSON.stringify({
// [initialCoverageDataForTheCurrentFile.path]: initialCoverageDataForTheCurrentFile
// }, null, 2), 'utf-8');
// }
const filePath = './.canyon_output/coverage-final.json';
const dir = path.dirname(filePath);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, {recursive: true});
}
// 防止返回的数据为空
if (initialCoverageDataForTheCurrentFile && initialCoverageDataForTheCurrentFile.path) {
fs.writeFileSync(`./.canyon_output/coverage-${Math.random()}.json`, JSON.stringify({
[initialCoverageDataForTheCurrentFile.path]: initialCoverageDataForTheCurrentFile
}, null, 2), 'utf-8');
}
return initialCoverageDataForTheCurrentFile;
}
13 changes: 2 additions & 11 deletions plugins/babel-plugin-canyon/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,13 @@ 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
// 检查是否是 coverageData
if (
t.isIdentifier(variablePath.node.id, { name: "coverageData" }) &&
t.isObjectExpression(variablePath.node.init)
Expand All @@ -85,13 +84,11 @@ export default declare((api,config) => {
);

if (hasInstrumentation) {
console.log("发现已插桩的 coverageData 节点,进行进一步修改...");

// 获取 coverageData 对象的 properties
const properties = variablePath.node.init.properties;

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

keysToRemove.forEach(key => {
const index = properties.findIndex(prop =>
Expand All @@ -100,17 +97,11 @@ export default declare((api,config) => {

if (index !== -1) {
properties.splice(index, 1); // 删除属性
console.log(`已删除 ${key} 属性。`);
}
});

// 打印修改后的代码
const newCode = generate(variablePath.node).code;
console.log("修改后的代码:", newCode);
}
}
}})

}
// generateCanyon(__canyon__)

Expand Down

0 comments on commit 9482661

Please sign in to comment.