gotwarlost / istanbul

Yet another JS code coverage tool that computes statement, line, function and branch coverage with module loader hooks to transparently add coverage when running tests. Supports all JS coverage use cases including unit tests, server side functional tests and browser tests. Built for scale.
Other
8.7k stars 787 forks source link

New `**` (pow) operator support #780

Open char0n opened 7 years ago

char0n commented 7 years ago

It seems that istanbul cannot parse code like this.

const test = 2 ** 13;

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Exponentiation

Processing file containing this operator throws:

Transformation error; return original code
{ Error: Line 6: Unexpected token *
    at constructError (C:\Users\vladi\Documents\GitHub\ramda-adjunct\node_modules\istanbul\node_modules\esprima\esprima.js:2407:21)
    at createError (C:\Users\vladi\Documents\GitHub\ramda-adjunct\node_modules\istanbul\node_modules\esprima\esprima.js:2426:17)
    at unexpectedTokenError (C:\Users\vladi\Documents\GitHub\ramda-adjunct\node_modules\istanbul\node_modules\esprima\esprima.js:2500:13)
    at throwUnexpectedToken (C:\Users\vladi\Documents\GitHub\ramda-adjunct\node_modules\istanbul\node_modules\esprima\esprima.js:2505:15)
    at parsePrimaryExpression (C:\Users\vladi\Documents\GitHub\ramda-adjunct\node_modules\istanbul\node_modules\esprima\esprima.js:3307:13)
    at inheritCoverGrammar (C:\Users\vladi\Documents\GitHub\ramda-adjunct\node_modules\istanbul\node_modules\esprima\esprima.js:2681:18)
    at parseLeftHandSideExpressionAllowCall (C:\Users\vladi\Documents\GitHub\ramda-adjunct\node_modules\istanbul\node_modules\esprima\esprima.js:3414:20
....

Is there something to bypass this (except using Math.pow of course :) ?

chris-verclytte commented 7 years ago

I am having kind of same problem but for me a simple case like 2 ** 3 works but it will fail when using negative numbers. Actually -2 ** 3 will natively never work in ES, which seems understandable because this syntax is not clear about wether the power is applied on 2 or on -2. But this would work (-2) ** 3 because the syntax is now explicit.

The problem is that running istanbul, this syntax is simplified in -2 ** 3 which is no more valid.

char0n commented 7 years ago

I used babel to transpile before running istanbul and it worked like a charm after that. Your problem seems quite interesting too.

chris-verclytte commented 7 years ago

Okay, nice that you found a workaround. In my case, I am having the problem on backend using node.js so I am not passing through transpilation step.