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
Update: update handling of destructuring in camelcase (fixes #8511) #9468
Update: update handling of destructuring in camelcase (fixes #8511) #9468
Conversation
LGTM |
LGTM |
795b6d7
to
c905181
Compare
LGTM |
I'm 👍 for this change - definitely seems like something the rule should catch.
@vitorbal @platinumazure @not-an-aardvark Are you okay with closing #8529 in favor of this PR? |
c905181
to
b0c134e
Compare
LGTM |
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.
LGTM, thanks! Do we want to include any of the new test cases in the documentation?
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.
Do you mind updating the PR title following the guidelines outlined here? This is important for us because we generate our changelogs and calculate the semver version of the release based on the commit messages in the commit history. Thanks!
Marking as accepted since #8529 had already been accepted. |
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 changing the PR title!
Have one comment, but I'd also love to see a test around destructuring with renaming and default values. Example (taken from #8529 (comment)):
var { foo: bar_baz = 1 } = quz;
It might also be nice to include a test for when multiple values are being destructured, just to ensure that works correctly.
lib/rules/camelcase.js
Outdated
} else if (node.parent.type === "Property" || node.parent.type === "AssignmentPattern") { | ||
|
||
if (node.parent.parent.type === "ObjectPattern") { | ||
node.parent.parent.properties.forEach(property => { |
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.
This method should worry about a single Property
node at a time. I don't think we need to loop through all the properties here, as this method will already be called on each Identifier
. The current implementation will loop through all the destructured properties for each destructured assignment that exists, which seems like more work than the rule needs to do.
Requesting a review from @not-an-aardvark since you had concerns with the initial PR. |
I've been pretty busy lately, so I'll try to review this if I get the chance, but don't let me be a blocker if others are satisfied with the PR. |
b0c134e
to
a30e043
Compare
LGTM |
1 similar comment
LGTM |
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.
This LGTM, but I would like to briefly discuss if this actually belongs behind the properties
configuration option. I wonder if this should actually be on by default (as a semver-minor bug fix, which it technically already currently is) or if we should put it behind a destructuring
configuration option. @erindepew @eslint/eslint-team Thoughts?
Apologies for thinking of this so late in the process. Also don't want to hold this PR up over a long discussion, but since this is a change that creates more errors I'd like to not have to do it twice :)
TSC Summary: I'm bringing this to the TSC because of the complicated history of this PR - given the number of iterations and changes that have happened since the issue was originally accepted, I figured it would be easier to have the TSC decide. Some history: The original PR - that this now includes - was accepted as a bug fix, and during the course of the original PR author working on it, another separate (but related) bug was discovered. It sounds like there is consensus to fix this bug, however, I'm concerned about putting it behind the TSC Question: Should this be accepted as it currently is (behind the |
75f251a
to
b84be4c
Compare
fe35472
to
6b82e4a
Compare
lib/rules/camelcase.js
Outdated
return; | ||
if (node.parent.parent && node.parent.parent.type === "ObjectPattern") { | ||
|
||
if (node.parent.key === node && node.parent.value !== node) { |
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.
I think it might be helpful to have a short comment that explains what this is checking.
lib/rules/camelcase.js
Outdated
return; | ||
} | ||
|
||
if (node.parent.value.right && node.parent.value.right.type === "Identifier" && isUnderscored(node.parent.value.right.name)) { |
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.
I think it might be helpful to have a short comment that explains what this is checking.
lib/rules/camelcase.js
Outdated
report(node); | ||
} | ||
|
||
if (node.parent.value.name && isUnderscored(node.parent.value.name)) { |
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.
I think it might be helpful to have a short comment that explains what this is checking.
docs/rules/camelcase.md
Outdated
// ... | ||
}; | ||
|
||
function foo({ no_camelcased: my_default }) { |
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.
I wonder if this example would be clearer as something like:
function foo({ isCamelcased: no_camelcased }) {
docs/rules/camelcase.md
Outdated
@@ -60,6 +78,19 @@ var { category_id: category } = query; | |||
|
|||
### never | |||
|
|||
|
|||
Examples of **incorrect** code for this rule with the default `{ "properties": "never" }` option: |
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.
I don't think we need this or this change any more, since this behavior is the same as the cases listed above.
tests/lib/rules/camelcase.js
Outdated
@@ -79,6 +79,20 @@ ruleTester.run("camelcase", rule, { | |||
{ | |||
code: "import { no_camelcased as camelCased, anoterCamelCased } from \"external-module\";", | |||
parserOptions: { ecmaVersion: 6, sourceType: "module" } | |||
}, | |||
{ | |||
code: "const { no_camelcased = false } = bar;", |
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.
Shouldn't this be failing?
tests/lib/rules/camelcase.js
Outdated
parserOptions: { ecmaVersion: 6 } | ||
}, | ||
{ | ||
code: "function foo({ no_camelcased = 'default value' }) {};", |
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.
Shouldn't this be failing?
tests/lib/rules/camelcase.js
Outdated
}, | ||
{ | ||
code: "const { no_camelcased = false } = bar;", | ||
options: [{ properties: "never" }], |
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.
I don't think we need the properties
any more in most of these valid tests. Maybe one with properties: "always"
and one with properties: "never"
at the end of the test suite to ensure they don't affect the outcome?
tests/lib/rules/camelcase.js
Outdated
parserOptions: { ecmaVersion: 6 } | ||
}, | ||
{ | ||
code: "function foo({ no_camelcased: camelCased }) {};", |
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.
I think we're missing some of the test cases outlined in the docs.
tests/lib/rules/camelcase.js
Outdated
type: "Identifier" | ||
}, | ||
{ | ||
message: "Identifier 'no_camelcased' is not in camel case.", |
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.
I don't think we should be warning for the assignment of no_camelcased
. In other words, I don't think we should be checking the right hand side of destructured assignments with default values since they're only being referenced and not defined. The rule should warn on the declaration (which it correctly does).
33c053d
to
791d406
Compare
Looks like this has been updated. @eslint/eslint-team Can we get some other eyes on this? This is a semver-minor bug fix (it will result in more errors being reported by default). |
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.
Just one question about a comment, otherwise looks good to me. Thanks for contributing!
lib/rules/camelcase.js
Outdated
return; | ||
} | ||
|
||
if (isUnderscored(name) && !ALLOWED_PARENT_TYPES.has(effectiveParent.type)) { | ||
// check right hand side of assignment to prevent duplicate warnings |
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.
Should this say "Don't check right hand side of AssignmentExpression (etc.)"?
adding to docs, tests, and fixing logic
791d406
to
1a4e72f
Compare
@platinumazure you're right, updated the comment! and thanks @kaicataldo for holding my hand through the process :) |
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.
LGTM. Thanks for sticking with us on this one! I know it took a while with it being an inherited issue and the discussions that surrounded the implementation.
Thanks for contributing to ESLint! |
Should it mark Example:
|
@rodrigok Can you make a new issue for this? These kinds of comments don't get much visibility. Thanks! |
@kaicataldo Thanks, here is the issue #9709 |
@j-f1 Thanks, I'll reference and close my issue. |
What is the purpose of this pull request? (put an "X" next to item)
[ ] Documentation update
[x] 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)
Fixes the bug reported in #8511 . I forked from #8529 and added a check for the right-side of the value assignment
Is there anything you'd like reviewers to focus on?
Nope!