-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
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
prefer-destructuring should not check non-numeric indexes #9784
prefer-destructuring should not check non-numeric indexes #9784
Comments
You could do |
Hi @aparajita, thanks for the issue. I agree with @j-f1 that the rule is currently working as designed but that we could consider an enhancement to avoid warning when a computed property access is being used. Would you mind editing your original post to match our Rule Change Proposal template so we can evaluate the proposed enhancement more easily? Thanks! |
Hi @aparajita, thanks for the issue. It looks like there's not enough information for us to know how to help you. Requesting a rule change? Please see Proposing a Rule Change for instructions. If it's something else, please just provide as much additional information as possible. Thanks! (edited by @platinumazure) |
@j-f1 That is invalid syntax; you're trying to declare and reference const words = { one: 1, two: 2 };
let num = 1;
let { [num]: num } = words; Run that, you get: SyntaxError: Identifier 'num' has already been declared Remove ReferenceError: num is not defined |
The correct way to do this is ({ [num]: num} = INTEGER_WORDS); |
@not-an-aardvark Correct, but looking at it I still can't decipher what's happening without seriously scratching my head, and I've written Javascript parsers and compilers. So under normal circumstances I think |
Oh, I see what's going on. The issue is that the The current behavior is intended to report something like this: num = foo.num;
// should be replaced with
({ num } = foo); But it should not report an issue when the right-hand side is |
@not-an-aardvark That explains it. |
The fix seems to be quite easy. I haven't thought through it deeply, but this works: Change these lines to: if (!rightNode.computed && ((property.type === "Literal" && leftNode.name === property.value) ||
(property.type === "Identifier" && leftNode.name === property.name))) { I added the check for |
Tell us about your environment
What parser (default, Babel-ESLint, etc.) are you using? default
Please show your full configuration:
Configuration
What rule do you want to change? prefer-destructuring
Does this change cause the rule to produce more or fewer warnings? Fewer
How will the change be implemented? (New option, new default behavior, etc.)? New default behavior
Please provide some example code that this change will affect:
What does the rule currently do for this code?
Generates a warning for
num = INTEGER_WORDS[num];
What will the rule do after it's changed?
It will ignore array indexes/keys that are not constants. I would argue this should always be the case, as it must be considered bad practice to destructure from a computed index or key.
The text was updated successfully, but these errors were encountered: