Skip to content

Commit da674d6

Browse files
committed
refactor(poet): use custom AST structs instead of go/ast wrappers
Redesign the poet package to use custom AST structures specifically designed for Go code generation instead of wrapping go/ast types. Key changes: - New ast.go: Custom types (File, Import, Decl, TypeDef, Func, Struct, Field, Interface, etc.) with proper support for comments - New render.go: Rendering logic that converts custom AST to formatted Go source code with proper comment placement - ImportGroups: Support for separating stdlib and third-party imports with blank lines between groups - TrailingComment: Support for trailing comments on struct fields (e.g., "Valid is true if X is not NULL") Removed old poet package files (expr.go, func.go, poet.go, stmt.go, types.go) that wrapped go/ast which made comment placement difficult. The generator.go now builds poet.File structures and calls poet.Render() to produce formatted Go code that exactly matches the previous output.
1 parent aafc662 commit da674d6

File tree

8 files changed

+1399
-1785
lines changed

8 files changed

+1399
-1785
lines changed

internal/codegen/golang/generator.go

Lines changed: 984 additions & 734 deletions
Large diffs are not rendered by default.

internal/poet/ast.go

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
// Package poet provides Go code generation with custom AST nodes
2+
// that properly support comment placement.
3+
package poet
4+
5+
// File represents a Go source file.
6+
type File struct {
7+
BuildTags string
8+
Comments []string // File-level comments
9+
Package string
10+
ImportGroups [][]Import // Groups separated by blank lines
11+
Decls []Decl
12+
}
13+
14+
// Import represents an import statement.
15+
type Import struct {
16+
Alias string // Optional alias
17+
Path string
18+
}
19+
20+
// Decl represents a declaration.
21+
type Decl interface {
22+
isDecl()
23+
}
24+
25+
// Raw is raw Go code (escape hatch).
26+
type Raw struct {
27+
Code string
28+
}
29+
30+
func (Raw) isDecl() {}
31+
32+
// Const represents a const declaration.
33+
type Const struct {
34+
Comment string
35+
Name string
36+
Type string
37+
Value string
38+
}
39+
40+
func (Const) isDecl() {}
41+
42+
// ConstBlock represents a const block.
43+
type ConstBlock struct {
44+
Consts []Const
45+
}
46+
47+
func (ConstBlock) isDecl() {}
48+
49+
// Var represents a var declaration.
50+
type Var struct {
51+
Comment string
52+
Name string
53+
Type string
54+
Value string
55+
}
56+
57+
func (Var) isDecl() {}
58+
59+
// VarBlock represents a var block.
60+
type VarBlock struct {
61+
Vars []Var
62+
}
63+
64+
func (VarBlock) isDecl() {}
65+
66+
// TypeDef represents a type declaration.
67+
type TypeDef struct {
68+
Comment string
69+
Name string
70+
Type TypeExpr
71+
}
72+
73+
func (TypeDef) isDecl() {}
74+
75+
// Func represents a function declaration.
76+
type Func struct {
77+
Comment string
78+
Recv *Param // nil for non-methods
79+
Name string
80+
Params []Param
81+
Results []Param
82+
Body string // Raw body code
83+
}
84+
85+
func (Func) isDecl() {}
86+
87+
// Param represents a function parameter or result.
88+
type Param struct {
89+
Name string
90+
Type string
91+
}
92+
93+
// TypeExpr represents a type expression.
94+
type TypeExpr interface {
95+
isTypeExpr()
96+
}
97+
98+
// Struct represents a struct type.
99+
type Struct struct {
100+
Fields []Field
101+
}
102+
103+
func (Struct) isTypeExpr() {}
104+
105+
// Field represents a struct field.
106+
type Field struct {
107+
Comment string // Leading comment (above the field)
108+
Name string
109+
Type string
110+
Tag string
111+
TrailingComment string // Trailing comment (on same line)
112+
}
113+
114+
// Interface represents an interface type.
115+
type Interface struct {
116+
Methods []Method
117+
}
118+
119+
func (Interface) isTypeExpr() {}
120+
121+
// Method represents an interface method.
122+
type Method struct {
123+
Comment string
124+
Name string
125+
Params []Param
126+
Results []Param
127+
}
128+
129+
// TypeName represents a type alias or named type.
130+
type TypeName struct {
131+
Name string
132+
}
133+
134+
func (TypeName) isTypeExpr() {}

internal/poet/expr.go

Lines changed: 0 additions & 195 deletions
This file was deleted.

0 commit comments

Comments
 (0)