gotranspiler

package
v0.6.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 6, 2026 License: MIT Imports: 21 Imported by: 0

Documentation

Overview

Package gotranspiler implements TypeScript to Go source code transpilation.

It uses the existing tsgo parser and type checker to parse TypeScript source files, resolve all types, and generate equivalent Go source code.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateBridgeCode

func GenerateBridgeCode(moduleName, pkgAlias string, funcs []ExportedFunc) string

GenerateBridgeCode generates Go source code that registers transpiled functions as a native JS module via NativeModuleFromFuncs. Generic functions are skipped with a warning message.

func GenerateDTS

func GenerateDTS(packages []string) (string, error)

GenerateDTS generates TypeScript .d.ts content for the given Go packages. Package names can optionally have a "go:" prefix (e.g., "go:fmt" or "fmt").

func GenerateNativeImport

func GenerateNativeImport(pkgImport, pkgAlias string) string

GenerateNativeImport generates the import statement for a native package.

func GenericWarnings

func GenericWarnings(funcs []ExportedFunc) []string

GenericWarnings returns warning messages for generic functions that can't be exposed as native module exports.

func TranspileProjectToDir

func TranspileProjectToDir(files []string, entryFile string, outDir string, moduleName string) error

TranspileProjectToDir transpiles multiple TS files and writes the Go project to outDir.

func TranspileProjectToDirWithNpm

func TranspileProjectToDirWithNpm(files []string, entryFile string, outDir string, moduleName string) error

TranspileProjectToDirWithNpm transpiles with npm package resolution.

func TranspileToDir

func TranspileToDir(filename string, outDir string, pkgName string) error

TranspileToDir transpiles a TypeScript file and writes the output to a directory.

Types

type DispatchTarget added in v0.5.0

type DispatchTarget int

DispatchTarget determines how a method call should be emitted in Go. The target is determined by the Go type category of the object, not by method name matching.

const (
	DispatchArrayHelper    DispatchTarget = iota // jsarray.* helpers (for []T types)
	DispatchStringStdlib                         // strings.* stdlib or inline (for string types)
	DispatchPromiseMethod                        // promise.Promise.Then/Await (for *promise.Promise[T])
	DispatchMapOperation                         // Go map operations (for map[K]V types)
	DispatchConcreteMethod                       // obj.Method() direct call (for *Struct types)
	DispatchJSRTRuntime                          // jsrt.Obj().Get().Call() (for any/unknown types)
)

type ExportedFunc

type ExportedFunc struct {
	GoName  string // PascalCase Go name (e.g., "Fibonacci")
	JSName  string // camelCase JS name (e.g., "fibonacci")
	Generic bool   // True if the function has type parameters
}

ExportedFunc represents an exported Go function discovered in transpiled code.

func DiscoverExportedFuncs

func DiscoverExportedFuncs(goSource string) ([]ExportedFunc, error)

DiscoverExportedFuncs parses Go source code and returns all exported top-level functions (including generics, which are flagged for warning).

type GoTypeCategory

type GoTypeCategory int

GoTypeCategory classifies a Go type for emission strategy dispatch.

const (
	GoTypePrimitive GoTypeCategory = iota // string, float64, bool, int
	GoTypePointer                         // *ClassName, *web.Response
	GoTypeInterface                       // discriminated union interfaces (Shape)
	GoTypeSlice                           // []T
	GoTypeMap                             // map[K]V
	GoTypeFunc                            // func(params) returnType
	GoTypePromise                         // *promise.Promise[T]
	GoTypeJSObject                        // *jsrt.JSObject (fallback when unmappable)
)

type GoTypeInfo

type GoTypeInfo struct {
	Category GoTypeCategory
	GoStr    string // Full Go type string: "[]float64", "*web.Response", etc.
	ElemType string // For Slice: element type; for Map: value type; for Promise: inner type
	KeyType  string // For Map: key type
	Name     string // For Pointer/Interface: the type name without *
}

GoTypeInfo holds the categorized Go type for an expression.

func (GoTypeInfo) IsAny

func (g GoTypeInfo) IsAny() bool

func (GoTypeInfo) IsBool

func (g GoTypeInfo) IsBool() bool

func (GoTypeInfo) IsFloat64

func (g GoTypeInfo) IsFloat64() bool

func (GoTypeInfo) IsInt

func (g GoTypeInfo) IsInt() bool

func (GoTypeInfo) IsMap

func (g GoTypeInfo) IsMap() bool

func (GoTypeInfo) IsNumeric

func (g GoTypeInfo) IsNumeric() bool

func (GoTypeInfo) IsPointer

func (g GoTypeInfo) IsPointer() bool

func (GoTypeInfo) IsPromise

func (g GoTypeInfo) IsPromise() bool

func (GoTypeInfo) IsSlice

func (g GoTypeInfo) IsSlice() bool

func (GoTypeInfo) IsString

func (g GoTypeInfo) IsString() bool

type PackageAnalysis

type PackageAnalysis struct {
	Name         string
	Version      string
	EntryFile    string   // Resolved entry point (.ts file)
	SourceFiles  []string // All .ts source files
	IsPureTS     bool
	Dependencies []string
	GoImportPath string // Resolved Go import path for this package
}

PackageAnalysis holds the result of analyzing an npm package.

type ProjectResult

type ProjectResult struct {
	// Files maps output file paths (relative to outDir) to Go source code.
	Files  map[string]string
	Errors []string
	// GoImports holds third-party Go module paths imported via go: prefix.
	GoImports []string
}

ProjectResult holds the output of a multi-file transpilation.

func TranspileProject

func TranspileProject(files []string, entryFile string, _ string, moduleName string) (*ProjectResult, error)

TranspileProject transpiles multiple TypeScript files into a Go project. entryFile is the main file (gets func main()), others become library packages. moduleName is the Go module name (e.g., "myapp").

func TranspileProjectWithNpm

func TranspileProjectWithNpm(files []string, entryFile string, _ string, moduleName string) (*ProjectResult, error)

TranspileProjectWithNpm transpiles multiple TS files with npm package resolution.

type TranspileResult

type TranspileResult struct {
	GoSource string // The generated Go source code
	Errors   []string
}

TranspileResult holds the output of a transpilation.

func TranspileFile

func TranspileFile(filename string, pkgName string) (*TranspileResult, error)

TranspileFile transpiles a single TypeScript file to Go source code. The file is treated as the entry point (generates func main()).

func TranspileLibraryFile

func TranspileLibraryFile(filename string, pkgName string) (*TranspileResult, error)

TranspileLibraryFile transpiles a TypeScript file as a library (no func main()). Top-level statements become func init().

func TranspileSource

func TranspileSource(filename string, source string, pkgName string) (*TranspileResult, error)

TranspileSource transpiles TypeScript source code to Go source code.

type Transpiler

type Transpiler struct {
	// contains filtered or unexported fields
}

Transpiler holds the state for a TypeScript-to-Go transpilation session.

type TypeGenerator

type TypeGenerator struct {
	// contains filtered or unexported fields
}

TypeGenerator generates TypeScript .d.ts declarations from Go packages.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL