compiler

package
v0.1.35 Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package compiler provides a compiler for GoSPA Single File Components (.gospa).

Index

Constants

This section is empty.

Variables

View Source
var (
	// PropsRegex matches GoSPA $props() declarations.
	PropsRegex = regexp.MustCompile(`(?m)var\s+\{\s*(.*?)\s*\}\s*=\s*\$props\(\)`)
	// StateRegex matches GoSPA $state() declarations.
	StateRegex = regexp.MustCompile(`(?m)(?:var|const)\s+([a-zA-Z0-9_]+)\s*=\s*\$state\((.*?)\)`)
	// DerivedRegex matches GoSPA $derived() declarations.
	DerivedRegex = regexp.MustCompile(`\$derived\((.*?)\)`)
	// EffectRegex matches GoSPA $effect() declarations.
	EffectRegex = regexp.MustCompile(`(?s)\$effect\(func\(\)\s*\{(.*?)\}\)`)
	// CSSDotRegex matches CSS class selectors.
	CSSDotRegex = regexp.MustCompile(`\.([a-zA-Z][a-zA-Z0-9-_]*)`)
	// ReactiveLabelRegex matches GoSPA $: reactive labels.
	ReactiveLabelRegex = regexp.MustCompile(`\$:\s*([a-zA-Z0-9_]+)\s*=\s*([^;\n]+)`)
	// CSSElementRegex matches CSS element selectors.
	CSSElementRegex = regexp.MustCompile(`(?m)^([a-z0-9]+)\s*\{`)
)
View Source
var DangerousCallPatterns = []*regexp.Regexp{
	regexp.MustCompile(`exec\.Command`),
	regexp.MustCompile(`os\.OpenFile`),
	regexp.MustCompile(`os\.Create`),
	regexp.MustCompile(`os\.Remove`),
	regexp.MustCompile(`os\.Rename`),
	regexp.MustCompile(`os\.Mkdir`),
	regexp.MustCompile(`os\.MkdirAll`),
	regexp.MustCompile(`os\.WriteFile`),
	regexp.MustCompile(`os\.RemoveAll`),
	regexp.MustCompile(`os\.Symlink`),
	regexp.MustCompile(`os\.Setenv`),
	regexp.MustCompile(`os\.Getenv`),
	regexp.MustCompile(`system\s*\(`),
	regexp.MustCompile(`syscall\.Exec`),
	regexp.MustCompile(`syscall\.ForkExec`),
	regexp.MustCompile(`unix\.Exec`),
	regexp.MustCompile(`unix\.ForkExec`),
}

DangerousCallPatterns are regex patterns for unsafe function calls that should be rejected in SafeMode.

View Source
var DangerousImportNames = []string{
	"os/exec", "exec",
	"os",
	"unsafe",
	"syscall",
	"plugin",
	"runtime/debug",
	"runtime/pprof",
	"reflect",
}

DangerousImportNames are packages whose presence indicates a trust boundary violation when compiling SFCs from untrusted sources.

Functions

func ExtractTypes added in v0.1.32

func ExtractTypes(script string) ([]Prop, []State)

ExtractTypes parses the script content to find props and state.

func GenerateGoStruct added in v0.1.32

func GenerateGoStruct(name string, props []Prop) string

GenerateGoStruct generates a Go struct for the component props.

func GenerateTSInterface added in v0.1.32

func GenerateTSInterface(name string, props []Prop, states []State) string

GenerateTSInterface generates a TypeScript interface for the component state.

func ValidateSafeScript added in v0.1.32

func ValidateSafeScript(script string) error

ValidateSafeScript validates that script content does not contain dangerous patterns. Returns nil if the script is safe, or an error describing the dangerous pattern found.

Types

type CompileOptions

type CompileOptions struct {
	Type       ComponentType
	IslandID   string
	Name       string
	PkgName    string
	Hydrate    bool
	ServerOnly bool
	// SafeMode enables stricter validation of script content.
	// When true, the compiler checks that Go scripts parse as valid Go and
	// rejects dangerous patterns such as exec.Command, os/exec, unsafe, or
	// syscall imports. Use this when compiling .gospa from sources you do
	// not fully trust (e.g., CMS-generated SFCs).
	SafeMode bool
}

CompileOptions configures the compilation of a .gospa component.

type ComponentType

type ComponentType string

ComponentType represents the type of a compiled component.

const (
	ComponentTypeIsland ComponentType = "island"
	ComponentTypePage   ComponentType = "page"
	ComponentTypeLayout ComponentType = "layout"
	ComponentTypeStatic ComponentType = "static"
	ComponentTypeServer ComponentType = "server"
)

Component type constants.

type GospaCompiler

type GospaCompiler struct{}

GospaCompiler handles the compilation of .gospa files.

func NewCompiler

func NewCompiler() *GospaCompiler

NewCompiler creates a new GospaCompiler.

func (*GospaCompiler) Compile

func (c *GospaCompiler) Compile(opts CompileOptions, input string) (templ, ts string, err error)

Compile compiles a .gospa component into Templ and TypeScript. If opts.SafeMode is true, the script content is validated against a set of dangerous patterns before compilation proceeds.

func (*GospaCompiler) CompileLegacy

func (c *GospaCompiler) CompileLegacy(goName, islandID, input, pkgName string) (templ, ts string, err error)

CompileLegacy preserves the old island-only API.

type Prop added in v0.1.32

type Prop struct {
	Name string
	Type string
}

Prop represents a component prop.

type State added in v0.1.32

type State struct {
	Name         string
	InitialValue string
	Type         string
}

State represents a component reactive state.

Directories

Path Synopsis
Package sfc provides a parser for GoSPA Single File Components (.gospa).
Package sfc provides a parser for GoSPA Single File Components (.gospa).

Jump to

Keyboard shortcuts

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