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(eslint-plugin): [prefer-at] create rule #6411
Conversation
Thanks for the PR, @sviat9440! 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. |
…ny number or variable
…ate statements, clear tests
Codecov Report
Additional details and impacted files@@ Coverage Diff @@
## main #6411 +/- ##
==========================================
+ Coverage 87.43% 87.47% +0.04%
==========================================
Files 386 387 +1
Lines 13194 13240 +46
Branches 3872 3882 +10
==========================================
+ Hits 11536 11582 +46
Misses 1292 1292
Partials 366 366
Flags with carried forward coverage won't be shown. Click here to find out 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.
A nice start, thank you for sending it in!
I gave it a once-over but it's hard to review more thoroughly until the unit tests are more readable. Once they're good to go I can take a deeper look. 🔍
👋 Hey @sviat9440! Just checking in, is this still something you have time for? No worries if not - I just don't want to leave it hanging. |
Hey, I'll do it next weekend |
Co-authored-by: Josh Goldberg ✨ <git@joshuakgoldberg.com>
- remove throw UnknownNodeError - add using isTypeFlagSet utility - getTypeAtLocation cannot return undefined - the verification of the .at method has been removed
- simplify tests
- getFullName now returns the source code range
@JoshuaKGoldberg fixed |
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 updates! There's still a thread from before - waiting on hearing back on that one before going deeper :).
function isExpectedExpressionLeft( | ||
node: TSESTree.BinaryExpression, | ||
): boolean { | ||
if (!isExpectedObject(node.left) || getName(node.left) !== 'length') { |
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.
Bump for this unresolved thread @sviat9440 ?
}, | ||
node, | ||
fix: fixer => | ||
fixer.replaceText(node, `${objectName}.at(-${rightName})`), |
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.
[Question] Similar lint rules such as prefer-regexp-exec
and prefer-string-starts-ends-with
use a util called getWrappingFixer
. Have you considered using it here? There are some edge cases in their tests I think we'd want for this one.
- fix coverage - add some exotic test cases - refactor isSupportedObject method
@JoshuaKGoldberg waiting for your reply |
@JoshuaKGoldberg Are you kidding? I asked you a question in this thread. I will close the PR if there is no response from you. |
Aha! I see what the issue is - you posted the comments as Pending, in a review that hasn't been posted. So I hadn't seen them yet. Love it. I was just talking with someone about how this can be easy to do. I'll take a look soon. Thanks for posting the screenshot! If there are other comments it'd be great if you could submit the review so I can see them. |
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.
Oh, I have 6 pending comments... Interestingly, some comments are published immediately, and some are not. Strange. I will keep in mind in the future.
function isExpectedExpressionLeft( | ||
node: TSESTree.BinaryExpression, | ||
): boolean { | ||
if (!isExpectedObject(node.left) || getName(node.left) !== 'length') { |
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 give me an example where this utility will help me?
function isExpectedExpressionLeft( | ||
node: TSESTree.BinaryExpression, | ||
): boolean { | ||
if (!isExpectedObject(node.left) || getName(node.left) !== 'length') { |
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.
Bump: Can you give me an example where this utility will help me??
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.
👍 progress!
property: TSESTree.BinaryExpression & { operator: '-' }; | ||
}, | ||
): void { | ||
if (!isExpectedExpression(node.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.
[Naming] I'm finding hard to read the code with the naming convention of isExpected*
. It's a little vague - what does 'expected' mean? Until you fully understand the code's intent that can be hard to determine it.
What do you think about renaming them to more explicitly say what they do? E.g. instead of isExpectedExpression
, something like isArrayLengthBasedLookup
?
name: objectName, | ||
}, | ||
node, | ||
fix: fixer => |
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.
[Bug] Just remembered, .at
's signature returns T | undefined
. That's a change from the original type of T
from []
lookups. We generally put anything that can break the build into suggestions fixers rather than direct fix fixers. So this should be a suggestion fixer.
function isExpectedExpressionLeft( | ||
node: TSESTree.BinaryExpression, | ||
): boolean { | ||
if (!isExpectedObject(node.left) || getName(node.left) !== 'length') { |
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.
Behaviorally, it should help catch cases like this:
str[str['length'] - 1]
const key = 'length'; str[str[key] - 1]
For code, it would eliminate the need to manually write a function getName
. Which is something a lot of our rules end up wanting. So we'd really rather than write explicit "what is the string name of this property?" logic repeatedly without a real need.
https://eslint-community.github.io/eslint-utils/api/ast-utils.html#getstaticvalue
function isExpectedExpressionLeft( | ||
node: TSESTree.BinaryExpression, | ||
): boolean { | ||
if (!isExpectedObject(node.left) || getName(node.left) !== 'length') { |
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.
(also, for anybody seeing just this thread and wondering why we're bumping at each other 😂 the comments were pending)
👋 Hey @sviat9440! Just checking in, is this still something you have time for? No worries if not - I just don't want to leave it hanging. |
Closing this PR as it's been stale for a few weeks without activity. Feel free to reopen @sviat9440 if you have time - but no worries if not! If anybody wants to drive it forward, please do post your own PR - and if you use this as a start, consider adding co-author attribution at the end of your PR description. Thanks! 😊 |
PR Checklist
Progress
Array
,String
,Uint8Array
...ignoreFunctions
option (default tofalse
)this
referenceString
,Array
...