Skip to content

Commit 7f84a85

Browse files
committed
wit: add support for WIT files with multiple packages
bytecodealliance/wasm-tools#1577
1 parent b949897 commit 7f84a85

File tree

2 files changed

+5
-10
lines changed

2 files changed

+5
-10
lines changed

wit/testdata_test.go

-3
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,6 @@ func TestGoldenWITRoundTrip(t *testing.T) {
8080
}
8181
err := loadTestdata(func(path string, res *Resolve) error {
8282
data := res.WIT(nil, "")
83-
if strings.Count(data, "package ") > 1 {
84-
return nil
85-
}
8683
t.Run(path, func(t *testing.T) {
8784
// Run the generated WIT through wasm-tools to generate JSON.
8885
cmd := exec.Command("wasm-tools", "component", "wit", "-j", "--all-features")

wit/wit.go

+5-7
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ func unwrap(s string) string {
5050
// WITKind returns the WIT kind.
5151
func (*Resolve) WITKind() string { return "resolve" }
5252

53-
// WIT returns the [WIT] text format for [Resolve] r. Note that the return value could
54-
// represent multiple files, so may not be precisely valid WIT text.
53+
// WIT returns the [WIT] text format for [Resolve] r.
5554
//
5655
// [WIT]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/WIT.md
5756
func (r *Resolve) WIT(_ Node, _ string) string {
@@ -975,32 +974,31 @@ func (p *Package) WIT(ctx Node, _ string) string {
975974
b.WriteString(p.Docs.WIT(ctx, ""))
976975
b.WriteString("package ")
977976
b.WriteString(p.Name.String())
978-
b.WriteString(";\n")
977+
b.WriteString(" {\n")
979978
if p.Interfaces.Len() > 0 {
980-
b.WriteRune('\n')
981979
i := 0
982980
p.Interfaces.All()(func(name string, face *Interface) bool {
983981
if i > 0 {
984982
b.WriteRune('\n')
985983
}
986-
b.WriteString(face.WIT(p, name))
984+
b.WriteString(indent(face.WIT(p, name)))
987985
b.WriteRune('\n')
988986
i++
989987
return true
990988
})
991989
}
992990
if p.Worlds.Len() > 0 {
993-
b.WriteRune('\n')
994991
i := 0
995992
p.Worlds.All()(func(name string, w *World) bool {
996993
if i > 0 {
997994
b.WriteRune('\n')
998995
}
999-
b.WriteString(w.WIT(p, name))
996+
b.WriteString(indent(w.WIT(p, name)))
1000997
b.WriteRune('\n')
1001998
i++
1002999
return true
10031000
})
10041001
}
1002+
b.WriteRune('}')
10051003
return b.String()
10061004
}

0 commit comments

Comments
 (0)