-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
fix(eslint-plugin): [no-unnecessary-type-assertion] fix false negative for const variable declarations #8558
fix(eslint-plugin): [no-unnecessary-type-assertion] fix false negative for const variable declarations #8558
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 on https://opencollective.com/typescript-eslint. |
✅ Deploy Preview for typescript-eslint ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
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.
|
packages/eslint-plugin/tests/rules/no-unnecessary-type-assertion.test.ts
Show resolved
Hide resolved
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?
@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)
let a: unknown
const b = (a = 1 as 1)
const c = (console.log(1), 1 as 1)
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
Outdated
Show resolved
Hide resolved
packages/eslint-plugin/tests/rules/no-unnecessary-type-assertion.test.ts
Outdated
Show resolved
Hide resolved
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
packages/eslint-plugin/tests/rules/no-unnecessary-type-assertion.test.ts
Outdated
Show resolved
Hide resolved
…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.
…o-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.
🙌 swell, thanks!
d2995df
into
typescript-eslint:main
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.