babel-plugin-istanbul icon indicating copy to clipboard operation
babel-plugin-istanbul copied to clipboard

Destructuring default function argument is instrumented incorrectly

Open rabelloo opened this issue 3 years ago • 0 comments

Given this example source code:

const fn = ({ prop } = {}) => prop;

Istanbul instruments the code in a way that executing the function always breaks with error TypeError: Cannot destructure 'undefined' as it is undefined., for example:

// format is as emitted by babel-plugin-istanbul
const fn = ({ prop  } = cov_3lpv0ilsj().b[0][0]++, {
})=>{
    cov_3lpv0ilsj().f[3]++;
    return prop;
};

The issue is clearly the default statement, which does not get properly wrapped in parens, for example this would work:

const fn = ({ prop } = (cov_3lpv0ilsj().b[0][0]++, {}))=> prop;

Also, if it helps, default values for properties in the destructure expression work just fine:

const fn = ({ prop = 'foo' }) => prop;

// becomes

const fn = ({ prop = (cov_3lpv0ilsj().b[0][0]++, 'foo') }) => prop;

Environment

@babel/core: 7.14.8
babel-plugin-istanbul: 6.0.0
node: v15.12.0

Config

.babelrc

{
  "plugins": ["istanbul"]
}

rabelloo avatar Jul 28 '21 13:07 rabelloo