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-destructuring-in-parameters rule #1045

Closed

Conversation

fisker
Copy link
Collaborator

@fisker fisker commented Jan 21, 2021

Questions:

  1. Should we allow rename, maybe under an option
- const foo = (bar, x) => bar.x === x
+ const foo = ({x: xOfBar}, x) => xOfBar === x
  1. Not sure if we should only allow 0/1 or just make sure the fixed code not against no-unreadable-array-destructuring rule
- const foo = bar => bar[1] + bar[2]
+ const foo = ([, second, third] => second + third)
  1. Should we add a limitation on numbers of properties

See https://github.com/sindresorhus/eslint-plugin-unicorn/pull/1045/files#diff-e9f53b43e2668561c178de9cab0e6745a034fee9e0c5321f93c83750b6c73553R740, this case creates a lot of variables.


Did not fix codebase, since the diff is big, I want keep this PR small. But you can check changes 9887f56

Fixes #954

@fisker fisker force-pushed the prefer-destructuring-in-parameters branch from b85bdb0 to b9f5126 Compare January 21, 2021 04:32
@fisker fisker marked this pull request as ready for review January 21, 2021 11:14
@fisker
Copy link
Collaborator Author

fisker commented Jan 22, 2021

Another question, currently

- const foo = (bar, baz) => bar.x === baz.x;
+ const foo = ({x}, baz) => x === baz.x;

This is a little a awkward, baz.x can't be destructed, because we created a new variable x, maybe we want ignore this case "accessing same property on different parameter".

But if we decided allow rename, this won't be a problem, we can fix to

- const foo = (bar, baz) => bar.x === baz.x;
+ const foo = ({x: xOfBar}, {x: xOfBaz}) => xOfBar === xOfBaz;

@sindresorhus
Copy link
Owner

I think this rule should have an option to only enforce it for inline arrow functions. I find it a bit opinionated to do it for all arrow functions. Constructuring many times leads to less readable code when applied to large functions.

@sindresorhus
Copy link
Owner

Should we allow rename, maybe under an option

Are you asking whether we should trigger on the case where it needs to be renamed or are you asking whether we should allow renaming at all?

Not sure if we should only allow 0/1 or just make sure the fixed code not against

Yes, just make sure the fix follows what we enforce in no-unreadable-array-destructuring.

Should we add a limitation on numbers of properties

Yes, but not sure what number makes sense. Should it be configurable?

Base automatically changed from master to main January 23, 2021 11:04
yakov116 added a commit to refined-github/refined-github that referenced this pull request Feb 14, 2021
@fregante
Copy link
Collaborator

fregante commented Feb 16, 2021

Should we add a limitation on numbers of properties

When I decide whether to destructure or just use the object, I also think of:

  • which one is shorter

    this is fine:

    - element => element.id
    + ({id}) => id

    this not so much:

    - user => user.isHomeDirectoryOpen
    + ({isHomeDirectoryOpen}) => isHomeDirectoryOpen
  • does the property make sense on its own?

    event.name makes perfect sense on its own, while name… name of what?

While it's a nice rule, I'm not sure that it can consistently improve the readability. More like 50/50

@sindresorhus
Copy link
Owner

@fisker I'm not sure about this rule. It seems to make code more unreadable in many cases. Maybe many of the cases could be detected, but not sure all can. (#1046)

@sindresorhus
Copy link
Owner

Closing as this has been stale for 2 years.

@fisker fisker deleted the prefer-destructuring-in-parameters branch October 29, 2023 18:49
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-destructuring-in-parameters
4 participants