tsx

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package tsx parses TypeScript and TSX and lowers Solid JSX.

Parsing is delegated to the Go port of the TypeScript compiler, so the grammar is the real one rather than an approximation. Two entry points:

  • Parse returns the compiler's own AST, which is what TransformSolid and any other analysis should work on.
  • Frontend implements parse.Frontend, converting the type-level subset into this library's canonical AST for generation.

This package depends on github.com/lilybw/typescript-go, a fork that re-exports the compiler internals. Pin it to an exact version.

Index

Constants

View Source
const VirtualRoot = "/"

VirtualRoot is the directory a relative file name is resolved against.

It is a fixed synthetic root rather than the working directory, so that output and diagnostics do not depend on where the build ran. Absolute paths are used unchanged.

Variables

This section is empty.

Functions

func ConvertSourceFile

func ConvertSourceFile(file *tsast.SourceFile) *ast.SourceFile

ConvertSourceFile converts a compiler source file to the canonical AST. Constructs outside the type grammar are preserved verbatim.

func ConvertType

func ConvertType(n *tsast.Node) ast.Type

ConvertType converts a compiler type node to a canonical type. Unrecognized nodes become ast.RawType carrying their source text.

func ToJSX

func ToJSX(n *tsast.Node, mod *solid.Module) (solid.Node, bool)

ToJSX converts a compiler JSX node into the solid IR. Prefer TransformSolid for whole files.

func TransformSolid

func TransformSolid(file *tsast.SourceFile, opts solid.Options) (out string, err error)

TransformSolid lowers every JSX expression in a parsed file and returns the resulting TypeScript source.

Type annotations are left in place: only JSX is lowered. Pass the result to esbuild with the TS loader to strip types and bundle.

Types

type Frontend

type Frontend struct{}

Frontend implements parse.Frontend on top of the TypeScript compiler.

Conversion covers the type grammar and the declarations that carry types. Statements and expressions are preserved verbatim as ast.RawStmt, which keeps the tree printable without duplicating the compiler's expression model. To work with expressions structurally, use Parse instead.

func (Frontend) Capabilities

func (Frontend) Capabilities() parse.Capability

Capabilities reports what this frontend covers.

func (Frontend) ParseFile

func (f Frontend) ParseFile(name string, src []byte) (*ast.SourceFile, error)

ParseFile parses a module and converts it to the canonical AST.

func (Frontend) ParseType

func (f Frontend) ParseType(src []byte) (ast.Type, error)

ParseType parses a single type expression.

type Kind

type Kind = tsast.Kind

Kind is the compiler's node kind discriminant.

type Node

type Node = tsast.Node

Node is the compiler's node type.

type ScriptKind

type ScriptKind uint8

ScriptKind selects the dialect used to parse a file.

const (
	// TS parses TypeScript without JSX, where a leading angle bracket is a type
	// assertion rather than an element.
	TS ScriptKind = iota
	// TSX parses TypeScript with JSX.
	TSX
	// DTS parses a declaration file.
	DTS
	// JS parses JavaScript, including JSDoc types.
	JS
	// JSX parses JavaScript with JSX.
	JSX
)

func ScriptKindOf

func ScriptKindOf(fileName string) ScriptKind

ScriptKindOf infers the dialect from a file name, which is what a build pipeline usually wants.

type SourceFile

type SourceFile = tsast.SourceFile

SourceFile is the compiler's source file type.

func Parse

func Parse(fileName, source string, kind ScriptKind) (file *SourceFile, err error)

Parse parses source text into the compiler's AST.

Syntax errors are returned as a parse.ErrorList, but the tree is still returned: a partial tree is useful to editors and watch-mode rebuilds.

func ParseFile

func ParseFile(fileName, source string) (*SourceFile, error)

ParseFile parses using the dialect inferred from the file name.

Jump to

Keyboard shortcuts

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