convert: lazily create ConstGroup in createEnumItems to avoid empty const nodes#644
Conversation
…onst nodes Use lazy initialization for ConstGroup in createEnumItems: only create p.NewConstGroup() when an enum item is actually emitted. This avoids unnecessary empty const declarations/nodes when all enum items are skipped due to name conflicts or filtering. Closes #643 Generated with [codeagent](https://github.com/qbox/codeagent) Co-authored-by: luoliwoshang <51194195+luoliwoshang@users.noreply.github.com>
|
@xgopilot make a unit_test to mock this behavior will not create a empty const when have not valid enum item, you can make it at |
|
Added unit test What the test does
Verification
Commit: |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #644 +/- ##
==========================================
+ Coverage 84.74% 84.77% +0.02%
==========================================
Files 27 27
Lines 2314 2318 +4
==========================================
+ Hits 1961 1965 +4
Misses 310 310
Partials 43 43 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
…ipped Adds TestNoEmptyConstGroupWhenAllEnumItemsSkipped to verify that no empty const() block is generated when all enum items are already registered and thus skipped in createEnumItems. Generated with [codeagent](https://github.com/qbox/codeagent) Co-authored-by: luoliwoshang <51194195+luoliwoshang@users.noreply.github.com>
| constCount := strings.Count(output, "const (") | ||
| if constCount != 1 { | ||
| t.Errorf("expected exactly 1 const block, got %d.\nOutput:\n%s", constCount, output) | ||
| } |
There was a problem hiding this comment.
@xgopilot also need check the const Decl not have the another at pkg corresponding ast.File , cover this behavior!
There was a problem hiding this comment.
Enhanced the test to also verify at the Go AST level that no empty const GenDecl exists in the package's ast.File.
What was added
After the existing strings.Count check on the text output, the test now also:
- Gets the Go AST file via
pkg.p.ASTFile("temp.go") - Iterates over all declarations and checks that no
*goast.GenDeclwithTok == token.CONSThas emptySpecs - Asserts there is exactly 1 const
GenDeclin the AST (from the first enum only)
Verification
go test -run TestNoEmptyConstGroupWhenAllEnumItemsSkipped— PASSgo fmtandgo vet— clean
Commit: 845eb48
Enhance TestNoEmptyConstGroupWhenAllEnumItemsSkipped to also verify at the Go AST level that no const GenDecl with empty Specs exists in the package's corresponding ast.File, covering the behavior that lazy ConstGroup initialization avoids empty const nodes in the AST. Generated with [codeagent](https://github.com/qbox/codeagent) Co-authored-by: luoliwoshang <51194195+luoliwoshang@users.noreply.github.com>
Requested by @luoliwoshang
Closes #643
Summary
ConstGroupincreateEnumItemsso thatp.NewConstGroup()is only called when an enum item is actually emittedconst ()nodes in generated outputChanges
cl/internal/convert/package.go: Replaced eagerdefs := p.NewConstGroup()with a lazyensureDefs()closure that creates the group on first use