Skip to content

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

Conversation

reduckted
Copy link
Contributor

@reduckted reduckted commented Mar 10, 2025

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 allow i 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:

Copy link

nx-cloud bot commented Mar 10, 2025

View your CI Pipeline Execution ↗ for commit 7ec5652.

Command Status Duration Result
nx run-many -t test ✅ Succeeded 24s View ↗
nx run-many -t build ✅ Succeeded 11s View ↗
nx run-many -t e2e-suite --parallel 1 ✅ Succeeded 9m 28s View ↗
nx run-many -t test --codeCoverage ✅ Succeeded 1m 11s View ↗
nx run-many -t build,typecheck,check-rule-docs,... ✅ Succeeded 48s View ↗
nx-cloud record -- pnpm nx sync:check ✅ Succeeded 4s View ↗
nx-cloud record -- pnpm format-check ✅ Succeeded 5s View ↗

☁️ Nx Cloud last updated this comment at 2025-03-22 09:55:15 UTC

Copy link

codecov bot commented Mar 10, 2025

Codecov Report

Attention: Patch coverage is 94.13681% with 18 lines in your changes missing coverage. Please review.

Project coverage is 92.53%. Comparing base (1bd350b) to head (7ec5652).
Report is 5 commits behind head on main.

Files with missing lines Patch % Lines
...plate/src/rules/prefer-contextual-for-variables.ts 93.02% 11 Missing and 4 partials ⚠️
...t-plugin-template/src/utils/are-equivalent-asts.ts 95.38% 3 Missing ⚠️
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     
Flag Coverage Δ
unittest 92.53% <94.13%> (+0.17%) ⬆️

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

Files with missing lines Coverage Δ
packages/eslint-plugin-template/src/index.ts 100.00% <100.00%> (ø)
...sts/rules/prefer-contextual-for-variables/cases.ts 100.00% <100.00%> (ø)
...t-plugin-template/src/utils/are-equivalent-asts.ts 95.38% <95.38%> (ø)
...plate/src/rules/prefer-contextual-for-variables.ts 93.02% <93.02%> (ø)

... and 1 file with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

JamesHenry
JamesHenry previously approved these changes Mar 22, 2025
Copy link
Member

@JamesHenry JamesHenry left a 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!

@JamesHenry
Copy link
Member

I pulled in latest main but looks like it still needs a docs update

@reduckted reduckted marked this pull request as ready for review March 22, 2025 09:55
@JamesHenry JamesHenry merged commit 2ea4b0d into angular-eslint:main Mar 22, 2025
7 checks passed
@reduckted reduckted deleted the feature/prefer-contextual-for-variables branch March 22, 2025 10:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants