-
-
Notifications
You must be signed in to change notification settings - Fork 353
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
Add prefer-object-from-entries
rule
#1308
Add prefer-object-from-entries
rule
#1308
Conversation
Why not? And would the rule even be able to handle such an advanced case anyway? |
I think it should handle simple cases like that, but maybe stop if the inner scope refers to variables outside it or some other heuristic. |
Can't tell, personal feeling, get used to use
The case is the simplest case, only one assignment 😄 , just a little long. |
I tried rewriting it to using const arrayReduce = new Map();
const listeners = Object.fromEntries(fixableArrayReduceCases.map(({selector, test, getKey, getValue}) => {
const value = function (node) {
// If this listener exit without adding fix, the `arrayReduceWithEmptyObject` listener
// should still add it into the `arrayReduce` map, to be safer, add it here too
arrayReduce.set(node);
const [callbackFunction] = node.arguments;
if (!test(callbackFunction)) {
return;
}
const [firstParameter] = callbackFunction.params;
const variables = context.getDeclaredVariables(callbackFunction);
const firstParameterVariable = variables.find(variable => variable.identifiers.length === 1 && variable.identifiers[0] === firstParameter);
if (!firstParameterVariable || firstParameterVariable.references.length !== 1) {
return;
}
arrayReduce.set(
node,
// The fix function
fixReduceAssignOrSpread({
sourceCode,
node,
key: getKey(callbackFunction),
value: getValue(callbackFunction)
})
);
};
return [selector, value];
})); |
Yes, if we decide to catch this, it will be fixed like this. |
But do you think it's better than |
Let's do this, only report cases don't need to do // Report this
const foo = {};
for (const [key, value] of pairs) {
foo[key] = value;
} // Not this
const foo = {};
for (const [key, value] of pairs) {
foo[key] = doSomething(value);
} WDYT? |
👍 |
Close for now, I'll reopen when it's ready. |
# Conflicts: # index.js # readme.md
1aacb10
to
f68d0d9
Compare
After a long time thinking about the |
👍 Can you open an issue about it? |
|
||
You can also check custom functions that transforms pairs. | ||
|
||
`lodash.fromPairs()` and `_.fromPairs()` are checked by default. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not clear from the docs here whether setting this option overrides the default checked functions or extends it. That should be made more clear.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about
lodash.fromPairs()
and_.fromPairs()
are always checked.
Note to myself, prefer-array-flat
use the same.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
rules/prefer-object-from-entries.js
Outdated
}; | ||
} | ||
|
||
listeners[arrayReduceWithEmptyObject] = function (node) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
listeners[arrayReduceWithEmptyObject] = function (node) { | |
listeners[arrayReduceWithEmptyObject] = node => { |
Same with the other ones.
…-plugin-unicorn into prefer-object-from-entries # Conflicts: # rules/prefer-object-from-entries.js
Currently,
pairs.reduce(object => ({...object, key}), {})
andpairs.reduce(object => Object.assign(object, {key}), {})
are fixableArray#reduce()
with inital value{}
orObject.create(null)
(except fixable cases above) are reported_.fromPairs()
are reported, and a configurablefunctions
list.Question:
I'm not sure about the case mentioned in the proposal now,
do we really want to check it?
Funny thing, this rule self violate it, but I'm not sure I want use
Object.fromEntries()
.Fixes #1260