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

Bug in parsing standard ES6 syntax #777

Closed getify closed 7 years ago

getify commented 7 years ago

I'm using Node v7.7.1 and Istanbul 0.4.5.

/tmp/foo.js:

function foo({x: y = 1}) {
    return y;
}

foo({ x: 1 });

Trying to run istanbul against that file:

$] istanbul cover /tmp/foo.js

/tmp/foo.js:9
function foo({y=1:y=1}){__cov_HZB7T0SjedjCpEtyBZ5mdA.f['1']++;__cov_HZB7T0SjedjCpEtyBZ5mdA.s['2']++;return y;}__cov_HZB7T0SjedjCpEtyBZ5mdA.s['3']++;foo({x:1});
                 ^
SyntaxError: Unexpected token :
    at createScript (vm.js:53:10)
    at Object.runInThisContext (vm.js:95:10)
    at Module._compile (module.js:543:28)
    at Object.Module._extensions.(anonymous function) [as .js] (/usr/local/lib/node_modules/istanbul/lib/hook.js:107:24)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Function.Module.runMain (module.js:605:10)
    at runFn (/usr/local/lib/node_modules/istanbul/lib/command/common/run-with-cover.js:122:16)
    at /usr/local/lib/node_modules/istanbul/lib/command/common/run-with-cover.js:251:17

The code in /tmp/foo.js is totally valid ES6 syntax, and in fact it parses fine with Esprima.

If I take out the = 1 part in that parameter destructuring, Istanbul parses and tests fine. Using a parameter default is also fine. It's only the default inside the object destructuring that causes this failure.

Looking at the code quoted in the error output, it's transformed the function declaration to:

function foo({y=1:y=1}){ ..

That's clearly invalid. Doesn't seem like this is a bug in Esprima, but rather an Istanbul bug.

MarkHerhold commented 7 years ago

You may want to try version 1.1.0-alpha.1. Code is on the v1 branch.

getify commented 7 years ago

Thanks seems to be working!