compiler

package
v0.0.14 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2025 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ComputeModulePath

func ComputeModulePath(outputRoot, goPkg string) string

ComputeModulePath computes the root of the output typescript module.

func TranslateGoFilePathToTypescriptFilePath

func TranslateGoFilePathToTypescriptFilePath(goPkgPath, goCodeFilename string) string

TranslateGoFilePathToTypescriptFilePath converts the go package path and typescript filename to output path within the typescript output dir

Types

type Compiler

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

Compiler is the root compiler for a project.

func NewCompiler

func NewCompiler(conf *Config, le *logrus.Entry, opts *packages.Config) (*Compiler, error)

NewCompiler builds a new Compiler opts can be nil

func (*Compiler) CompilePackages

func (c *Compiler) CompilePackages(ctx context.Context, patterns ...string) error

CompilePackages attempts to build packages.

type CompilerContext

type CompilerContext struct {
	// GoPackage is the name of the go package to compile
	GoPackage string
	// ComputedTsPackage is the path to the typescript package for this Go package.
	ComputedTsPackage string
}

CompilerContext is the context for the compiler.

type Config

type Config struct {

	// Dir is the working directory for the compiler. If empty, uses the current working directory.
	Dir string
	// OutputPathRoot is the output path root.
	OutputPathRoot string
	// BuildFlags are the Go build flags (tags) to use during analysis.
	BuildFlags []string
	// contains filtered or unexported fields
}

Config is the configuration for the compiler Dir is the working directory for the compiler. If empty, uses the current working directory.

func (*Config) Validate

func (c *Config) Validate() error

Validate checks the config.

type FileCompiler

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

FileCompiler is the root compiler for a file.

func NewFileCompiler

func NewFileCompiler(
	compilerConf *Config,
	pkg *packages.Package,
	astFile *ast.File,
	fullPath string,
) (*FileCompiler, error)

NewFileCompiler builds a new FileCompiler

func (*FileCompiler) Compile

func (c *FileCompiler) Compile(ctx context.Context) error

Compile compiles a file.

type GoToTSCompiler

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

GoToTSCompiler compiles Go code to TypeScript code.

func NewGoToTSCompiler

func NewGoToTSCompiler(tsw *TSCodeWriter, pkg *packages.Package, cmap ast.CommentMap) *GoToTSCompiler

NewGoToTSCompiler builds a new GoToTSCompiler

func (*GoToTSCompiler) WriteBasicLitValue

func (c *GoToTSCompiler) WriteBasicLitValue(exp *ast.BasicLit)

WriteBasicLitValue writes a basic literal value.

func (*GoToTSCompiler) WriteBinaryExprValue

func (c *GoToTSCompiler) WriteBinaryExprValue(exp *ast.BinaryExpr) error

WriteBinaryExprValue writes a binary operation on values.

func (*GoToTSCompiler) WriteCallExpr

func (c *GoToTSCompiler) WriteCallExpr(exp *ast.CallExpr) error

WriteCallExpr writes a function call.

func (*GoToTSCompiler) WriteCaseClause

func (c *GoToTSCompiler) WriteCaseClause(exp *ast.CaseClause) error

WriteCaseClause writes a case clause within a switch statement.

func (*GoToTSCompiler) WriteCompositeLitValue

func (c *GoToTSCompiler) WriteCompositeLitValue(exp *ast.CompositeLit) error

WriteCompositeLitValue writes a composite literal value. For array literals, uses type information to determine array length and element type, and fills uninitialized elements with the correct zero value. For map literals, creates a new Map with entries.

func (*GoToTSCompiler) WriteDecls

func (c *GoToTSCompiler) WriteDecls(decls []ast.Decl) error

WriteDecls writes a slice of declarations.

func (*GoToTSCompiler) WriteDeferStmt

func (c *GoToTSCompiler) WriteDeferStmt(exp *ast.DeferStmt) error

WriteDeferStmt writes a defer statement using TypeScript's DisposableStack.

func (*GoToTSCompiler) WriteDoc

func (c *GoToTSCompiler) WriteDoc(doc *ast.CommentGroup)

WriteDoc writes a comment group, preserving // and /* */ styles.

func (*GoToTSCompiler) WriteField

func (c *GoToTSCompiler) WriteField(field *ast.Field, isArguments bool)

WriteField writes a field definition.

func (*GoToTSCompiler) WriteFieldList

func (c *GoToTSCompiler) WriteFieldList(a *ast.FieldList, isArguments bool)

WriteFieldList writes a field list.

func (*GoToTSCompiler) WriteForInit

func (c *GoToTSCompiler) WriteForInit(stmt ast.Stmt) error

WriteForInit writes the initialization part of a for loop header.

func (*GoToTSCompiler) WriteForPost

func (c *GoToTSCompiler) WriteForPost(stmt ast.Stmt) error

WriteForPost writes the post part of a for loop header.

func (*GoToTSCompiler) WriteFuncDeclAsFunction

func (c *GoToTSCompiler) WriteFuncDeclAsFunction(decl *ast.FuncDecl) error

