Documentation
¶
Overview ¶
Package solid lowers JSX into the DOM-expressions calls that solid-js/web provides.
The transform operates on the JSX intermediate representation defined in this file rather than on any parser's AST. Build the IR directly, or use the tsx package to produce it from TypeScript source.
m := solid.NewModule(solid.Options{})
expr := m.Compile(node)
src := m.Prelude() + "const C = () => " + expr + ";" + m.Postlude()
Compile returns a JavaScript expression. Prelude returns the runtime import and hoisted templates the compiled expressions refer to, and Postlude the event delegation registration; both must be included in the emitted module.
Index ¶
- Variables
- func DelegatedEvents() []string
- func Display(v any) (string, bool)
- func EscapeValue(s string, mode Escape) string
- func Eval(p Plan, props map[string]any) (any, error)
- func FormatNumber(f float64) string
- func IsBooleanAttribute(name string) bool
- func IsComponentTag(tag string) bool
- func IsDelegatedEvent(name string) bool
- func IsPropertyAttribute(name string) bool
- func IsVoidElement(tag string) bool
- func NormalizeJSXText(s string) string
- func PlanKey(p Plan) string
- func PropertyAttributes() []string
- func Reads(p Plan) []string
- func StrictEqual(l, r any) bool
- func ToNumber(v any) float64
- func ToString(v any) string
- func Truthy(v any) bool
- type Attr
- type AttrKind
- type Attribute
- type BinOp
- type Binary
- type Component
- type Concat
- type Cond
- type Const
- type Element
- type Escape
- type Expr
- type Fragment
- type Hole
- type Map
- type Module
- type Node
- type Options
- type Path
- type Plan
- type Program
- type Red
- type Segment
- type Slot
- type Spread
- type Static
- type Text
- type UnOp
- type Unary
- type Value
- type ValueKind
Constants ¶
This section is empty.
Variables ¶
var Undef = undef{}
Undef is the value of a property that was never set.
Functions ¶
func DelegatedEvents ¶ added in v0.2.2
func DelegatedEvents() []string
DelegatedEvents lists the delegated event names, sorted.
func Display ¶ added in v0.2.2
Display encodes a value as JSX writes it, reporting false for the values JSX omits: null, undefined, and the booleans.
func EscapeValue ¶ added in v0.2.2
EscapeValue encodes a value arriving at render time, which is how a renderer must write what a Hole or an Attribute evaluates to.
Unlike the escaping applied to author-written text, an ampersand is always encoded: a value that happens to read as a character reference is data.
func Eval ¶ added in v0.2.2
Eval computes a plan against props. The error reports the first construct the plan required and the value could not supply.
v, err := solid.Eval(slot.Plan, props) s, ok := solid.Display(v) // ok is false for values JSX omits
func FormatNumber ¶ added in v0.2.2
FormatNumber implements ECMAScript Number::toString for base ten.
Go's shortest representation agrees on digits but not on when to switch to exponential form, which JavaScript does only at 1e21 and below 1e-6.
func IsBooleanAttribute ¶ added in v0.2.2
IsBooleanAttribute reports whether an attribute carries its value by presence rather than by content.
func IsComponentTag ¶
IsComponentTag reports whether a JSX tag names a component rather than a host element: capitalized, dotted, or namespaced.
func IsDelegatedEvent ¶
IsDelegatedEvent reports whether an event name is delegated by solid-js.
func IsPropertyAttribute ¶ added in v0.2.2
IsPropertyAttribute reports whether a name is assigned as a DOM property.
func IsVoidElement ¶
IsVoidElement reports whether tag is an HTML void element.
func NormalizeJSXText ¶
NormalizeJSXText applies JSX's whitespace rules to raw text between tags: whitespace-only lines vanish, indentation is stripped, and interior line breaks collapse to a single space.
The empty string means the text contributes nothing.
func PlanKey ¶ added in v0.2.2
PlanKey renders a plan compactly. Two plans with the same key compute the same value from the same fields, which is what lets a consumer key a cache on a slot rather than on whole props.
func PropertyAttributes ¶ added in v0.2.2
func PropertyAttributes() []string
PropertyAttributes lists the property-assigned names, sorted.
func Reads ¶ added in v0.2.2
Reads reports every props path a plan depends on, sorted and deduplicated. A fragment's cache key needs only these fields rather than the whole props object.
key := hashFields(props, solid.Reads(slot.Plan))
func StrictEqual ¶ added in v0.2.2
StrictEqual implements ===, under which NaN equals nothing and the two zeroes are equal.
Types ¶
type Attr ¶
type Attr struct {
Namespace string
Name string
Kind AttrKind
// Value is the string content for AttrString, or the expression source
// for AttrExpr.
Value string
// Eval derives the value from props alone, or is nil when the frontend
// could not prove it derivable. Only ever set for AttrExpr. See [Plan].
Eval Plan
}
Attr is a JSX attribute. Namespace holds the part before a colon, as in on:click, use:directive, prop:value, attr:id, bool:x, style:color, class:active.
type Attribute ¶ added in v0.2.2
type Attribute struct {
Name string
Slot Slot
// Bool marks an attribute whose presence carries its value: written bare
// when the value is truthy, omitted otherwise.
Bool bool
}
Attribute writes name="value". A value JSX omits omits the attribute entirely rather than writing it empty.
type BinOp ¶ added in v0.2.2
type BinOp uint8
BinOp is a binary operator. Only operators with agreed semantics across the JavaScript and Go evaluators appear here; == and != deliberately do not.
type Component ¶
Component is a capitalized or dotted tag, which compiles to a createComponent call rather than to DOM.
type Concat ¶ added in v0.2.2
type Concat struct{ Parts []Plan }
Concat is string building, from a template literal or the + operator.
type Cond ¶ added in v0.2.2
type Cond struct{ If, Then, Else Plan }
Cond is a ternary, or a logical operator lowered to one.
type Const ¶ added in v0.2.2
type Const struct{ Value Value }
Const is a value fixed at compile time.
type Escape ¶ added in v0.2.2
type Escape uint8
Escape selects the encoding a plan's result is written with.
type Expr ¶
type Expr struct {
Code string
// Eval derives the value from props alone, or is nil when the frontend
// could not prove it derivable. See [Plan].
Eval Plan
}
Expr is a {...} interpolation. Code is the original TypeScript source of the expression, which is emitted verbatim.
type Fragment ¶
type Fragment struct {
Children []Node
}
Fragment is <>...</>, which compiles to an array.
type Hole ¶ added in v0.2.2
type Hole struct{ Slot Slot }
Hole writes a slot's value as character data, or nothing when the value is one JSX omits.
type Map ¶ added in v0.2.2
Map repeats Body once per element of Each, binding the element to Bind. It is what <For>, <Index>, and Array.prototype.map lower to.
type Module ¶
type Module struct {
// contains filtered or unexported fields
}
Module accumulates the state shared across every JSX expression in one source file: hoisted templates, the runtime helpers used, and the events needing delegation.
Compile one expression at a time, then take the Prelude and Postlude. A Module is not safe for concurrent use.
func (*Module) Compile ¶
Compile lowers one JSX node into a JavaScript expression, suitable for substituting in place of the original JSX.
func (*Module) Helpers ¶
Helpers returns the runtime helpers used so far, sorted. The template helper is included whenever any template was hoisted.
type Node ¶
type Node interface {
// contains filtered or unexported methods
}
Node is a node of the JSX intermediate representation.
type Options ¶
type Options struct {
// ModuleName is the import source for runtime helpers.
// Defaults to "solid-js/web".
ModuleName string
// Delegate routes supported events through a single document-level
// listener. Defaults to true; set DisableDelegation to turn it off.
DisableDelegation bool
// Prefix is prepended to generated runtime helper identifiers.
// Defaults to "_$", matching solid's own output.
Prefix string
}
Options configures the transform. The zero value is valid.
type Path ¶ added in v0.2.2
Path is a property chain rooted at props or at a Map binding, as in props.user.name.
type Plan ¶ added in v0.2.2
type Plan interface {
// contains filtered or unexported methods
}
Plan is a hole's value expressed over props. A nil Plan marks a red hole: one no frontend could prove derivable.
type Program ¶ added in v0.2.2
type Program struct {
Segments []Segment
// Red lists the holes that need the client. A program with none can be
// rendered from props alone.
Red []Red
// Interactive lists the reasons the component needs client JavaScript
// even where its markup is complete: event handlers, refs, directives.
Interactive []Red
}
Program is a component's markup as segments, with whatever it could not describe.
func Emit ¶ added in v0.2.2
Emit lowers a JSX node into a segment program.
prog := solid.Emit(node)
if prog.Inert() { /* markup alone; the component needs no client JS */ }
func (*Program) Green ¶ added in v0.2.2
Green reports whether every hole could be described in terms of props, which is what makes the program renderable without a JavaScript engine.
type Red ¶ added in v0.2.2
Red is a hole whose markup could not be described without running JavaScript. Code is the expression's source, for diagnostics.
type Segment ¶ added in v0.2.2
type Segment interface {
// contains filtered or unexported methods
}
Segment is one piece of a program's markup.
type Slot ¶ added in v0.2.2
Slot is one plan together with the encoding its result is written with. It is the unit a segment program interleaves with static markup.
type Static ¶ added in v0.2.2
type Static struct{ HTML string }
Static is markup fixed at compile time, already escaped.
type Text ¶
type Text struct {
Value string
}
Text is literal text between tags. Value is the already-normalized text; see NormalizeJSXText.