Skip to content

Commit

Permalink
Merge 8c4343f into 8a678fa
Browse files Browse the repository at this point in the history
  • Loading branch information
Victorystick committed Jan 4, 2016
2 parents 8a678fa + 8c4343f commit 26b4142
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 9 deletions.
35 changes: 33 additions & 2 deletions lib/instrumenter.js
Expand Up @@ -397,6 +397,7 @@
this.walker = new Walker({
ArrowFunctionExpression: [ this.arrowBlockConverter ],
ExpressionStatement: this.coverStatement,
ExportNamedDeclaration: this.coverExport,
BreakStatement: this.coverStatement,
ContinueStatement: this.coverStatement,
DebuggerStatement: this.coverStatement,
Expand Down Expand Up @@ -790,6 +791,7 @@
coverStatement: function (node, walker) {
var sName,
incrStatementCount,
parent,
grandParent;

this.maybeSkipNode(node, 'next');
Expand All @@ -805,10 +807,19 @@
}
}
}

if (node.type === SYNTAX.FunctionDeclaration.name) {
sName = this.statementName(node.loc, 1);
// Called for the side-effect of setting the function's statement count to 1.
this.statementName(node.loc, 1);
} else {
// We let `coverExport` handle ExportNamedDeclarations.
parent = walker.parent();
if (parent && parent.node.type === SYNTAX.ExportNamedDeclaration.name) {
return;
}

sName = this.statementName(node.loc);

incrStatementCount = astgen.statement(
astgen.postIncrement(
astgen.subscript(
Expand All @@ -817,10 +828,31 @@
)
)
);

this.splice(incrStatementCount, node, walker);
}
},

coverExport: function (node, walker) {
var sName, incrStatementCount;

if ( !node.declaration || !node.declaration.declarations ) { return; }

this.maybeSkipNode(node, 'next');

sName = this.statementName(node.declaration.loc);
incrStatementCount = astgen.statement(
astgen.postIncrement(
astgen.subscript(
astgen.dot(astgen.variable(this.currentState.trackerVar), astgen.variable('s')),
astgen.stringLiteral(sName)
)
)
);

this.splice(incrStatementCount, node, walker);
},

splice: function (statements, node, walker) {
var targetNode = walker.isLabeled() ? walker.parent().node : node;
targetNode.prepend = targetNode.prepend || [];
Expand Down Expand Up @@ -1048,4 +1080,3 @@
}

}(typeof module !== 'undefined' && typeof module.exports !== 'undefined' && typeof exports !== 'undefined'));

23 changes: 16 additions & 7 deletions test/es6.js
@@ -1,16 +1,25 @@
var esprima = require('esprima');

function tryThis(str, feature) {
try {
/*jshint evil: true */
eval(str);
} catch (ex) {
console.error('ES6 feature [' + feature + '] is not available in this environment');
return false;
// We can test instrumentation of exports even if the environment doesn't support them.
if (feature !== 'export') {
try {
/*jshint evil: true */
eval(str);
} catch (ex) {
console.error('ES6 feature [' + feature + '] is not available in this environment');
return false;
}
}

// esprima parses sources with sourceType 'script' per default.
// The only way to enable `import`/`export` is to parse as sourceType 'module'.
try {
esprima.parse(str);
try {
esprima.parse(str);
} catch (ex) {
esprima.parse(str, { sourceType: 'module' });
}
} catch (ex) {
console.error('ES6 feature [' + feature + '] is not yet supported by esprima mainline');
return false;
Expand Down
5 changes: 5 additions & 0 deletions test/helper.js
Expand Up @@ -104,6 +104,11 @@ function setup(file, codeArray, opts) {
}
return;
}

// `export`/`import` cannot be wrapped inside a function.
// For our purposes, simply remove the `export` from export declarations.
generated = generated.replace(/export (var|function|let|const)/g, '$1');

var wrappedCode = '(function (args) { var output;\n' + generated + '\nreturn output;\n})',
fn;
global[coverageVariable] = undefined;
Expand Down
18 changes: 18 additions & 0 deletions test/instrumentation/test-es6-export.js
Expand Up @@ -21,6 +21,24 @@ if (require('../es6').isExportAvailable()) {
statements: {'1': 1, '2': 1, '3': 1}
});
test.done();
},

'should cover export declarations': function (test) {
code = [
'export var a = 2, b = 3;',
'output = a + b'
];
verifier = helper.verifier(__filename, code, {
esModules: true,
noAutoWrap: true
});
verifier.verify(test, [], 5, {
lines: {'1':1, '2': 1},
branches: {},
functions: {},
statements: {'1': 1, '2': 1}
});
test.done();
}
};
}

0 comments on commit 26b4142

Please sign in to comment.