WriteFuncDeclAsFunction writes a function declaration NOTE: This function now ONLY handles regular functions, not methods (functions with receivers). Method generation is handled within the type definition writer (e.g., for structs).

func (*GoToTSCompiler) WriteFuncDeclAsMethod

func (c *GoToTSCompiler) WriteFuncDeclAsMethod(decl *ast.FuncDecl) error

WriteFuncDeclAsMethod writes a TypeScript method declaration from a Go FuncDecl. Assumes it's called only for functions with receivers.

func (*GoToTSCompiler) WriteFuncLitValue

func (c *GoToTSCompiler) WriteFuncLitValue(exp *ast.FuncLit) error

WriteFuncLitValue writes a function literal as a TypeScript arrow function.

func (*GoToTSCompiler) WriteFuncType

func (c *GoToTSCompiler) WriteFuncType(exp *ast.FuncType, isAsync bool)

WriteFuncType writes a function type signature.

func (*GoToTSCompiler) WriteGoType

func (c *GoToTSCompiler) WriteGoType(typ gtypes.Type)

WriteGoType writes a Go type as a TypeScript type.

func (*GoToTSCompiler) WriteIdentType

func (c *GoToTSCompiler) WriteIdentType(exp *ast.Ident)

WriteIdentType writes an identifier used as a type.

func (*GoToTSCompiler) WriteIdentValue

func (c *GoToTSCompiler) WriteIdentValue(exp *ast.Ident)

WriteIdentValue writes an identifier used as a value (variable, function name, nil).

func (*GoToTSCompiler) WriteImportSpec

func (c *GoToTSCompiler) WriteImportSpec(a *ast.ImportSpec)

WriteImportSpec writes an import specification to the output.

func (*GoToTSCompiler) WriteInterfaceMethodSignature

func (c *GoToTSCompiler) WriteInterfaceMethodSignature(field *ast.Field) error

WriteInterfaceMethodSignature writes a TypeScript interface method signature from a Go ast.Field.

func (*GoToTSCompiler) WriteInterfaceType

func (c *GoToTSCompiler) WriteInterfaceType(exp *ast.InterfaceType)

WriteInterfaceType writes an interface type definition.

func (*GoToTSCompiler) WriteKeyValueExprValue

func (c *GoToTSCompiler) WriteKeyValueExprValue(exp *ast.KeyValueExpr) error

WriteKeyValueExprValue writes a key-value pair. Returns an error if writing the key or value fails.

func (*GoToTSCompiler) WriteSelectorExprType

func (c *GoToTSCompiler) WriteSelectorExprType(exp *ast.SelectorExpr) error

WriteSelectorExprType writes a selector expression used as a type (e.g., pkg.Type).

func (*GoToTSCompiler) WriteSelectorExprValue

func (c *GoToTSCompiler) WriteSelectorExprValue(exp *ast.SelectorExpr) error

WriteSelectorExprValue writes a selector expression used as a value (e.g., obj.Field).

func (*GoToTSCompiler) WriteSpec

func (c *GoToTSCompiler) WriteSpec(a ast.Spec) error

WriteSpec writes a specification to the output.

func (*GoToTSCompiler) WriteStarExprType

func (c *GoToTSCompiler) WriteStarExprType(exp *ast.StarExpr)

WriteStarExprType writes a pointer type (e.g., *MyStruct).

func (*GoToTSCompiler) WriteStarExprValue

func (c *GoToTSCompiler) WriteStarExprValue(exp *ast.StarExpr) error

WriteStarExprValue writes a pointer dereference value (e.g., *myVar).

func (*GoToTSCompiler) WriteStmt

func (c *GoToTSCompiler) WriteStmt(a ast.Stmt) error

WriteStmt writes a statement to the output.

func (*GoToTSCompiler) WriteStmtAssign

func (c *GoToTSCompiler) WriteStmtAssign(exp *ast.AssignStmt) error

func (*GoToTSCompiler) WriteStmtBlock

func (c *GoToTSCompiler) WriteStmtBlock(exp *ast.BlockStmt, suppressNewline bool) error

WriteStmtBlock writes a block statement, preserving comments and blank lines.

func (*GoToTSCompiler) WriteStmtCompat

func (c *GoToTSCompiler) WriteStmtCompat(a ast.Stmt) error

Overload for backward compatibility

func (*GoToTSCompiler) WriteStmtExpr

func (c *GoToTSCompiler) WriteStmtExpr(exp *ast.ExprStmt) error

WriteStmtExpr writes an expr statement.

func (*GoToTSCompiler) WriteStmtFor

func (c *GoToTSCompiler) WriteStmtFor(exp *ast.ForStmt) error

WriteStmtFor writes a for statement.

func (*GoToTSCompiler) WriteStmtIf

func (s *GoToTSCompiler) WriteStmtIf(exp *ast.IfStmt) error

WriteStmtIf writes an if statement.

func (*GoToTSCompiler) WriteStmtRange

func (c *GoToTSCompiler) WriteStmtRange(exp *ast.RangeStmt) error

