-
-
Notifications
You must be signed in to change notification settings - Fork 243
feat(eslint-plugin-template): add rule prefer-contextual-for-variables #2311
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-template): add rule prefer-contextual-for-variables #2311
Conversation
View your CI Pipeline Execution ↗ for commit 7ec5652.
☁️ Nx Cloud last updated this comment at |
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #2311 +/- ##
==========================================
+ Coverage 92.36% 92.53% +0.17%
==========================================
Files 181 184 +3
Lines 3377 3724 +347
Branches 689 835 +146
==========================================
+ Hits 3119 3446 +327
- Misses 200 215 +15
- Partials 58 63 +5
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
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.
Great work, this was a pretty involved rule but it's well explained and very well tested, thank you!
I pulled in latest main but looks like it still needs a docs update |
Closes #2203
When using the
@for
control flow statement, some contextual variables can be accessed within the block without aliasing them.See Contextual variables in @for blocks for details.
This new rule will report any contextual variables in
@for
statements that are aliased. An option can be provided for each contextual variable to allow specific aliases to be allowed. For example,{ allowedAliases: { $index: ['i'] } }
would allowi
to be used as an alias for$index
. The fixer will remove the alias and also rename any references to the aliased variables.The only scenario where aliases are kept is when there are nested
@for
blocks. This allows, for example, the outer$index
and inner$index
to be aliased to provide more meaningful names.The rule will also detect expressions where a different contextual variable can be used. For example
$index === 0
can be replaced with$first
,$index === $count - 1
can be replaced with$last
, etc. This also has a fixer.This depends on: