Skip to content

Commit

Permalink
wit: special-case Resolve with single package
Browse files Browse the repository at this point in the history
Do not write with additional braces for single-file, multi-package format.

WebAssembly/component-model#340
bytecodealliance/wasm-tools#1577
  • Loading branch information
ydnar committed Jun 9, 2024
1 parent 11cac3c commit 85452a6
Showing 1 changed file with 27 additions and 6 deletions.
33 changes: 27 additions & 6 deletions wit/wit.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ func (r *Resolve) WIT(_ Node, _ string) string {
slices.SortFunc(packages, func(a, b *Package) int {
return strings.Compare(a.Name.String(), b.Name.String())
})
// Special case if only single package.
if len(packages) == 1 {
return packages[0].WIT(nil, "")
}
// Use single-file, multi-package style:
// https://github.com/WebAssembly/component-model/pull/340
// https://github.com/bytecodealliance/wasm-tools/pull/1577
var b strings.Builder
for i, p := range packages {
if i > 0 {
Expand Down Expand Up @@ -876,35 +883,49 @@ func (*Package) WITKind() string { return "package" }
//
// [WIT]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/WIT.md
func (p *Package) WIT(ctx Node, _ string) string {
_, multi := ctx.(*Resolve)
var b strings.Builder
b.WriteString(p.Docs.WIT(ctx, ""))
b.WriteString("package ")
b.WriteString(p.Name.String())
b.WriteString(" {\n")
if multi {
b.WriteString(" {\n")
} else {
b.WriteString(";\n\n")
}
i := 0
if p.Interfaces.Len() > 0 {
i := 0
p.Interfaces.All()(func(name string, face *Interface) bool {
if i > 0 {
b.WriteRune('\n')
}
b.WriteString(indent(face.WIT(p, name)))
if multi {
b.WriteString(indent(face.WIT(p, name)))
} else {
b.WriteString(face.WIT(p, name))
}
b.WriteRune('\n')
i++
return true
})
}
if p.Worlds.Len() > 0 {
i := 0
p.Worlds.All()(func(name string, w *World) bool {
if i > 0 {
b.WriteRune('\n')
}
b.WriteString(indent(w.WIT(p, name)))
if multi {
b.WriteString(indent(w.WIT(p, name)))
} else {
b.WriteString(w.WIT(p, name))
}
b.WriteRune('\n')
i++
return true
})
}
b.WriteRune('}')
if multi {
b.WriteRune('}')
}
return b.String()
}

0 comments on commit 85452a6

Please sign in to comment.