refactor(migrate): auto derive variant_index() function#9803
Conversation
|
Merging this PR will not alter performance
Comparing Footnotes
|
WalkthroughThis change introduces a new procedural derive macro, 🚥 Pre-merge checks | ✅ 2✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (3)
crates/biome_analyze/src/rule.rs (1)
97-102: Please lockRuleSourceordering down with a regression test.The proc-macro unit tests prove the generator, but they do not pin the actual
RuleSourceordering thatOrdandcmp_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.
RuleSourceVariantIndexis now part of this crate’s public surface, but it is undocumented whilstdeclare_group_from_fsis. 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 + 1guard 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
⛔ Files ignored due to path filters (1)
Cargo.lockis excluded by!**/*.lockand included by**
📒 Files selected for processing (4)
crates/biome_analyze/Cargo.tomlcrates/biome_analyze/src/rule.rscrates/biome_analyze_macros/src/lib.rscrates/biome_analyze_macros/src/rule_source_variant_index.rs
Summary
A small refactor to change the
RuleSource::variant_index()function automatically derived. The reason for thisis 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