Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: istanbul-lib-source-maps to preserve implicit else when sourcemaps are used #706

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions packages/istanbul-lib-source-maps/lib/transformer.js
Expand Up @@ -82,6 +82,18 @@ class SourceMapTransformer {
locs.push(mapping.loc);
mappedHits.push(hits[i]);
}
// Check if this is an implicit else
else if (
branchMeta.type === 'if' &&
i > 0 &&
loc.start.line === undefined &&
loc.start.end === undefined &&
loc.end.line === undefined &&
loc.end.end === undefined
) {
locs.push(loc);
mappedHits.push(hits[i]);
}
Comment on lines +85 to +96
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For reference:

- `locations[index].start` **[Object][1]** the branch component start location object. (`{}` for if branch without else)

- `locations[index].end` **[Object][1]** the branch component end location object. (`{}` for if branch without else)

// Check if the branch is a conditional if branch.
branchMeta[branchName].type === 'if' &&
// Check if the branch has an implicit else.
branchArray.length === 2 &&
// Check if the implicit else branch is unnacounted for.
metaArray.length === 1 &&
// Check if the implicit else branch is uncovered.
branchArray[1] === 0

});

const locMapping = branchMeta.loc
Expand Down
@@ -0,0 +1,104 @@
{
"path": "/path/to/file.js",
"statementMap": {
"0": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 4,
"column": 3
}
},
"1": {
"start": {
"line": 3,
"column": 4
},
"end": {
"line": 3,
"column": 13
}
},
"2": {
"start": {
"line": 6,
"column": 0
},
"end": {
"line": 6,
"column": 10
}
}
},
"fnMap": {
"0": {
"name": "add",
"decl": {
"start": {
"line": 1,
"column": 9
},
"end": {
"line": 1,
"column": 12
}
},
"loc": {
"start": {
"line": 1,
"column": 19
},
"end": {
"line": 5,
"column": 1
}
},
"line": 1
}
},
"branchMap": {
"0": {
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 4,
"column": 3
}
},
"type": "if",
"locations": [
{
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 4,
"column": 3
}
},
{
"start": {},
"end": {}
}
],
"line": 2
}
},
"s": {
"0": 1,
"1": 0,
"2": 1
},
"f": {
"0": 1
},
"b": {
"0": [0, 1]
}
}
47 changes: 47 additions & 0 deletions packages/istanbul-lib-source-maps/test/transformer.test.js
Expand Up @@ -140,4 +140,51 @@ describe('transformer', () => {
switchBranchCoverageDataExpectedResult
);
});

it('correctly maps branch count of implicit else', async () => {
const implicitElseCoverageData = require('./testdata/ifWithImplicitElseCoverageData.json');
const sourceMap = {
version: 3,
sources: [sourceFileSlash],
mappings:
'AACA,SAAS,IAAI,GAAW,GAAmB;AAGvC,UAAQ,IAAI,aAAa;AAEzB,MAAG,MAAM,IAAI;AACT,WAAO;AAAA,EACX;AACJ;AAEA,IAAI,GAAG,CAAC;'
};

const coverageMap = createMap({});
coverageMap.addFileCoverage(implicitElseCoverageData);

const transformer = new SourceMapTransformer(
() => new SourceMapConsumer(sourceMap)
);

const mapped = await transformer.transform(coverageMap);
const fileCoverage = mapped.fileCoverageFor(sourceFileSlash);

assert.deepEqual(fileCoverage.data.b, {
'0': [0, 1]
});

assert.deepEqual(fileCoverage.data.branchMap['0'].locations, [
{
start: {
line: 5,
column: 4
},
end: {
line: 8,
column: 15
}
},
{
start: {
line: undefined,
column: undefined
},
end: {
line: undefined,
column: undefined
}
}
]);
});
});