Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

browserlist.coverage fail with custom coverage file #607

Closed
danylkaaa opened this issue Jul 6, 2021 · 1 comment · Fixed by #608
Closed

browserlist.coverage fail with custom coverage file #607

danylkaaa opened this issue Jul 6, 2021 · 1 comment · Fixed by #608

Comments

@danylkaaa
Copy link
Contributor

Hello! I tried to use your snippet to get coverage when querying my custom stats.

// process.env.BROWSERSLIST_STATS === path.join(__dirname, './browserslist-stats.json');
const browsers = browserslist();
browserslist.coverage(browsers, 'my stats')

But I get this error

TypeError: Cannot read property 'op_mini' of undefined
    at normalizeStats (<...>/node_modules/browserslist/node.js:127:11)
    at Object.getStat (<...>/node_modules/browserslist/node.js:209:12)
    at Function.browserslist.coverage (<....>/node_modules/browserslist/index.js:580:27)

I found that it is caused by this 3f54ebd/index.js#L580 :

// node_modules/browserslist/index.js:580
var customStats = env.getStat(opts)

Which calls getStat() from browserslist/node.js

3f54ebd/node.js#L209

  getStat: function getStat (opts, data) {
    // some ops, but "data" argument is not overrided
    return normalizeStats(data, stats) // <- data is undefined
  }

And then browserlist tries to access the data members w/o checking that the data exists.

3f54ebd/node.js#L127

if (
      versions.length === 1 &&
      data[i] && // <- raises error
      data[i].versions.length === 1
    )

So, I considered to add extra check


function normalizeStats (data, stats) {
  if (!data) {
    data = {};
  }
  //...
}
@danylkaaa
Copy link
Contributor Author

Also I created PR #608 with my proposed changes

@ai ai closed this as completed in #608 Jul 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant