Skip to content

Commit 44b03fb

Browse files
committed
x/format: GopClass support normal .gox
1 parent ce1c337 commit 44b03fb

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

x/format/gopclass_test.go

+40
Original file line numberDiff line numberDiff line change
@@ -197,3 +197,43 @@ func reset() {
197197
log.println "MainEntry"
198198
`)
199199
}
200+
201+
func TestClassGox(t *testing.T) {
202+
testClass(t, "gox class", "", "Rect", false, `package main
203+
204+
type BaseClass struct {
205+
x int
206+
y int
207+
}
208+
type AggClass struct {
209+
}
210+
type Rect struct {
211+
BaseClass
212+
Width float64
213+
Height float64
214+
*AggClass
215+
}
216+
217+
func (this *Rect) Area() float64 {
218+
return this.Width * this.Height
219+
}
220+
`, `var (
221+
BaseClass
222+
Width float64
223+
Height float64
224+
*AggClass
225+
)
226+
227+
type BaseClass struct {
228+
x int
229+
y int
230+
}
231+
232+
type AggClass struct {
233+
}
234+
235+
func Area() float64 {
236+
return Width * Height
237+
}
238+
`)
239+
}

x/format/gopstyle.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ func Gopstyle(file *ast.File) {
6969
formatFile(file)
7070
}
7171

72+
// GopClassSource format Go+ source to Go+ class source.
73+
// pkg set class project pkg path, emtpy if normal .gox class.
74+
// class set the class name. proj indication project or class.
7275
func GopClassSource(src []byte, pkg string, class string, proj bool, filename ...string) (ret []byte, err error) {
7376
var fname string
7477
if filename != nil {
@@ -285,7 +288,7 @@ func formatClass(file *ast.File, pkg string, class string, proj bool) {
285288
if spec, ok := v.Specs[0].(*ast.TypeSpec); ok && spec.Name.Name == class {
286289
if st, ok := spec.Type.(*ast.StructType); ok {
287290
for _, fs := range st.Fields.List {
288-
if len(fs.Names) == 0 {
291+
if len(fs.Names) == 0 && pkg != "" {
289292
continue
290293
}
291294
varSpecs = append(varSpecs, &ast.ValueSpec{Names: fs.Names, Type: fs.Type})

0 commit comments

Comments
 (0)