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 786 forks source link

Invalid JS generated when exporting IIFE #700

Open shannon opened 8 years ago

shannon commented 8 years ago

I came across this issue while debugging an issue with https://github.com/guybedford/systemjs-istanbul/issues/4.

It seems that if you export an IIFE (well export the returned value anyways) the code generated from istanbul is invalid.

example.js

export default (function () {
  return 'foo';
})();
> istanbul instrument --es-modules example.js
var __cov_SIkTguWUcKOrc5jdRLQPSA = (Function('return this'))();                                                                                                                                   
if (!__cov_SIkTguWUcKOrc5jdRLQPSA.__coverage__) { __cov_SIkTguWUcKOrc5jdRLQPSA.__coverage__ = {}; }                                                                                               
__cov_SIkTguWUcKOrc5jdRLQPSA = __cov_SIkTguWUcKOrc5jdRLQPSA.__coverage__;                                                                                                                         
if (!(__cov_SIkTguWUcKOrc5jdRLQPSA['/home/shannon/example.js'])) {                                                                                                                                 
   __cov_SIkTguWUcKOrc5jdRLQPSA['/home/shannon/example.js'] = {"path":"/home/shannon/example.js","s":{"1":0},"b":{},"f":{"1":0},"fnMap":{"1":{"name":"(anonymous_1)","line":1,"loc":{"start":{"line"
:1,"column":16},"end":{"line":1,"column":28}}}},"statementMap":{"1":{"start":{"line":2,"column":2},"end":{"line":2,"column":15}}},"branchMap":{}};                                                
}                                                                                                                                                                                                 
__cov_SIkTguWUcKOrc5jdRLQPSA = __cov_SIkTguWUcKOrc5jdRLQPSA['/home/shannon/example.js'];                                                                                                           
export default function(){__cov_SIkTguWUcKOrc5jdRLQPSA.f['1']++;__cov_SIkTguWUcKOrc5jdRLQPSA.s['1']++;return'foo';}();  

I think it's just missing parenthesis around the exported function.