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

Parse error on destructured object parameter with defaults #717

Open bryanburgers opened 7 years ago

bryanburgers commented 7 years ago

I have a function defined as follows in my code:

function thing({ one: localOne = 1 } = {}) {
  return localOne;
}

This bit of code is causing Istanbul to fail.

Error message $ npm run coverage > istanbul-test@0.0.0 coverage /Users/bburgers/projects/tmp > istanbul cover --include-all-sources -i 'lib/**' _mocha 'test/**/*.js' No coverage information was collected, exit without writing coverage information /Users/bburgers/projects/tmp/lib/thing.js:9 function thing({localOne=1:localOne=1}={}){__cov_xOmAt4oxzoE9o4mYWd1f7w.f['1']++;__cov_xOmAt4oxzoE9o4mYWd1f7w.s['2']++;return localOne;}__cov_xOmAt4oxzoE9o4mYWd1f7w.s['3']++;module.exports=thing; ^ SyntaxError: Unexpected token : at Object.exports.runInThisContext (vm.js:76:16) at Module._compile (module.js:542:28) at Object.Module._extensions.(anonymous function) [as .js] (/Users/bburgers/projects/tmp/node_modules/istanbul/lib/hook.js:107:24) at Module.load (module.js:487:32) at tryModuleLoad (module.js:446:12) at Function.Module._load (module.js:438:3) at Module.require (module.js:497:17) at require (internal/module.js:20:19) at Object. (/Users/bburgers/projects/tmp/test/thing.test.js:2:15) at Module._compile (module.js:570:32) at Object.Module._extensions..js (module.js:579:10) at Object.Module._extensions.(anonymous function) [as .js] (/Users/bburgers/projects/tmp/node_modules/istanbul/lib/hook.js:109:37) at Module.load (module.js:487:32) at tryModuleLoad (module.js:446:12) at Function.Module._load (module.js:438:3) at Module.require (module.js:497:17) at require (internal/module.js:20:19) at /Users/bburgers/projects/tmp/node_modules/mocha/lib/mocha.js:222:27 at Array.forEach (native) at Mocha.loadFiles (/Users/bburgers/projects/tmp/node_modules/mocha/lib/mocha.js:219:14) at Mocha.run (/Users/bburgers/projects/tmp/node_modules/mocha/lib/mocha.js:487:10) at Object. (/Users/bburgers/projects/tmp/node_modules/mocha/bin/_mocha:458:18) at Module._compile (module.js:570:32) at Object.Module._extensions..js (module.js:579:10) at Object.Module._extensions.(anonymous function) [as .js] (/Users/bburgers/projects/tmp/node_modules/istanbul/lib/hook.js:109:37) at Module.load (module.js:487:32) at tryModuleLoad (module.js:446:12) at Function.Module._load (module.js:438:3) at Function.Module.runMain (module.js:604:10) at runFn (/Users/bburgers/projects/tmp/node_modules/istanbul/lib/command/common/run-with-cover.js:122:16) at /Users/bburgers/projects/tmp/node_modules/istanbul/lib/command/common/run-with-cover.js:251:17 at /Users/bburgers/projects/tmp/node_modules/istanbul/lib/util/file-matcher.js:68:16 at /Users/bburgers/projects/tmp/node_modules/async/lib/async.js:52:16 at /Users/bburgers/projects/tmp/node_modules/async/lib/async.js:361:13 at /Users/bburgers/projects/tmp/node_modules/async/lib/async.js:52:16 at done (/Users/bburgers/projects/tmp/node_modules/async/lib/async.js:246:17) at /Users/bburgers/projects/tmp/node_modules/async/lib/async.js:44:16 at /Users/bburgers/projects/tmp/node_modules/async/lib/async.js:358:17 at LOOP (fs.js:1758:14) at _combinedTickCallback (internal/process/next_tick.js:67:7) at process._tickCallback (internal/process/next_tick.js:98:9)

This does not seem to be an issue with esprima; this bit of code is correctly parsed by esprima@2.7.x.

It also seems to happen only with the combination of the two es6 features. Both of these bits of code seem to not bother Istanbul:

function thing2({ one: localOne } = {}) {
  return localOne;
}

function thing3({ one = 1 } = {}) {
  return one;
}

I pushed up a repo that replicates this issue: https://github.com/bryanburgers/istanbul-error-reproduction

Run tests with npm test. Run istanbul with npm run coverage.

Running on node 6.8.1.

darrennolan commented 7 years ago

Is this a similar issue? Due to the logger = default to a new instance of the function?

image

The tests exhaustedly try all combinations of the init function, and then I get 'yellow' for the rest of the method - which seems highly odd as they are indeed run through.

Either that or I'm missing what the feedback here is properly providing.

airbinder commented 7 years ago

Hi, I noticed an error also happened on my codes. The error is:

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] (/home/user/.nvm/versions/node/v7.9.0/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 Module.require (module.js:498:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/home/user/codes/projects/myproject/index.js:9:552)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions.(anonymous function) [as .js] (/home/user/.nvm/versions/node/v7.9.0/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 Module.require (module.js:498:17)

After much commenting and uncommenting the code, I have located the error happened when using destructuring object with default, much like this issue.

Cheers.

jy95 commented 7 years ago

Same for me ^^ You can find it there : https://coveralls.io/builds/13109007/source?filename=src%2FTorrentLibrary.js#L190

EDIT : If anyone wants a quick fix , use / istanbul ignore next /

avocadee commented 6 years ago

same for me