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 rule to avoid destructuring in params #2

Open
lxcid opened this issue May 4, 2020 · 0 comments
Open

Add rule to avoid destructuring in params #2

lxcid opened this issue May 4, 2020 · 0 comments

Comments

@lxcid
Copy link
Collaborator

lxcid commented May 4, 2020

I personally think this rule is prone to error.

When we do destructuring params, it usually meant for options or props.

And we sometimes also provide default params to them, especially options: e.g.

function foo({ bar = true } = {}) {
}

This mean that if the first argument to foo is undefined, assign it to {} and we then destructure the attribute bar which if undefined, it defaults to true.

First, there is way too much logic going on for just something as simple. Secondly, null will break the assumption pretty badly. e.g. if we pass null as first argument, it will crash. or if we pass null to bar, it will not default to true and take on null value. Although such cases can be now caught in the TypeScript land easily.

I would have prefer the following code

function foo(options) {
  const bar = options?.foo ?? true;
}

This will cover all possible null and undefined cases as mentioned above. much more clear and easily to follow with no special object created implicitly thus actually more performant.

Copy in the rule from here…

https://github.com/lukeapage/eslint-plugin-destructuring

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

No branches or pull requests

1 participant