Skip to content

Commit

Permalink
feat: merge
Browse files Browse the repository at this point in the history
  • Loading branch information
Allen Zhang (张涛) committed Nov 7, 2024
1 parent d8063b5 commit e2b1b15
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 104 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/publish-babel-plugin-canyon.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ jobs:
env:
NODE_AUTH_TOKEN: ${{secrets.NODE_AUTH_TOKEN}}
- run: |
pnpm dist-tag add [email protected].80 latest
pnpm dist-tag add [email protected].86 latest
env:
NODE_AUTH_TOKEN: ${{secrets.NODE_AUTH_TOKEN}}
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.78-beta.22",
"version": "1.8.86",
"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,5 +1,4 @@
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
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;
}
44 changes: 10 additions & 34 deletions plugins/babel-plugin-canyon/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ function newAtob() {
}
const newatob = newAtob()

function analyzeIntervalTime (intervalTime) {
if (!isNaN(Number(intervalTime))){
return String(intervalTime)
} else {
return '0'
}
}

export default declare((api,config) => {
api.assertVersion(7)
return {
Expand All @@ -58,46 +66,14 @@ export default declare((api,config) => {
BRANCH: config.branch || '-',
REPORT_ID: config.reportID || '-',
COMPARE_TARGET: config.compareTarget || '-',
INTERVAL_TIME: analyzeIntervalTime(config.intervalTime),
VERSION: packageJson.version || '-'
}


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


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
1 change: 1 addition & 0 deletions plugins/babel-plugin-canyon/templates/canyon.template.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
compareTarget: 'COMPARE_TARGET',
branch: 'BRANCH',
version: 'VERSION',
intervalTime: 'INTERVAL_TIME'
}


Expand Down
29 changes: 1 addition & 28 deletions plugins/babel-plugin-canyon/templates/inject-in-page-popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@
background: rgba(0,0,0,0.9);
font-size: 12px;
color: white;
z-index: 1000000;
}
.__canyon__segment{
margin-bottom: 20px;
Expand Down Expand Up @@ -154,34 +155,6 @@
margin-bottom: 10px;
display:flex;
gap: 5px
}
.canyon-form-value-reportid{
color: #333;
}
/* 设置未选中样式 */
.canyon-form-value-auto {
position: relative;
width: 15px;
height: 15px;
line-height: 15px;
border: 1px solid #949494;
/* 取消默认样式 */
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
}
/* 设置选中样式 */
.canyon-form-value-auto:checked {
background-color: deepskyblue;
}
.canyon-form-value-auto:checked::after {
content: "✓";
position: absolute;
top: 0;
width: 15px;
height: 15px;
color: #fff;
text-align: center;
}`
document.head.appendChild(style)

Expand Down
55 changes: 30 additions & 25 deletions tools/canyon-extension/public/interceptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ window.addEventListener('message', function (e) {
canyon: {
...window.__canyon__,
reportID: localStorage.getItem('__canyon__report__id__') || undefined,
intervalTime: localStorage.getItem('__canyon__interval__time__') || undefined,
intervalTime: localStorage.getItem('__canyon__interval__time__') || window.__canyon__.intervalTime,
reporter: localStorage.getItem('__canyon__reporter__') || window.__canyon__.reporter,
},
coverage: window.__coverage__,
Expand All @@ -28,28 +28,33 @@ window.addEventListener('message', function (e) {
}
});

if (!isNaN(Number(localStorage.getItem('__canyon__interval__time__')))) {
const num = Number(localStorage.getItem('__canyon__interval__time__'));
if (num > 0) {
setInterval(() => {
if (window.__canyon__ && window.__coverage__) {
fetch(window.__canyon__.dsn, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${window.__canyon__.reporter}`,
},
body: JSON.stringify({
coverage: window.__coverage__,
...window.__canyon__,
reportID: localStorage.getItem('__canyon__report__id__') || undefined,
}),
}).then(() => {
console.log('report coverage success');
});
} else {
console.log('coverage or canyon data is not ready');
}
}, num * 1000);
// 1s后再去获取尝试间隔上报
setTimeout(()=>{
// 先尝试本地获取,如果没有再去获取window上的数据
const __canyon__interval__time__ = Number(localStorage.getItem('__canyon__interval__time__')) || Number(window?.__canyon__?.intervalTime);
if (!isNaN(__canyon__interval__time__)) {
const num = __canyon__interval__time__;
if (num > 0) {
setInterval(() => {
if (window.__canyon__ && window.__coverage__) {
fetch(window.__canyon__.dsn, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${window.__canyon__.reporter}`,
},
body: JSON.stringify({
coverage: window.__coverage__,
...window.__canyon__,
reportID: localStorage.getItem('__canyon__report__id__') || undefined,
}),
}).then(() => {
console.log('report coverage success');
});
} else {
console.log('coverage or canyon data is not ready');
}
}, num * 1000);
}
}
}
},1000)
2 changes: 1 addition & 1 deletion tools/canyon-extension/public/manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Canyon Extension",
"version": "1.0.7",
"version": "1.0.9",
"manifest_version": 3,
"description": "Canyon manual report plugin",
"icons": {
Expand Down

0 comments on commit e2b1b15

Please sign in to comment.