istanbuljs / babel-plugin-istanbul

A babel plugin that adds istanbul instrumentation to ES6 code
BSD 3-Clause "New" or "Revised" License
616 stars 72 forks source link

Destructuring default function argument is instrumented incorrectly #263

Open rabelloo opened 2 years ago

rabelloo commented 2 years ago

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"]
}