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

for(var x of xs) is not handled correctly when coverage is turned on #470

Closed
HCanber opened this issue Oct 23, 2015 · 0 comments
Assignees
Labels
bug
Milestone

Comments

@HCanber
Copy link
Contributor

@HCanber HCanber commented Oct 23, 2015

I'm getting the error Unexpected token var for this code when running with -c flag:

In file test.js:

// require('lab') and other code deleted for clarity
require('./fail.js')

In file fail.js

'use strict';
var actions=[1,2,3]
var rm={}
for (var action of actions) {
    rm[action] = true
}

If declaration of action is moved outside the for-loop it works

var action
for (action of actions) {

Looks like && node.parent.type !== 'ForOfStatement' needs to be added to https://github.com/hapijs/lab/blob/master/lib/coverage.js#L165 to get:

if (trackedTypes.indexOf(node.type) !== -1 &&
    (node.type !== 'VariableDeclaration' || (node.parent.type !== 'ForStatement' && node.parent.type !== 'ForInStatement' && node.parent.type !== 'ForOfStatement')) &&
    (node.type !== 'ExpressionStatement' || node.expression.value !== 'use strict') &&
    node.parent.type !== 'LabeledStatement') {

    tracking.push(node.loc.start.line);
    node.set('global.__$$labCov._line(\'' + filename + '\',' + node.loc.start.line + ');' + node.source());
}

Found in lab version 6.2.0

HCanber added a commit to HCanber/lab that referenced this issue Oct 26, 2015
…e for-of

I got numbers in test/coverage.js (percent, sloc & hits) by first adding
another for-in loop into partial.js below the existing one:
    for (var o in j) {
        ++l;
    }

When the test worked and 100% code coverage was achieved, the loop was
changed to for (var o of j)
HCanber added a commit to HCanber/lab that referenced this issue Oct 26, 2015
This occurs when this is true:
node.type == "VariableDeclaration"
node.parent.type == "ForOfStatement"

Fixes issue hapijs#470
@geek geek added the bug label Oct 27, 2015
@geek geek added this to the 7.0.0 milestone Oct 27, 2015
@geek geek closed this in #471 Oct 27, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.