Skip to content

Commit 85452a6

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 11cac3c commit 85452a6

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 {
@@ -876,35 +883,49 @@ func (*Package) WITKind() string { return "package" }
876883
//
877884
// [WIT]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/WIT.md
878885
func (p *Package) WIT(ctx Node, _ string) string {
886+
_, multi := ctx.(*Resolve)
879887
var b strings.Builder
880888
b.WriteString(p.Docs.WIT(ctx, ""))
881889
b.WriteString("package ")
882890
b.WriteString(p.Name.String())
883-
b.WriteString(" {\n")
891+
if multi {
892+
b.WriteString(" {\n")
893+
} else {
894+
b.WriteString(";\n\n")
895+
}
896+
i := 0
884897
if p.Interfaces.Len() > 0 {
885-
i := 0
886898
p.Interfaces.All()(func(name string, face *Interface) bool {
887899
if i > 0 {
888900
b.WriteRune('\n')
889901
}
890-
b.WriteString(indent(face.WIT(p, name)))
902+
if multi {
903+
b.WriteString(indent(face.WIT(p, name)))
904+
} else {
905+
b.WriteString(face.WIT(p, name))
906+
}
891907
b.WriteRune('\n')
892908
i++
893909
return true
894910
})
895911
}
896912
if p.Worlds.Len() > 0 {
897-
i := 0
898913
p.Worlds.All()(func(name string, w *World) bool {
899914
if i > 0 {
900915
b.WriteRune('\n')
901916
}
902-
b.WriteString(indent(w.WIT(p, name)))
917+
if multi {
918+
b.WriteString(indent(w.WIT(p, name)))
919+
} else {
920+
b.WriteString(w.WIT(p, name))
921+
}
903922
b.WriteRune('\n')
904923
i++
905924
return true
906925
})
907926
}
908-
b.WriteRune('}')
927+
if multi {
928+
b.WriteRune('}')
929+
}
909930
return b.String()
910931
}

0 commit comments

Comments
 (0)