Skip to content

feat(eslint-plugin): [prefer-for-of] support multiple variables #1617

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

Closed
wants to merge 6 commits into from

Conversation

PSeitz
Copy link

@PSeitz PSeitz commented Feb 18, 2020

support multiple variables if second var is only assigning array length

@typescript-eslint
Copy link
Contributor

Thanks for the PR, @PSeitz!

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. As a thank you, your profile/company logo will be added to our main README which receives thousands of unique visitors per day.

@bradzacher bradzacher added the enhancement: plugin rule option New rule option for an existing eslint-plugin rule label Feb 18, 2020
@PSeitz
Copy link
Author

PSeitz commented Feb 19, 2020

I added more tests for better coverage, but the voter is not updated? Trying to close and open

@PSeitz PSeitz closed this Feb 19, 2020
@PSeitz PSeitz reopened this Feb 19, 2020
@PSeitz
Copy link
Author

PSeitz commented Feb 20, 2020

so I use the check in 252 with return; as a cast on the type and this line is expected to never hit

          if (
            !indexPosVar ||
            !isZeroInitialized(indexPosVar) ||
            indexPosVar.id.type !== AST_NODE_TYPES.Identifier ||
            arraLengthVar.id.type !== AST_NODE_TYPES.Identifier
          ) {
            return;
          }

@codecov
Copy link

codecov bot commented Mar 2, 2020

Codecov Report

Merging #1617 into master will decrease coverage by 0.26%.
The diff coverage is 79.38%.

@@            Coverage Diff             @@
##           master    #1617      +/-   ##
==========================================
- Coverage   95.20%   94.94%   -0.27%     
==========================================
  Files         148      158      +10     
  Lines        6989     7100     +111     
  Branches     2017     2034      +17     
==========================================
+ Hits         6654     6741      +87     
- Misses        124      155      +31     
+ Partials      211      204       -7     
Flag Coverage Δ
#unittest 94.94% <79.38%> (-0.27%) ⬇️
Impacted Files Coverage Δ
...es/eslint-plugin/src/rules/no-array-constructor.ts 100.00% <ø> (ø)
...s/eslint-plugin/src/rules/prefer-optional-chain.ts 95.09% <ø> (-0.05%) ⬇️
...nt-plugin/src/rules/switch-exhaustiveness-check.ts 100.00% <ø> (ø)
...perimental-utils/src/ts-eslint-scope/Definition.ts 100.00% <ø> (ø)
...mental-utils/src/ts-eslint-scope/PatternVisitor.ts 100.00% <ø> (ø)
...xperimental-utils/src/ts-eslint-scope/Reference.ts 100.00% <ø> (ø)
...perimental-utils/src/ts-eslint-scope/Referencer.ts 100.00% <ø> (ø)
...es/experimental-utils/src/ts-eslint-scope/Scope.ts 100.00% <ø> (ø)
...rimental-utils/src/ts-eslint-scope/ScopeManager.ts 100.00% <ø> (ø)
...experimental-utils/src/ts-eslint-scope/Variable.ts 100.00% <ø> (ø)
... and 38 more

@PSeitz
Copy link
Author

PSeitz commented Mar 6, 2020

@bradzacher should I also create an issue for this enhancement?

@bradzacher
Copy link
Member

Could you please explain the motivation behind this change?
why do you assign the length as a loop variable when it's not being incremented?

The examples you provided don't shed much light on what the programming pattern is exactly. Do you have some real world code which might illustrate the intention?

My concern is that this is use case is very, very rare, and in contrast the code required to support it makes it harder to maintain.

@bradzacher bradzacher added the awaiting response Issues waiting for a reply from the OP or another party label Apr 3, 2020
@PSeitz
Copy link
Author

PSeitz commented Apr 3, 2020

@bradzacher

why do you assign the length as a loop variable when it's not being incremented?

I don't think it makes sense, but I see this quite often in a codebase I'm working on. I think it's bad style, since it impairs readability. I guess some people do this for performance reasons. In this case the developers are coming from java.

http://allthingsjavascript.com/blog/index.php/2016/11/16/caching-the-length-property-in-a-for-loop/

https://stackoverflow.com/questions/5752906/is-reading-the-length-property-of-an-array-really-that-expensive-an-operation

Assuming the ts ast is rather stable, I don't think its hard to maintain, because the implementation is disjunct from the original, so you can imo just leave this case as is and not integrate changes.

@bradzacher
Copy link
Member

I don't think its hard to maintain, because the implementation is disjunct from the original, so you can imo just leave this case as is and not integrate changes

Sorry, when I say maintenance, I don't just mean "keeping the code running as we upgrade versions", I also mean enhancing it with new features, and fixing bugs.

This PR adds ~100 lines of code, which adds ~50% to the rule, which is non-trivial. This adds maintenance overhead in two ways:

  • more code is harder to understand for contributors, which raises the bar for contributions.
  • if someone wants to add a new feature they will have to understand this code to decide if it needs to be enhanced as well.
    • if they do need to enhance it, that's more cases to think about.
  • more code is more branches that need to be tested with those changes.

With all the above, if someone else wants to make an update or fix to the rule, then they can easily be scared off by the complexity. We already have too few contributors, so we don't want to scare anyone off.

Additionally, if no contributors want to fix bugs, then it falls on the core maintainers to fix bugs (which likely won't happen as we are pretty overloaded as is).

All of this is why I'm careful to validate that a change provide more use beyond a single codebase.

@PSeitz
Copy link
Author

PSeitz commented Apr 6, 2020

@bradzacher Yes, it's hard to understand. I don't think both cases can be merged into one block. I could offer to add comments explaining all the steps. As for the tests, I think the new code is quite well covered.

@bradzacher bradzacher removed the awaiting response Issues waiting for a reply from the OP or another party label May 17, 2020
@bradzacher bradzacher added the awaiting response Issues waiting for a reply from the OP or another party label Oct 25, 2020
@bradzacher
Copy link
Member

Closing as there isn't any drive from the community for such a feature, so IMO it's not worth the additional maintenance overhead.

Thank you for your contribution!

@bradzacher bradzacher closed this Jul 6, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 6, 2021
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: plugin rule option New rule option for an existing eslint-plugin rule
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants