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
Docs: Add prefer-destructuring examples (fixes #9970) #9989
Conversation
Add examples for the `prefer-destructuring` rules when `enforceForRenamedProperties` is enabled.
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.
Thanks for the PR!
I think both of these samples are actually specific to the object
option, not the array
option. (ESLint doesn't know whether your variable actually contains an array -- it just sees a dynamic property access like foo[bar]
.)
I'm confused. In #9970, you mentioned that for accessing a specific index of an Array with a variable, when |
Syntactically, your property access from #9970 is the same as: const array = { [someIndex]: 3, foo: 1, bar: 2 }; // an object with a `someIndex` key
const { [someIndex]: value } = array; This is considered "object destructuring" because it's a syntax used for object property access, regardless of whether the value of the |
With the config var foo = array[someIndex]; And the following is considered an error: var foo = array[0]; So it seems the rule for But with the config var foo = array[someIndex]; So it seems that with Is your explanation in #9970 a way to workaround the |
The I think the confusion might be coming from the fact that both the |
Sorry for the late answer. After doing a bit more test, I found out that with the following config: {
"prefer-destructuring": ["error", {"array": false, "object": true}, {"enforceForRenamedProperties": true}]
} I get the error var array = [1,2,3];
var someIndex = 1;
var foo = array[someIndex]; I think this is the problematic (or at least confusing part). When the rule is configured for reporting errors related to You mentioned:
But In addition shouldn't the For example: var array = [1,2,3];
var someIndex = 1;
var foo = array[someIndex]; // => Apply the array rule as array is an Array
var obj = {a: 'value', b: 'value'};
var someProp = 'a';
var foo = obj[someProp]; => Apply the object rule as obj is an Object If it's not possible to determine if an Object is an Array or not via the AST, and we have to rely on the type of What's even more confusing is that this behavior happens only when |
To be more precise, For example, it wouldn't be possible to use array destructuring in this code: function accessIndex(array, index) {
const element = array[index];
}
Hopefully, this example shows that array destructuring can only be used when the index is a known constant at development time. You could use I think the names "array destructuring" might be leading to some confusion. The term describes the syntax used to destructure a value (specifically the syntax that uses |
Understood. It seems really difficult to handle this case as it's not possible to know what is the type of the variable The fact the rule offer the options The current implementation considers the obj.prop; //Consider the object option => Make sense
obj[variable]; //Consider the object option => Make sense
array[1] //Consider the array option => Make sense
array[variable]; //Consider the object option => Confusing Maybe the I'm not sure how to improve the documentation in the current situation. Adding example as I did in this PR is incorrect and don't make things less confusing. It seems the following statements from the doc are inaccurate/misleading:
|
@not-an-aardvark @pvdlg Where are we at on this? Is this change still something we want to pursue? |
I think the current state is that the rule's behavior is causing some confusion which could be improved by adding something to the documentation, but this PR currently isn't what's needed. I'm not sure it would be a good idea to rename the |
Yes, I guess this PR can be closed. The way the rule works is confusing and I don't think that can be resolved by updating example as I initially thought. |
Closing per this comment. @pvdlg Thank you for the great discussion, and please do feel free to create an issue or submit another PR if you have more ideas on how to improve the documentation. We greatly appreciate all contributions! |
What is the purpose of this pull request? (put an "X" next to item)
[x] Documentation update
[ ] Bug fix (template)
[ ] New rule (template)
[ ] Changes an existing rule (template)
[ ] Add autofixing to a rule
[ ] Add a CLI option
[ ] Add something to the core
[ ] Other, please explain:
What changes did you make? (Give an overview)
Add examples for the
prefer-destructuring
rules whenenforceForRenamedProperties
is enabled.Is there anything you'd like reviewers to focus on?
Nothing in particular.