From 3e15a61d01a3ecc152405843892ca0a79ec18ea2 Mon Sep 17 00:00:00 2001 From: Allen Zhang <37892968+zhangtao25@users.noreply.github.com> Date: Wed, 6 Nov 2024 01:18:53 +0800 Subject: [PATCH] feat: update --- .../src/helpers/extract-coverage-data.js | 1 + plugins/babel-plugin-canyon/src/index.js | 72 +++++++++---------- 2 files changed, 34 insertions(+), 39 deletions(-) diff --git a/plugins/babel-plugin-canyon/src/helpers/extract-coverage-data.js b/plugins/babel-plugin-canyon/src/helpers/extract-coverage-data.js index a734f1af..1d55a84e 100644 --- a/plugins/babel-plugin-canyon/src/helpers/extract-coverage-data.js +++ b/plugins/babel-plugin-canyon/src/helpers/extract-coverage-data.js @@ -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 { diff --git a/plugins/babel-plugin-canyon/src/index.js b/plugins/babel-plugin-canyon/src/index.js index 2f9b46c5..7870391e 100644 --- a/plugins/babel-plugin-canyon/src/index.js +++ b/plugins/babel-plugin-canyon/src/index.js @@ -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__);