Skip to content
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

Closed
wants to merge 16 commits into from

Conversation

sviat9440
Copy link
Contributor

@sviat9440 sviat9440 commented Feb 2, 2023

PR Checklist

Progress

  • Add type checks for Array, String, Uint8Array ...
  • Add ignoreFunctions option (default to false)
  • Add support for -2, -3 ... -i
  • Clear tests
  • Add tests for this reference
  • Add tests for functions
  • Add more examples for documentation
  • Fix coverage
  • Add tests for custom String, Array ...

@typescript-eslint
Copy link
Contributor

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.

@nx-cloud
Copy link

nx-cloud bot commented Feb 2, 2023

☁️ Nx Cloud Report

CI is running/has finished running commands for commit ac0bbcf. As they complete they will appear below. Click to see the status, the terminal output, and the build insights.

📂 See all runs for this branch


✅ Successfully ran 46 targets

Sent with 💌 from NxCloud.

@netlify
Copy link

netlify bot commented Feb 2, 2023

Deploy Preview for typescript-eslint ready!

Name Link
🔨 Latest commit d0d0141
🔍 Latest deploy log https://app.netlify.com/sites/typescript-eslint/deploys/649d168976bef0000868d2bf
😎 Deploy Preview https://deploy-preview-6411--typescript-eslint.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@sviat9440 sviat9440 changed the title feat(eslint-plugin) [prefer-at] Create rule #6401 feat(eslint-plugin): [prefer-at] Create rule Feb 2, 2023
@sviat9440 sviat9440 changed the title feat(eslint-plugin): [prefer-at] Create rule feat(eslint-plugin): [prefer-at] create rule Feb 2, 2023
@sviat9440 sviat9440 marked this pull request as draft February 2, 2023 20:59
@codecov
Copy link

codecov bot commented Mar 1, 2023

Codecov Report

Merging #6411 (d0d0141) into main (997783f) will increase coverage by 0.04%.
The diff coverage is 100.00%.

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              
Flag Coverage Δ
unittest 87.47% <100.00%> (+0.04%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
packages/eslint-plugin/src/rules/prefer-at.ts 100.00% <100.00%> (ø)

@sviat9440 sviat9440 marked this pull request as ready for review March 3, 2023 10:05
@bradzacher bradzacher added the enhancement: new plugin rule New rule request for eslint-plugin label Mar 13, 2023
Copy link
Member

@JoshuaKGoldberg JoshuaKGoldberg left a 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. 🔍

packages/eslint-plugin/src/rules/prefer-at.ts Show resolved Hide resolved
packages/eslint-plugin/tests/rules/prefer-at.test.ts Outdated Show resolved Hide resolved
packages/eslint-plugin/src/rules/prefer-at.ts Outdated Show resolved Hide resolved
packages/eslint-plugin/src/rules/prefer-at.ts Outdated Show resolved Hide resolved
packages/eslint-plugin/src/rules/prefer-at.ts Outdated Show resolved Hide resolved
packages/eslint-plugin/docs/rules/prefer-at.md Outdated Show resolved Hide resolved
packages/eslint-plugin/docs/rules/prefer-at.md Outdated Show resolved Hide resolved
packages/eslint-plugin/docs/rules/prefer-at.md Outdated Show resolved Hide resolved
packages/eslint-plugin/docs/rules/prefer-at.md Outdated Show resolved Hide resolved
packages/eslint-plugin/src/rules/prefer-at.ts Outdated Show resolved Hide resolved
@JoshuaKGoldberg JoshuaKGoldberg added the awaiting response Issues waiting for a reply from the OP or another party label Mar 13, 2023
@JoshuaKGoldberg
Copy link
Member

👋 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.

@sviat9440
Copy link
Contributor Author

Hey, I'll do it next weekend

Co-authored-by: Josh Goldberg ✨ <git@joshuakgoldberg.com>
Sviatoslav Zaytsev added 3 commits April 16, 2023 12:15
- remove throw UnknownNodeError
- add using isTypeFlagSet utility
- getTypeAtLocation cannot return undefined
- the verification of the .at method has been removed
- getFullName now returns the source code range
@sviat9440
Copy link
Contributor Author

@JoshuaKGoldberg fixed

Copy link
Member

@JoshuaKGoldberg JoshuaKGoldberg left a 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') {
Copy link
Member

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})`),
Copy link
Member

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.

packages/eslint-plugin/src/rules/prefer-at.ts Show resolved Hide resolved
packages/eslint-plugin/src/rules/prefer-at.ts Show resolved Hide resolved
sviat9440 and others added 2 commits June 29, 2023 06:43
- fix coverage
- add some exotic test cases
- refactor isSupportedObject method
@sviat9440
Copy link
Contributor Author

@JoshuaKGoldberg waiting for your reply

@sviat9440
Copy link
Contributor Author

@JoshuaKGoldberg Are you kidding? I asked you a question in this thread. I will close the PR if there is no response from you.

@sviat9440
Copy link
Contributor Author

Screenshot_20230717_224129_Yandex Start.jpg

@JoshuaKGoldberg
Copy link
Member

JoshuaKGoldberg commented Jul 17, 2023

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.

screenshot of the same thread but with only my comments visible

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.

Copy link
Contributor Author

@sviat9440 sviat9440 left a 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.

packages/eslint-plugin/src/rules/prefer-at.ts Outdated Show resolved Hide resolved
function isExpectedExpressionLeft(
node: TSESTree.BinaryExpression,
): boolean {
if (!isExpectedObject(node.left) || getName(node.left) !== 'length') {
Copy link
Contributor Author

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') {
Copy link
Contributor Author

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??

packages/eslint-plugin/src/rules/prefer-at.ts Outdated Show resolved Hide resolved
packages/eslint-plugin/src/rules/prefer-at.ts Show resolved Hide resolved
packages/eslint-plugin/src/rules/prefer-at.ts Show resolved Hide resolved
Copy link
Member

@JoshuaKGoldberg JoshuaKGoldberg left a 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)) {
Copy link
Member

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 =>
Copy link
Member

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') {
Copy link
Member

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') {
Copy link
Member

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)

@JoshuaKGoldberg
Copy link
Member

👋 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.

@JoshuaKGoldberg JoshuaKGoldberg added the stale PRs or Issues that are at risk of being or have been closed due to inactivity for a prolonged period label Oct 17, 2023
@JoshuaKGoldberg
Copy link
Member

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! 😊

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 21, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
awaiting response Issues waiting for a reply from the OP or another party enhancement: new plugin rule New rule request for eslint-plugin stale PRs or Issues that are at risk of being or have been closed due to inactivity for a prolonged period
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants