Skip to content

Commit e4aea1e

Browse files
committed
wit: special-case Resolve with single package
Do not write with additional braces for single-file, multi-package format. WebAssembly/component-model#340 bytecodealliance/wasm-tools#1577
1 parent 7f84a85 commit e4aea1e

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

wit/wit.go

+27-6
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ func (r *Resolve) WIT(_ Node, _ string) string {
5858
slices.SortFunc(packages, func(a, b *Package) int {
5959
return strings.Compare(a.Name.String(), b.Name.String())
6060
})
61+
// Special case if only single package.
62+
if len(packages) == 1 {
63+
return packages[0].WIT(nil, "")
64+
}
65+
// Use single-file, multi-package style:
66+
// https://github.com/WebAssembly/component-model/pull/340
67+
// https://github.com/bytecodealliance/wasm-tools/pull/1577
6168
var b strings.Builder
6269
for i, p := range packages {
6370
if i > 0 {
@@ -970,35 +977,49 @@ func (*Package) WITKind() string { return "package" }
970977
//
971978
// [WIT]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/WIT.md
972979
func (p *Package) WIT(ctx Node, _ string) string {
980+
_, multi := ctx.(*Resolve)
973981
var b strings.Builder
974982
b.WriteString(p.Docs.WIT(ctx, ""))
975983
b.WriteString("package ")
976984
b.WriteString(p.Name.String())
977-
b.WriteString(" {\n")
985+
if multi {
986+
b.WriteString(" {\n")
987+
} else {
988+
b.WriteString(";\n\n")
989+
}
990+
i := 0
978991
if p.Interfaces.Len() > 0 {
979-
i := 0
980992
p.Interfaces.All()(func(name string, face *Interface) bool {
981993
if i > 0 {
982994
b.WriteRune('\n')
983995
}
984-
b.WriteString(indent(face.WIT(p, name)))
996+
if multi {
997+
b.WriteString(indent(face.WIT(p, name)))
998+
} else {
999+
b.WriteString(face.WIT(p, name))
1000+
}
9851001
b.WriteRune('\n')
9861002
i++
9871003
return true
9881004
})
9891005
}
9901006
if p.Worlds.Len() > 0 {
991-
i := 0
9921007
p.Worlds.All()(func(name string, w *World) bool {
9931008
if i > 0 {
9941009
b.WriteRune('\n')
9951010
}
996-
b.WriteString(indent(w.WIT(p, name)))
1011+
if multi {
1012+
b.WriteString(indent(w.WIT(p, name)))
1013+
} else {
1014+
b.WriteString(w.WIT(p, name))
1015+
}
9971016
b.WriteRune('\n')
9981017
i++
9991018
return true
10001019
})
10011020
}
1002-
b.WriteRune('}')
1021+
if multi {
1022+
b.WriteRune('}')
1023+
}
10031024
return b.String()
10041025
}

0 commit comments

Comments
 (0)