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

Add prefer-object-from-entries rule #1308

Merged
merged 26 commits into from
Jul 29, 2021

Conversation

fisker
Copy link
Collaborator

@fisker fisker commented May 21, 2021

Currently,

  1. Simple cases pairs.reduce(object => ({...object, key}), {}) and pairs.reduce(object => Object.assign(object, {key}), {}) are fixable
  2. Any Array#reduce() with inital value {} or Object.create(null) (except fixable cases above) are reported
  3. _.fromPairs() are reported, and a configurable functions list.

Question:

I'm not sure about the case mentioned in the proposal now,

const foo = {};
for (const [key, value] of pairs) {
	foo[key] = value;
}

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

@sindresorhus
Copy link
Owner

Funny thing, this rule self violate it, but I'm not sure I want use Object.fromEntries().

Why not? And would the rule even be able to handle such an advanced case anyway?

@sindresorhus
Copy link
Owner

I'm not sure about the case mentioned in the proposal now,

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.

@fisker
Copy link
Collaborator Author

fisker commented May 21, 2021

Why not?

Can't tell, personal feeling, get used to use for...of.

would the rule even be able to handle such an advanced case anyway?

The case is the simplest case, only one assignment 😄 , just a little long.

@sindresorhus
Copy link
Owner

I tried rewriting it to using Object.fromEntries and it looks ok to me:

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];
}));

@fisker
Copy link
Collaborator Author

fisker commented May 21, 2021

Yes, if we decide to catch this, it will be fixed like this.

@fisker
Copy link
Collaborator Author

fisker commented May 21, 2021

But do you think it's better than for...of?

@fisker
Copy link
Collaborator Author

fisker commented May 21, 2021

Let's do this, only report cases don't need to do .map(), like the case in proposal, it's already [key, value]

// 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?

@sindresorhus
Copy link
Owner

WDYT?

👍

@fisker
Copy link
Collaborator Author

fisker commented May 21, 2021

Close for now, I'll reopen when it's ready.

@fisker fisker closed this May 21, 2021
@fisker fisker reopened this Jul 26, 2021
@fisker fisker force-pushed the prefer-object-from-entries branch from 1aacb10 to f68d0d9 Compare July 26, 2021 11:21
@fisker
Copy link
Collaborator Author

fisker commented Jul 26, 2021

@sindresorhus

After a long time thinking about the for...of case, I still can't find a good solution for it.
It's really complicated, I suggest we merge this (only checking .reduce()) first, and improve it in future?

@sindresorhus
Copy link
Owner

It's really complicated, I suggest we merge this (only checking .reduce()) first, and improve it in future?

👍 Can you open an issue about it?

@fisker fisker marked this pull request as ready for review July 27, 2021 01:45
@fisker
Copy link
Collaborator Author

fisker commented Jul 27, 2021

#1449


You can also check custom functions that transforms pairs.

`lodash.fromPairs()` and `_.fromPairs()` are checked by default.
Copy link
Owner

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.

Copy link
Collaborator Author

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.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

};
}

listeners[arrayReduceWithEmptyObject] = function (node) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
listeners[arrayReduceWithEmptyObject] = function (node) {
listeners[arrayReduceWithEmptyObject] = node => {

Same with the other ones.

@sindresorhus sindresorhus merged commit 4a14187 into sindresorhus:main Jul 29, 2021
@fisker fisker deleted the prefer-object-from-entries branch July 29, 2021 13:17
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 this pull request may close these issues.

Rule proposal: prefer-object-from-entries
2 participants