question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Possible issue with `prefer-object-from-entries`

See original GitHub issue

I just updated unicorn, and enabled prefer-object-from-entries. It reported this code as invalid:

const categories = stores
  .reduce((acc, val) => {
    const category = getCategory(val);
    if (acc[category])
      acc[category].push(val);
    else
      acc[category] = [val];
    return acc;
  }, {});

I’m not really sure, but I don’t think I can just replace this with Object.fromEntries, can I? If not, maybe it should try finding when we create a pair of k/v out of an object, and report only then? Or update the error message to tell us to use fromEntries and another Array method (map or whatever) to make our modifications? Because here it looks like it tells me to only use fromEntries, but I don’t see how I can 😅

EDIT: I just realised here, my stores is a Collection (a Map on steroid, and it has a reduce method). So maybe it’s just that the rule shouldn’t apply here and I have to disable it.

Unrelated

It would be great to enable Github Discussions, as I’m not sure this is a bug, so a Discussion would have been more appropriate IMO 😄

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5

github_iconTop GitHub Comments

4reactions
fregantecommented, Jan 15, 2022

You may be right on this one. I see an overlap with no-array-reduce and an impossible suggestion (because not all reduces can be turned into an Object.fromEntries)

Maybe the scope of the rule should be reduced.

2reactions
fiskercommented, Jan 17, 2022

Make sense to me, let’s remove those cases that we don’t know what’s in the callback, we have no-array-reduce anyway.

Read more comments on GitHub >

github_iconTop Results From Across the Web

eslint-plugin-unicorn/prefer-object-from-entries.md at main
When transforming a list of key-value pairs into an object, Object.fromEntries(…) should be preferred. This rule is fixable for simple cases. Fail. const ......
Read more >
JavaScript Object.fromEntries() | SamanthaMing.com
Object.fromEntries is the inverse of Object.entries. It will take key-value pairs and return a new object. You can use it on Arrays and...
Read more >
Restrain inferred type of Object.fromEntries - Stack Overflow
Unfortunately, the compiler cannot follow your logic at the type-level strongly enough to give you the types you're looking for.
Read more >
Issue 8021 in v8: Implement Object.fromEntries - Google Groups
Updates: Components: -Infrastructure Language Labels: -Type-Bug Priority-3 Type-FeatureRequest. Owner: ---- Status: Available Comment #1 on issue 8021 by ne ...
Read more >
8021 - Implement Object.fromEntries - v8 - Monorail
though if the copied code is preferred, I'm happy to revert to that. Item 2) is needed for Object.fromEntries(), as it's better to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found