From 334c1870d219e271d212b1d0307c8539324c79f1 Mon Sep 17 00:00:00 2001 From: camc314 <18101008+camc314@users.noreply.github.com> Date: Fri, 22 Aug 2025 02:31:58 +0000 Subject: [PATCH] feat(gen-shims): skip generating shims for types containing unexported types (#128) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://github.com/microsoft/typescript-go/pull/1505 makes some changes that mean that some functions reference types that aren’t exported, this means that we can’t compile tsgolint anymore :disappointed_relieved: this PR skips generating shims for functions with unexported types, logging a warning to stderr. --- tools/gen_shims/main.go | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/tools/gen_shims/main.go b/tools/gen_shims/main.go index 2ce59a0c..38fc4cc1 100644 --- a/tools/gen_shims/main.go +++ b/tools/gen_shims/main.go @@ -3,7 +3,6 @@ package main import ( "bytes" "fmt" - "github.com/go-json-experiment/json" "go/types" "log" "maps" @@ -12,6 +11,8 @@ import ( "slices" "strings" + "github.com/go-json-experiment/json" + "golang.org/x/text/cases" "golang.org/x/text/language" "golang.org/x/tools/go/packages" @@ -19,6 +20,24 @@ import ( const tsgoInternalPrefix = "github.com/microsoft/typescript-go/internal/" +func signatureHasUnexportedType(t types.Signature) bool { + if params := t.Params(); params != nil { + for i := range params.Len() { + ty := params.At(i).Type() + + if ptrType, ok := ty.(*types.Pointer); ok { + ty = ptrType.Elem() + } + if named, ok := ty.(*types.Named); ok { + if !named.Obj().Exported() { + return true + } + } + } + } + return false +} + type ExtraShim struct { ExtraFunctions []string ExtraMethods map[string]([]string) @@ -128,6 +147,10 @@ func main() { // linking to functions with generics is not supported in go:linkname return false } + if signatureHasUnexportedType(*fn.Signature()) { + fmt.Fprintf(os.Stderr, "Skipping %s.%s: references unexported types\n", fn.Pkg().Name(), fn.Name()) + return false + } name := cases.Title(language.English, cases.NoLower).String(fn.Name()) emitGoLinknameDirective(name, fn) shimBuilder.WriteString("func ")