Skip to content

Commit

Permalink
fix: Eliminate babel hoisting of the coverage variable (#481)
Browse files Browse the repository at this point in the history
This replaces the coverage variable with a function that returns
coverage data.

Fixes #92
  • Loading branch information
coreyfarrell committed Oct 5, 2019
1 parent dd7048e commit 8dfbcba
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
31 changes: 23 additions & 8 deletions packages/istanbul-lib-instrument/src/visitor.js
Expand Up @@ -28,6 +28,7 @@ class VisitState {
ignoreClassMethods = []
) {
this.varName = genVar(sourceFilePath);
this.varCalled = false;
this.attrs = {};
this.nextIgnore = null;
this.cov = new SourceCoverage(sourceFilePath);
Expand Down Expand Up @@ -167,12 +168,13 @@ class VisitState {
? // If `index` present, turn `x` into `x[index]`.
x => T.memberExpression(x, T.numericLiteral(index), true)
: x => x;
this.varCalled = true;
return T.updateExpression(
'++',
wrap(
T.memberExpression(
T.memberExpression(
T.identifier(this.varName),
T.callExpression(T.identifier(this.varName), []),
T.identifier(type)
),
T.numericLiteral(id),
Expand Down Expand Up @@ -535,18 +537,24 @@ const globalTemplateVariable = template(`
`);
// the template to insert at the top of the program.
const coverageTemplate = template(`
var COVERAGE_VAR = (function () {
function COVERAGE_FUNCTION () {
var path = PATH;
var hash = HASH;
GLOBAL_COVERAGE_TEMPLATE
var gcv = GLOBAL_COVERAGE_VAR;
var coverageData = INITIAL;
var coverage = global[gcv] || (global[gcv] = {});
if (coverage[path] && coverage[path].hash === hash) {
return coverage[path];
if (!coverage[path] || coverage[path].hash !== hash) {
coverage[path] = coverageData;
}
return coverage[path] = coverageData;
})();
var actualCoverage = coverage[path];
COVERAGE_FUNCTION = function () {
return actualCoverage;
}
return actualCoverage;
}
`);
// the rewire plugin (and potentially other babel middleware)
// may cause files to be instrumented twice, see:
Expand Down Expand Up @@ -660,12 +668,19 @@ function programVisitor(
const cv = coverageTemplate({
GLOBAL_COVERAGE_VAR: T.stringLiteral(opts.coverageVariable),
GLOBAL_COVERAGE_TEMPLATE: gvTemplate,
COVERAGE_VAR: T.identifier(visitState.varName),
COVERAGE_FUNCTION: T.identifier(visitState.varName),
PATH: T.stringLiteral(sourceFilePath),
INITIAL: coverageNode,
HASH: T.stringLiteral(hash)
});
cv._blockHoist = 5;
// explicitly call this.varName if this file has no coverage
if (!visitState.varCalled) {
path.node.body.unshift(
T.expressionStatement(
T.callExpression(T.identifier(visitState.varName), [])
)
);
}
path.node.body.unshift(cv);
return {
fileCoverage: coverageData,
Expand Down
16 changes: 12 additions & 4 deletions packages/istanbul-lib-instrument/test/varia.test.js
Expand Up @@ -79,7 +79,7 @@ describe('varia', () => {
const code = v.getGeneratedCode();
assert.ok(
code.match(
/}\(\);export function fn1\(\){}export default function\(\){}/
/return actualCoverage;}cov_[^(]+\(\);export function fn1\(\){}export default function\(\){}/
)
);
});
Expand All @@ -96,7 +96,7 @@ describe('varia', () => {
const code = v.getGeneratedCode();
assert.ok(
code.match(
/}\(\);export function fn1\(\){cov_(.+)\.f\[\d+\]\+\+;}export default function\(\){cov_(.+)\.f\[\d+\]\+\+;}/
/return actualCoverage;}export function fn1\(\){cov_(.+)\.f\[\d+\]\+\+;}export default function\(\){cov_(.+)\.f\[\d+\]\+\+;}/
)
);
});
Expand Down Expand Up @@ -199,7 +199,11 @@ describe('varia', () => {
assert.ok(!v.err);

const code = v.getGeneratedCode();
assert.ok(code.match(/cov_(.+);export class App extends/));
assert.ok(
code.match(
/return actualCoverage;}cov_[^(]+\(\);export class App extends/
)
);
});

it('declares Function when needed', () => {
Expand Down Expand Up @@ -233,7 +237,11 @@ describe('varia', () => {
assert.ok(!v.err);

const code = v.getGeneratedCode();
assert.ok(code.match(/cov_(.+);class App extends Component/));
assert.ok(
code.match(
/return actualCoverage;}cov_[^(]+\(\);class App extends Component/
)
);
});

it('can store coverage object in alternative scope', () => {
Expand Down

0 comments on commit 8dfbcba

Please sign in to comment.