Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
fix(eslint-plugin): [no-unnecessary-type-assertion] fix false negative for const variable declarations#8558
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
Thanks for the PR,@abrahamguo! typescript-eslint is a 100% community driven project, and we are incredibly grateful that you are contributing to that community. The core maintainers work on this in their personal time, so please understand that it may not be possible for them to review your work immediately. Thanks again! 🙏Please, if you or your company is finding typescript-eslint valuable, help us sustain the project by sponsoring it transparently onhttps://opencollective.com/typescript-eslint. |
netlifybot commentedFeb 27, 2024 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
✅ Deploy Preview fortypescript-eslint ready!
To edit notification comments on pull requests, go to yourNetlify site configuration. |
nx-cloudbot commentedFeb 27, 2024 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
codecovbot commentedFeb 27, 2024 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@## main #8558 +/- ##==========================================- Coverage 88.10% 88.09% -0.02%========================================== Files 402 402 Lines 14029 14015 -14 Branches 4117 4112 -5 ==========================================- Hits 12360 12346 -14 Misses 1370 1370 Partials 299 299
Flags with carried forward coverage won't be shown.Click here to find out more.
|
Uh oh!
There was an error while loading.Please reload this page.
bradzacher left a comment• edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
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.
See the end of this comment#4485 (comment) for details of the changes we would accept to the rule.
Furthermore, the TSLint PR above also introduced some special handling for tuple types, which appears to be unnecessary, so it has been removed.
playground is this not the case that this assertion is required?
Or are you saying that the logic isn't required because it's never hit?
Uh oh!
There was an error while loading.Please reload this page.
abrahamguo commentedFeb 27, 2024 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
@bradzacher this assertion is indeed required; however, no special handling is required for it (or, for any tuple types, I believe) in this lint rule because |
Alrighty — I still need to clean up the tests a little bit, but I've reviewed (and added tests for):
and I am confident that my logic is correct for detecting useful vs useless literal type assertions:
Things to investigate for the future:
|
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.
Overall looks good to me! Thanks for working on this! 🚀
Just requesting splitting the test cases
function isConstVariableDeclaration(node: TSESTree.Node): boolean { | ||
return ( | ||
node.type === AST_NODE_TYPES.VariableDeclaration && | ||
node.kind === 'const' | ||
); | ||
} | ||
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.
[non-actionable] Hmmm, looks like these cases are not reported (playground)
leta:unknownconstb=(a=1as1)constc=(console.log(1),1as1)
However, I'm not sure we can easily detect all such cases. This function might become too complex...
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.
Yeah I think we can leave these for now. If someone files an issue showing them being buggy in isolation, then that'd indicate there's enough user appetite to start thinking about whether the complexity is worth it.
packages/eslint-plugin/tests/rules/no-unnecessary-type-assertion.test.ts OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
packages/eslint-plugin/tests/rules/no-unnecessary-type-assertion.test.ts OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
no-unnecessary-type-assertion
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.
Getting closer! 🙌
I think the main blocker in my mind is updating correct example in the docs
Uh oh!
There was an error while loading.Please reload this page.
packages/eslint-plugin/tests/rules/no-unnecessary-type-assertion.test.ts OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
…o-unnecessary-type-assertion
Co-authored-by: auvred <61150013+auvred@users.noreply.github.com>
…/typescript-eslint into no-unnecessary-type-assertion
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.
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.
🙌 swell, thanks!
Uh oh!
There was an error while loading.Please reload this page.
PR Checklist
Overview
The
no-unnecessary-type-assertions
rule needs a bit more detail about when type assertions are truly useful or useless on literals.