Skip to content

Commit

Permalink
fixed coverage for static initialization blocks (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
cenfun committed Jun 13, 2024
1 parent 3d86578 commit ffa7017
Show file tree
Hide file tree
Showing 15 changed files with 207 additions and 82 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

- 2.8.4
- updated UI: highlight keywords for searching results

- fixed coverage for static initialization blocks (#37)

- 2.8.3
- fixed error occurred in electron (#27)
Expand Down
30 changes: 25 additions & 5 deletions lib/converter/ast-visitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ const getParentCount = (reverseParents, functionCount) => {
return functionCount;
};

const getFunctionRange = (start, end, coverageInfo) => {
const getFunctionRange = (start, end, type, coverageInfo) => {

const {
functionMap, functionNameMap, functionUncoveredRanges
functionMap, functionNameMap, functionStaticRanges, functionUncoveredRanges
} = coverageInfo;

// exact matched in functionMap
Expand All @@ -49,6 +49,13 @@ const getFunctionRange = (start, end, coverageInfo) => {
return nameRange;
}

if (type === 'StaticBlock' && functionStaticRanges.length) {
const staticRange = Util.findInRanges(start, end, functionStaticRanges, 'startOffset', 'endOffset');
if (staticRange) {
return staticRange;
}
}

// find in uncoveredRanges
return Util.findInRanges(start, end, functionUncoveredRanges, 'startOffset', 'endOffset');
};
Expand Down Expand Up @@ -283,8 +290,10 @@ const updateBranchCount = (group) => {
if (none) {
return;
}

item.block = getFunctionBlock(start, end, functionState);
setGeneratedOnly(item.block, group.generatedOnly);

});


Expand Down Expand Up @@ -464,6 +473,15 @@ const collectNodes = (ast) => {
});
},

// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/static
// as a function "functionName": "<static_initializer>",
StaticBlock: (node, parents) => {
const reverseParents = [].concat(parents).reverse();
functionNodes.push({
node,
reverseParents
});
},

// ===============================================================================
// functions
Expand Down Expand Up @@ -617,10 +635,12 @@ const getRootFunctionState = (ast, coverageInfo) => {
const findFunctionRange = (item, coverageInfo) => {

const { node, reverseParents } = item;
const { start, end } = node;
const {
start, end, type
} = node;

// try function start/end
const functionRange = getFunctionRange(start, end, coverageInfo);
const functionRange = getFunctionRange(start, end, type, coverageInfo);
if (functionRange) {
return functionRange;
}
Expand All @@ -629,7 +649,7 @@ const findFunctionRange = (item, coverageInfo) => {
// 0 is function self
const parent = reverseParents[1];
if (parent && parent.type === 'MethodDefinition') {
return getFunctionRange(parent.start, parent.end, coverageInfo);
return getFunctionRange(parent.start, parent.end, parent.type, coverageInfo);
}

};
Expand Down
6 changes: 6 additions & 0 deletions lib/converter/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const { collectAstInfo } = require('./ast-visitor.js');
const getCoverageInfo = (coverageList) => {
const functionMap = new Map();
const functionNameMap = new Map();
const functionStaticRanges = [];
const functionUncoveredRanges = [];
let rootRange;
coverageList.forEach((block) => {
Expand Down Expand Up @@ -61,6 +62,10 @@ const getCoverageInfo = (coverageList) => {
functionNameMap.set(functionRange.startOffset + functionName.length, functionRange);
}

if (functionName === '<static_initializer>') {
functionStaticRanges.push(functionRange);
}

// uncovered is unique
if (functionRange.count === 0) {
functionUncoveredRanges.push(functionRange);
Expand All @@ -76,6 +81,7 @@ const getCoverageInfo = (coverageList) => {
return {
functionMap,
functionNameMap,
functionStaticRanges,
functionUncoveredRanges,
rootRange
};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
"stylelint": "^16.6.1",
"stylelint-config-plus": "^1.1.2",
"supports-color": "^9.4.0",
"tsx": "^4.15.2",
"tsx": "^4.15.3",
"ws": "^8.17.0"
}
}
4 changes: 4 additions & 0 deletions test/mock/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const typescript = require('./typescript.ts');

const component = require('./component.js');

const { ClassWithStaticInitializationBlock } = require('./static.js');

function foo(argument) {
console.log('this is foo');

Expand Down Expand Up @@ -85,6 +87,8 @@ function init(stop) {
privateFunction();
}

new ClassWithStaticInitializationBlock();

}

const onload = (something) => {
Expand Down
32 changes: 32 additions & 0 deletions test/mock/src/static.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class ClassWithStaticInitializationBlock {
static staticProperty1 = 'Property 1';
static staticProperty2;
static {
const funInStatic = (v) => {
if (v) {
return v;
}
return 'function in static';
};
if (this.staticProperty2) {
this.staticProperty2 = 'Property 2';
} else {
this.staticProperty2 = 'Property 1';
funInStatic(this.staticProperty1);
}
}

static staticProperty3;

constructor() {
this.prop = 1;
}

myMethod() {
this.prop = 2;
}
}

module.exports = {
ClassWithStaticInitializationBlock
};
20 changes: 14 additions & 6 deletions test/snapshot/cli.snapshot.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"type": "v8",
"summary": {
"bytes": "57.25 %",
"statements": "61.63 %",
"branches": "53.52 %",
"functions": "58.67 %",
"lines": "49.53 %"
"bytes": "54.82 %",
"statements": "59.92 %",
"branches": "52.69 %",
"functions": "55.70 %",
"lines": "47.60 %"
},
"files": {
"test/mock/src/async.js": {
Expand Down Expand Up @@ -38,7 +38,7 @@
"statements": "0.00 %",
"lines": "0.00 %",
"bytes": "0.00 %",
"uncoveredLines": "1-118"
"uncoveredLines": "1-122"
},
"test/mock/src/statement.js": {
"functions": "0.00 %",
Expand All @@ -48,6 +48,14 @@
"bytes": "0.00 %",
"uncoveredLines": "1-32"
},
"test/mock/src/static.js": {
"functions": "0.00 %",
"branches": "0.00 %",
"statements": "0.00 %",
"lines": "0.00 %",
"bytes": "0.00 %",
"uncoveredLines": "1-32"
},
"test/mock/src/typescript.ts": {
"functions": "100.00 %",
"branches": "",
Expand Down
16 changes: 8 additions & 8 deletions test/snapshot/client.snapshot.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,36 @@
"lines": "40.00 %"
},
"files": {
"src/puppeteer.js": {
"src/playwright.js": {
"functions": "0.00 %",
"branches": "",
"statements": "66.67 %",
"lines": "25.00 %",
"bytes": "68.03 %",
"bytes": "68.55 %",
"uncoveredLines": "3-5"
},
"src/puppeteer.css": {
"src/playwright.css": {
"functions": "",
"branches": "",
"statements": "",
"lines": "45.45 %",
"bytes": "63.30 %",
"bytes": "63.68 %",
"uncoveredLines": "4-9"
},
"src/playwright.js": {
"src/puppeteer.js": {
"functions": "0.00 %",
"branches": "",
"statements": "66.67 %",
"lines": "25.00 %",
"bytes": "68.55 %",
"bytes": "68.03 %",
"uncoveredLines": "3-5"
},
"src/playwright.css": {
"src/puppeteer.css": {
"functions": "",
"branches": "",
"statements": "",
"lines": "45.45 %",
"bytes": "63.68 %",
"bytes": "63.30 %",
"uncoveredLines": "4-9"
}
}
Expand Down
26 changes: 17 additions & 9 deletions test/snapshot/esbuild.snapshot.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"type": "v8",
"summary": {
"bytes": "80.99 %",
"statements": "83.30 %",
"branches": "59.58 %",
"functions": "83.54 %",
"lines": "70.61 %"
"bytes": "81.00 %",
"statements": "83.37 %",
"branches": "59.43 %",
"functions": "83.33 %",
"lines": "70.88 %"
},
"files": {
"src/comments.js": {
Expand Down Expand Up @@ -128,13 +128,21 @@
"bytes": "81.63 %",
"uncoveredLines": "16-22"
},
"src/static.js": {
"functions": "80.00 %",
"branches": "50.00 %",
"statements": "83.33 %",
"lines": "75.00 %",
"bytes": "77.98 %",
"uncoveredLines": "9,11-13,25-27"
},
"src/index.js": {
"functions": "50.00 %",
"branches": "42.86 %",
"statements": "65.08 %",
"lines": "51.81 %",
"bytes": "61.62 %",
"uncoveredLines": "20-26,34-59,65-68,72-74,84-86,92-95,106"
"statements": "66.15 %",
"lines": "52.94 %",
"bytes": "63.82 %",
"uncoveredLines": "22-28,36-61,67-70,74-76,86-88,96-99,110"
}
}
}
21 changes: 14 additions & 7 deletions test/snapshot/istanbul.snapshot.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"type": "istanbul",
"summary": {
"lines": "82.46 %",
"statements": "82.55 %",
"functions": "76.11 %",
"branches": "58.46 %"
"lines": "82.33 %",
"statements": "82.42 %",
"functions": "75.71 %",
"branches": "58.33 %"
},
"files": {
"async.js": {
Expand Down Expand Up @@ -99,11 +99,11 @@
"uncoveredLines": "7,20"
},
"index.js": {
"lines": "69.64 %",
"lines": "70.68 %",
"functions": "53.84 %",
"statements": "70.17 %",
"statements": "71.18 %",
"branches": "57.14 %",
"uncoveredLines": "24,35-57,66-67,73,85,93-94"
"uncoveredLines": "26,37-59,68-69,75,87,97-98"
},
"statement.js": {
"lines": "100.00 %",
Expand All @@ -112,6 +112,13 @@
"branches": "100.00 %",
"uncoveredLines": ""
},
"static.js": {
"lines": "75.00 %",
"functions": "66.66 %",
"statements": "75.00 %",
"branches": "50.00 %",
"uncoveredLines": "9,12,26"
},
"typescript.ts": {
"lines": "100.00 %",
"functions": "100.00 %",
Expand Down
26 changes: 17 additions & 9 deletions test/snapshot/merge.snapshot.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"type": "v8",
"summary": {
"bytes": "73.51 %",
"statements": "77.35 %",
"branches": "59.72 %",
"functions": "70.79 %",
"lines": "63.16 %"
"bytes": "73.79 %",
"statements": "77.38 %",
"branches": "59.59 %",
"functions": "70.97 %",
"lines": "63.64 %"
},
"files": {
"test/mock/src/async.js": {
Expand Down Expand Up @@ -35,10 +35,10 @@
"test/mock/src/index.js": {
"functions": "53.85 %",
"branches": "57.14 %",
"statements": "69.84 %",
"lines": "55.42 %",
"bytes": "65.61 %",
"uncoveredLines": "23-25,34-59,65-68,72-74,84-86,92-95,106"
"statements": "70.77 %",
"lines": "56.47 %",
"bytes": "67.58 %",
"uncoveredLines": "25-27,36-61,67-70,74-76,86-88,96-99,110"
},
"test/mock/src/statement.js": {
"functions": "100.00 %",
Expand All @@ -48,6 +48,14 @@
"bytes": "100.00 %",
"uncoveredLines": ""
},
"test/mock/src/static.js": {
"functions": "75.00 %",
"branches": "50.00 %",
"statements": "75.00 %",
"lines": "75.00 %",
"bytes": "77.98 %",
"uncoveredLines": "9,11-13,25-27"
},
"test/mock/src/typescript.ts": {
"functions": "100.00 %",
"branches": "",
Expand Down
Loading

0 comments on commit ffa7017

Please sign in to comment.