Skip to content

Commit

Permalink
fix: Be more friendly to ts-node. (#352)
Browse files Browse the repository at this point in the history
ts-node gets angry when coverageData.hash is set after coverageData is
declared, add it to the initial object instead of setting it separately.

Also only output `Function` if a replacement is found in scope.

Fixes #336
  • Loading branch information
coreyfarrell committed Apr 3, 2019
1 parent deb3963 commit 40d15f5
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 8 deletions.
26 changes: 19 additions & 7 deletions packages/istanbul-lib-instrument/src/visitor.js
Expand Up @@ -515,10 +515,13 @@ const codeVisitor = {
ConditionalExpression: entries(coverTernary),
LogicalExpression: entries(coverLogicalExpression)
};
const globalTemplateFunction = template(`
const globalTemplateAlteredFunction = template(`
var Function = (function(){}).constructor;
var global = (new Function(GLOBAL_COVERAGE_SCOPE))();
`);
const globalTemplateFunction = template(`
var global = (new Function(GLOBAL_COVERAGE_SCOPE))();
`);
const globalTemplateVariable = template(`
var global = GLOBAL_COVERAGE_SCOPE;
`);
Expand All @@ -534,7 +537,6 @@ const coverageTemplate = template(`
if (coverage[path] && coverage[path].hash === hash) {
return coverage[path];
}
coverageData.hash = hash;
return coverage[path] = coverageData;
})();
`);
Expand Down Expand Up @@ -619,15 +621,25 @@ function programVisitor(
const hash = createHash(SHA)
.update(JSON.stringify(coverageData))
.digest('hex');
coverageData.hash = hash;
const coverageNode = T.valueToNode(coverageData);
delete coverageData[MAGIC_KEY];
delete coverageData.hash;
let gvTemplate;
if (opts.coverageGlobalScopeFunc) {
gvTemplate = globalTemplateFunction({
GLOBAL_COVERAGE_SCOPE: T.stringLiteral(
'return ' + opts.coverageGlobalScope
)
});
if (path.scope.getBinding('Function')) {
gvTemplate = globalTemplateAlteredFunction({
GLOBAL_COVERAGE_SCOPE: T.stringLiteral(
'return ' + opts.coverageGlobalScope
)
});
} else {
gvTemplate = globalTemplateFunction({
GLOBAL_COVERAGE_SCOPE: T.stringLiteral(
'return ' + opts.coverageGlobalScope
)
});
}
} else {
gvTemplate = globalTemplateVariable({
GLOBAL_COVERAGE_SCOPE: opts.coverageGlobalScope
Expand Down
8 changes: 7 additions & 1 deletion packages/istanbul-lib-instrument/test/util/verifier.js
Expand Up @@ -73,7 +73,13 @@ class Verifier {
);
const initial = readInitialCoverage(this.getGeneratedCode());
assert.ok(initial);
assert.deepEqual(initial.coverageData, this.result.emptyCoverage);
assert.deepEqual(
initial.coverageData,
Object.assign(
{ hash: initial.coverageData.hash },
this.result.emptyCoverage
)
);
assert.ok(initial.path);
if (this.result.file) {
assert.equal(initial.path, this.result.file);
Expand Down
24 changes: 24 additions & 0 deletions packages/istanbul-lib-instrument/test/varia.test.js
Expand Up @@ -202,6 +202,30 @@ describe('varia', () => {
assert.ok(code.match(/cov_(.+);export class App extends/));
});

it('declares Function when needed', () => {
const v = verifier.create(
'function Function() {}',
{ generateOnly: true },
{ esModules: true }
);
assert.ok(!v.err);

const code = v.getGeneratedCode();
assert.ok(code.match(/var Function\s*=/));
});

it('does not declare Function when not needed', () => {
const v = verifier.create(
'function differentFunction() {}',
{ generateOnly: true },
{ esModules: true }
);
assert.ok(!v.err);

const code = v.getGeneratedCode();
assert.ok(!code.match(/var Function\s*=/));
});

it('does not add extra parenthesis when superclass is an identifier', () => {
const v = verifier.create('class App extends Component {};', {
generateOnly: true
Expand Down

0 comments on commit 40d15f5

Please sign in to comment.