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
feat: add no-useless-assignment
rule
#17625
Conversation
✅ Deploy Preview for docs-eslint ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
Co-authored-by: Nicholas C. Zakas <nicholas@humanwhocodes.com>
Hmm. I don't understand why it crashes in CI. I can't reproduce it well locally. If you know the cause, could you please advise? |
The problem is fixed in dependencies now (#17664 (comment), fixed by humanwhocodes/object-schema@6ca80b0). |
lib/rules/no-useless-assignment.js
Outdated
// It is unused variable | ||
return; |
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 is to avoid reporting on variables that are reported by no-unused-vars
?
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 thought it shouldn't be reported in the new rule because it's not an unnecessary assignment, it's clearly an unnecessary variable. And it is reported by no-unused-vars
.
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.
Can you add those details both as a comment here and in the documentation? I can see this being a bit confusing, but I agree with your rationale.
I tried enabling this rule in the eslint codebase (this branch) and it found 46 errors:
Most are initial assignments in declarations to default values like There are also some that look to me like false positives for this rule, where the value is used on the right side of a subsequent assignment. For example: Lines 375 to 377 in 994596b
|
This is another kind of false positive, I believe: eslint/lib/linter/config-comment-parser.js Lines 102 to 128 in 994596b
The error is reported on line 102, but if Edit: actually, this looks correct since |
204860a
to
96dd2ac
Compare
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.
Can you switch this to use FlatRuleTester
instead? We are going through and updating all rule tests to do so. It should look like this:
const RuleTester = require("../../../lib/rule-tester/flat-rule-tester");
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 changed the test case to use FlatRuleTester 👍
@mdjermanovic this is ready for you to check on your suggestions. |
Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>
Thank you for your suggestions and also for finding the false positives. I fixed them. |
Co-authored-by: Nicholas C. Zakas <nicholas@humanwhocodes.com>
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. Leaving open for @mdjermanovic to review.
LGTM, I'll just close-reopen to check CI before merging. |
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.
Great work, thanks!
Given the complexity of the problem and JS syntax, I wouldn't be surprised if some edge cases pop up in production, but can't see any at the moment.
Sorry for the delay in reviewing, there have been many tasks for the v9 release lately.
* main: chore: Split Docs CI from core CI (eslint#17897) fix: Ensure config keys are printed for config errors (eslint#17980) chore: delete relative-module-resolver.js (eslint#17981) docs: migration guide entry for `no-inner-declarations` (eslint#17977) docs: Update README feat: maintain latest ecma version in ESLint (eslint#17958) feat!: no-inner-declaration new default behaviour and option (eslint#17885) feat: add `no-useless-assignment` rule (eslint#17625) fix: `no-misleading-character-class` edge cases with granular errors (eslint#17970) feat: `no-misleading-character-class` granular errors (eslint#17515) docs: fix number of code-path events on custom rules page (eslint#17969) docs: reorder entries in v9 migration guide (eslint#17967) fix!: handle `--output-file` for empty output when saving to disk (eslint#17957)
Prerequisites checklist
What is the purpose of this pull request? (put an "X" next to an item)
[ ] Documentation update
[ ] Bug fix (template)
[X] New rule (template)
[ ] Changes an existing rule (template)
[ ] Add autofix to a rule
[ ] Add a CLI option
[ ] Add something to the core
[ ] Other, please explain:
What changes did you make? (Give an overview)
This PR adds a new
no-useless-assignment
rule which warns if a variable is not used after being assigned a value.close #17559
Is there anything you'd like reviewers to focus on?
This PR is still a draft as many test cases are missing.