WriteStmtRange writes a for…range loop by generating equivalent TypeScript code.

func (*GoToTSCompiler) WriteStmtReturn

func (c *GoToTSCompiler) WriteStmtReturn(exp *ast.ReturnStmt) error

WriteStmtReturn writes a return statement.

func (*GoToTSCompiler) WriteStmtSelect

func (c *GoToTSCompiler) WriteStmtSelect(exp *ast.SelectStmt) error

WriteStmtSelect writes a select statement.

func (*GoToTSCompiler) WriteStmtSend

func (c *GoToTSCompiler) WriteStmtSend(exp *ast.SendStmt) error

WriteStmtSend writes a channel send statement (ch <- value).

func (*GoToTSCompiler) WriteStmtSwitch

func (c *GoToTSCompiler) WriteStmtSwitch(exp *ast.SwitchStmt) error

WriteStmtSwitch writes a switch statement.

func (*GoToTSCompiler) WriteStructType

func (c *GoToTSCompiler) WriteStructType(exp *ast.StructType)

WriteStructType writes a struct type definition.

func (*GoToTSCompiler) WriteTypeAssertExpr

func (c *GoToTSCompiler) WriteTypeAssertExpr(exp *ast.TypeAssertExpr) error

WriteTypeAssertExpr writes a type assertion expression.

func (*GoToTSCompiler) WriteTypeExpr

func (c *GoToTSCompiler) WriteTypeExpr(a ast.Expr)

WriteTypeExpr writes an expression that represents a type.

func (*GoToTSCompiler) WriteTypeSpec

func (c *GoToTSCompiler) WriteTypeSpec(a *ast.TypeSpec) error

WriteTypeSpec writes the type specification to the output.

func (*GoToTSCompiler) WriteUnaryExprValue

func (c *GoToTSCompiler) WriteUnaryExprValue(exp *ast.UnaryExpr) error

WriteUnaryExprValue writes a unary operation on a value.

func (*GoToTSCompiler) WriteValueExpr

func (c *GoToTSCompiler) WriteValueExpr(a ast.Expr) error

WriteValueExpr writes an expression that represents a value.

func (*GoToTSCompiler) WriteValueSpec

func (c *GoToTSCompiler) WriteValueSpec(a *ast.ValueSpec) error

WriteValueSpec writes the value specification to the output.

func (*GoToTSCompiler) WriteZeroValue

func (c *GoToTSCompiler) WriteZeroValue(expr ast.Expr)

WriteZeroValue writes the TypeScript zero‐value for a Go type.

func (*GoToTSCompiler) WriteZeroValueForType

func (c *GoToTSCompiler) WriteZeroValueForType(typ any)

WriteZeroValueForType writes the zero value for a given type. Handles array types recursively.

type PackageCompiler

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

PackageCompiler compiles an entire package.

func NewPackageCompiler

func NewPackageCompiler(
	le *logrus.Entry,
	compilerConf *Config,
	pkg *packages.Package,
) (*PackageCompiler, error)

NewPackageCompiler builds a new PackageCompiler.

func (*PackageCompiler) Compile

func (c *PackageCompiler) Compile(ctx context.Context) error

Compile compiles the package.

func (*PackageCompiler) CompileFile

func (p *PackageCompiler) CompileFile(ctx context.Context, name string, syntax *ast.File) error

CompileFile compiles a file.

type TSCodeWriter

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

TSCodeWriter writes TypeScript code.

func NewTSCodeWriter

func NewTSCodeWriter(w io.Writer) *TSCodeWriter

NewTSCodeWriter builds a new TypeScript code writer.

func (*TSCodeWriter) Indent

func (w *TSCodeWriter) Indent(count int)

Indent changes the indentation level by a delta.

func (*TSCodeWriter) WriteCommentInline

func (w *TSCodeWriter) WriteCommentInline(commentText string)

WriteCommentInline write a comment within /* */.

func (*TSCodeWriter) WriteCommentLine

func (w *TSCodeWriter) WriteCommentLine(commentText string)

WriteCommentLine writes a comment as a // line.

func (*TSCodeWriter) WriteImport

func (w *TSCodeWriter) WriteImport(symbolName, importPath string)

WriteImport writes a TypeScript import.

func (*TSCodeWriter) WriteLine

func (w *TSCodeWriter) WriteLine(line string)

WriteLine writes a line of code to the output.

func (*TSCodeWriter) WriteLinePreamble

func (w *TSCodeWriter) WriteLinePreamble()

WriteLinePreamble writes the indentation.

func (*TSCodeWriter) WriteLinef

func (w *TSCodeWriter) WriteLinef(line string, args ...any)

WriteLinef writes a formatted line of code to the output.

func (*TSCodeWriter) WriteLiterally

func (w *TSCodeWriter) WriteLiterally(literal string)

WriteLiterally writes something to the output without processing

func (*TSCodeWriter) WriteSectionTail

func (w *TSCodeWriter) WriteSectionTail()

WriteSectionTail writes the end of a section.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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