Skip to content

refactor(migrate): auto derive variant_index() function#9803

Merged
dyc3 merged 1 commit intomainfrom
dyc3/derive-variant-index
Apr 7, 2026
Merged

refactor(migrate): auto derive variant_index() function#9803
dyc3 merged 1 commit intomainfrom
dyc3/derive-variant-index

Conversation

@dyc3
Copy link
Copy Markdown
Contributor

@dyc3 dyc3 commented Apr 5, 2026

Summary

A small refactor to change the RuleSource::variant_index() function automatically derived. The reason for this
is to guarentee that all variants will have a unique value. Sometimes, when we merge in new rule sources,
we accidently introduce a collision. This fixes that by adding a small proc macro to do this.

generated by gpt 5.4

Test Plan

CI should be green

Docs

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Apr 5, 2026

⚠️ No Changeset found

Latest commit: 7b9e7f2

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@github-actions github-actions Bot added the A-Linter Area: linter label Apr 5, 2026
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Apr 5, 2026

Merging this PR will not alter performance

✅ 58 untouched benchmarks
⏩ 196 skipped benchmarks1


Comparing dyc3/derive-variant-index (7b9e7f2) with main (1d09f0f)

Open in CodSpeed

Footnotes

  1. 196 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 5, 2026

Walkthrough

This change introduces a new procedural derive macro, RuleSourceVariantIndex, implemented in the biome_analyze_macros crate. The macro automatically generates a variant_index() method for enum types, mapping each variant to a zero-based u16 index. The RuleSource enum in rule.rs is updated to use this derive macro instead of its previous manual implementation, removing approximately 50 lines of match-based code whilst maintaining the same functionality for variant indexing and sorting.

🚥 Pre-merge checks | ✅ 2
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the main change: refactoring to auto-derive the variant_index() function instead of manually implementing it.
Description check ✅ Passed The description explains the motivation (preventing accidental variant collisions) and the solution (proc macro auto-derivation), relating directly to the changeset.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch dyc3/derive-variant-index

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (3)
crates/biome_analyze/src/rule.rs (1)

97-102: Please lock RuleSource ordering down with a regression test.

The proc-macro unit tests prove the generator, but they do not pin the actual RuleSource ordering that Ord and cmp_any() depend on. A tiny sort/comparison test here would catch accidental enum reshuffles before they reshuffle behaviour.

As per coding guidelines "All code changes MUST include appropriate tests".

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@crates/biome_analyze/src/rule.rs` around lines 97 - 102, Add a regression
test that pins the ordering of the RuleSource enum by asserting the expected
ordering behavior used by Ord and cmp_any(); specifically, write a unit test
that enumerates the RuleSource variants in their declared order and asserts
pairwise comparisons (e.g., VariantA < VariantB) and that sorting a vec of all
variants yields the declared order, and also verify cmp_any() produces the same
ordering decisions; place the test adjacent to the RuleSource definition (in the
same module's tests) and reference the RuleSource type and the cmp_any() helper
to make it obvious if the enum order changes.
crates/biome_analyze_macros/src/lib.rs (1)

32-35: Add rustdoc for the new derive.

RuleSourceVariantIndex is now part of this crate’s public surface, but it is undocumented whilst declare_group_from_fs is. A short note about declaration-order indexing would make it much easier to reuse safely.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@crates/biome_analyze_macros/src/lib.rs` around lines 32 - 35, Add a rustdoc
comment for the new derive to document its public API and behavior: above the
proc_macro_derive attribute for rule_source_variant_index, add a short docstring
that explains what RuleSourceVariantIndex does, that it derives an index based
on declaration-order of RuleSource variants, and any caveats about relying on
declaration-order indexing; reference the proc-macro entry point function
rule_source_variant_index (and/or the implementation
rule_source_variant_index::rule_source_variant_index_impl) so users know where
the derive is implemented.
crates/biome_analyze_macros/src/rule_source_variant_index.rs (1)

22-27: Cover the variant-limit guard too.

The non-enum path is tested, but the u16::MAX + 1 guard is still freewheeling. A generated mega-enum in this module would pin that compile-time error down as well.

As per coding guidelines "All code changes MUST include appropriate tests".

Also applies to: 58-105

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@crates/biome_analyze_macros/src/rule_source_variant_index.rs` around lines 22
- 27, Add a unit test that exercises the enum-variant-limit guard: construct a
synthetic syn::DeriveInput with Data::Enum whose variants vec length equals
usize::from(u16::MAX) + 1 (you can build a DataEnum and push dummy Variant
entries rather than writing 65k tokens) and call the same function/path that
contains the guard (the code referencing data_enum, input.ident and returning
the syn::Error with message "RuleSourceVariantIndex supports at most u16::MAX +
1 variants"); assert it returns Err and that the error message contains that
string. Also add a complementary test for the non-enum path if absent. This
ensures the guard in rule_source_variant_index.rs is covered without generating
an enormous real enum.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@crates/biome_analyze_macros/src/lib.rs`:
- Around line 32-35: Add a rustdoc comment for the new derive to document its
public API and behavior: above the proc_macro_derive attribute for
rule_source_variant_index, add a short docstring that explains what
RuleSourceVariantIndex does, that it derives an index based on declaration-order
of RuleSource variants, and any caveats about relying on declaration-order
indexing; reference the proc-macro entry point function
rule_source_variant_index (and/or the implementation
rule_source_variant_index::rule_source_variant_index_impl) so users know where
the derive is implemented.

In `@crates/biome_analyze_macros/src/rule_source_variant_index.rs`:
- Around line 22-27: Add a unit test that exercises the enum-variant-limit
guard: construct a synthetic syn::DeriveInput with Data::Enum whose variants vec
length equals usize::from(u16::MAX) + 1 (you can build a DataEnum and push dummy
Variant entries rather than writing 65k tokens) and call the same function/path
that contains the guard (the code referencing data_enum, input.ident and
returning the syn::Error with message "RuleSourceVariantIndex supports at most
u16::MAX + 1 variants"); assert it returns Err and that the error message
contains that string. Also add a complementary test for the non-enum path if
absent. This ensures the guard in rule_source_variant_index.rs is covered
without generating an enormous real enum.

In `@crates/biome_analyze/src/rule.rs`:
- Around line 97-102: Add a regression test that pins the ordering of the
RuleSource enum by asserting the expected ordering behavior used by Ord and
cmp_any(); specifically, write a unit test that enumerates the RuleSource
variants in their declared order and asserts pairwise comparisons (e.g.,
VariantA < VariantB) and that sorting a vec of all variants yields the declared
order, and also verify cmp_any() produces the same ordering decisions; place the
test adjacent to the RuleSource definition (in the same module's tests) and
reference the RuleSource type and the cmp_any() helper to make it obvious if the
enum order changes.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: d7b04b6a-5f74-4c47-a6a5-c0b8a4391e74

📥 Commits

Reviewing files that changed from the base of the PR and between 1d09f0f and 7b9e7f2.

⛔ Files ignored due to path filters (1)
  • Cargo.lock is excluded by !**/*.lock and included by **
📒 Files selected for processing (4)
  • crates/biome_analyze/Cargo.toml
  • crates/biome_analyze/src/rule.rs
  • crates/biome_analyze_macros/src/lib.rs
  • crates/biome_analyze_macros/src/rule_source_variant_index.rs

@dyc3 dyc3 merged commit 10ecab1 into main Apr 7, 2026
34 checks passed
@dyc3 dyc3 deleted the dyc3/derive-variant-index branch April 7, 2026 12:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-Linter Area: linter

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants