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 · 1 comment · Fixed by #471
Closed

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

HCanber opened this issue Oct 23, 2015 · 1 comment · Fixed by #471
Assignees
Labels
bug Bug or defect
Milestone

Comments

@HCanber
Copy link
Contributor

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 pushed 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 pushed 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 Bug or defect label Oct 27, 2015
@geek geek added this to the 7.0.0 milestone Oct 27, 2015
@geek geek closed this as completed in #471 Oct 27, 2015
@lock
Copy link

lock bot commented Jan 9, 2020

This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions.

@lock lock bot locked as resolved and limited conversation to collaborators Jan 9, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Bug or defect
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants