Documentation
¶
Overview ¶
Package compiler parses .gx files into an AST. The AST is the input of the formatter, the type checker and the code generator.
Index ¶
- Constants
- Variables
- func Describe(root string) (*AppModel, []Diagnostic)
- func Format(f *File) []byte
- func ImportPath(raw string) string
- func ImportQualifier(raw string) string
- func ParseFile(file string, src []byte) (*File, []Diagnostic)
- func ParseFragment(src []byte) (*File, []Diagnostic)
- func Quoted(s string) string
- func Routes(root string) ([]RouteReport, []Diagnostic)
- type ActionModel
- type AppModel
- type Attr
- type AttrKind
- type Case
- type CheckOptions
- type CollectionRef
- type Comment
- type Component
- type ComponentModel
- type ComponentRef
- type ContentComponent
- type Control
- type Diagnostic
- func Check(root string) []Diagnostic
- func CheckApp(root string, opt CheckOptions) []Diagnostic
- func CheckWith(root string, opt CheckOptions) []Diagnostic
- func FormatSource(file string, src []byte) ([]byte, []Diagnostic)
- func Generate(root string) (map[string][]byte, []Diagnostic)
- func Stale(root string) []Diagnostic
- type Element
- type Expr
- type Field
- type FieldModel
- type File
- type FormFieldModel
- type FormModel
- type FragmentModel
- type HTMLComment
- type IconSetModel
- type Import
- type Info
- type IslandModel
- type Let
- type Model
- type Module
- type Node
- type Package
- type ParamModel
- type Pos
- type Prop
- type PropModel
- type RegistryModel
- type RouteModel
- type RouteRef
- type RouteReport
- type Session
- func (s *Session) ClearOverlay(path string)
- func (s *Session) Content(root string) []Diagnostic
- func (s *Session) Diagnostics() []Diagnostic
- func (s *Session) Generate(root string) (map[string][]byte, []Diagnostic)
- func (s *Session) Model(root string) (*Model, []Diagnostic)
- func (s *Session) SetOverlay(path string, src []byte)
- type SignalModel
- type Symbol
- type Text
- type TransitionModel
Constants ¶
const ( CodeParse = "GX1000" CodeFileName = "GX1001" CodeStale = "GX1002" CodeRouteField = "GX3000" CodePathVar = "GX3001" CodePathField = "GX3002" CodeUnmounted = "GX3003" CodeDuplicate = "GX3004" CodeRoutePkg = "GX3005" CodeRequiredProp = "GX2001" CodeUnknownComponent = "GX2002" CodeUnknownAttr = "GX2003" CodeStaticStringProp = "GX2004" CodeType = "GX2000" CodeSpread = "GX2005" CodeDuplicateSlot = "GX2006" CodeEventAttr = "GX2007" CodeFragment = "GX2008" CodeLoopKey = "GX2009" CodeSignal = "GX2010" CodeURLAttr = "GX2011" CodeUnrenderable = "GX2013" CodeTrustedHTML = "GX7001" CodeSecret = "GX7002" CodeEnum = "GX5001" CodeTransition = "GX5002" CodeRuntimeClass = "GX5003" CodeContentFrontmatter = "GX8001" CodeContentComponent = "GX8002" CodeContentLink = "GX8003" CodeCodeFile = "GX8004" CodeActionMissing = "GX4001" CodeActionTwice = "GX4002" CodeActionMissingSignal = "GX4003" CodeSignalTypeMismatch = "GX4004" CodeClientCall = "GX4005" CodeClientType = "GX4007" CodeInstanceKey = "GX2012" CodeSignalDefault = "GX2014" CodeSignalRoot = "GX2015" CodeEventMod = "GX4010" CodeActionMethod = "GX4009" CodeSignalRules = "GX4008" )
Diagnostic codes. Every code is stable and has a docs page (REQ-AUT-19).
Variables ¶
var Catalog = []Info{ {CodeParse, "parse error"}, {CodeFileName, "component file name is not an exported identifier"}, {CodeStale, "generated code is missing or stale"}, {CodeType, "type error in an expression"}, {CodeRequiredProp, "missing required prop"}, {CodeUnknownComponent, "unknown component"}, {CodeUnknownAttr, "unknown attribute or slot"}, {CodeStaticStringProp, "static value for a typed prop"}, {CodeSpread, "attribute spread on a component"}, {CodeDuplicateSlot, "duplicate slot"}, {CodeEventAttr, "dynamic event attribute"}, {CodeFragment, "fragment free variable"}, {CodeLoopKey, "loop needs a key"}, {CodeSignal, "signal in a server expression"}, {CodeInstanceKey, "signal instance needs a key"}, {CodeSignalDefault, "signal needs an initial value"}, {CodeSignalRoot, "component with signals has no top-level HTML element"}, {CodeURLAttr, "dynamic URL attribute"}, {CodeUnrenderable, "value cannot render as text"}, {CodeRouteField, "route field type cannot bind"}, {CodePathVar, "pattern variable has no field"}, {CodePathField, "path field has no pattern variable"}, {CodeUnmounted, "route value is not held by any gx.Collect"}, {CodeDuplicate, "duplicate route pattern"}, {CodeRoutePkg, "route package contents"}, {CodeActionMissing, "no action is registered for a route type"}, {CodeActionTwice, "more than one action is registered for a route type"}, {CodeActionMissingSignal, "no signal is declared for a signal-bound field"}, {CodeSignalTypeMismatch, "signal type does not match the action field"}, {CodeClientCall, "call is not allowed in a client expression"}, {CodeClientType, "value or operator is not allowed in a client expression"}, {CodeSignalRules, "signal-bound fields need rules or gx.Unchecked"}, {CodeActionMethod, "action method cannot be invoked from the client"}, {CodeEventMod, "unknown event modifier or special event"}, {CodeEnum, "gx.Enum misses a constant of its type"}, {CodeTransition, "duplicate view-transition-name in one template"}, {CodeRuntimeClass, "class string is built at runtime"}, {CodeTrustedHTML, "conversion to gx.SafeHTML needs //gx:trusted"}, {CodeSecret, "gx.Secret cannot cross to the client"}, {CodeContentFrontmatter, "frontmatter is malformed or unknown"}, {CodeContentComponent, "component is not declared in this collection"}, {CodeContentLink, "content link or anchor is broken"}, {CodeCodeFile, "code file or line range is missing"}, }
Catalog lists every diagnostic code (REQ-AUT-19). Keep it in step with the code constants above; the catalog test fails when it does not.
var DescribeSchema []byte
DescribeSchema is the JSON Schema of AppModel. `gx describe --schema` prints it.
Functions ¶
func Describe ¶
func Describe(root string) (*AppModel, []Diagnostic)
Describe returns the app model of the module under root (REQ-AI-01).
func ImportPath ¶
ImportPath returns the path of an import spec as written, without the alias and quotes (REQ-DEV-08).
func ImportQualifier ¶
ImportQualifier returns the name an import spec is used by. An import without an alias uses the last path segment.
func ParseFile ¶
func ParseFile(file string, src []byte) (*File, []Diagnostic)
ParseFile parses a .gx file. file may be empty for input with no file name, such as stdin; the component name check is then skipped.
func ParseFragment ¶
func ParseFragment(src []byte) (*File, []Diagnostic)
ParseFragment parses markup with no package clause, imports, props or signals. It is used by the HTML corpus tests.
func Routes ¶
func Routes(root string) ([]RouteReport, []Diagnostic)
Routes reports every route of the module under root.
Types ¶
type ActionModel ¶
type ActionModel struct {
Handler string `json:"handler"`
Route string `json:"route"`
Method string `json:"method"`
Pattern string `json:"pattern"`
// Signals lists the signal names the action reads.
Signals []string `json:"signals"`
// External is true when another server answers the action.
External bool `json:"external"`
}
ActionModel is one gx.Action.
type AppModel ¶
type AppModel struct {
Module string `json:"module"`
Components []ComponentModel `json:"components"`
Routes []RouteModel `json:"routes"`
Actions []ActionModel `json:"actions"`
Forms []FormModel `json:"forms"`
Islands []IslandModel `json:"islands"`
Transitions []TransitionModel `json:"transitions"`
Icons []IconSetModel `json:"icons"`
Registry []RegistryModel `json:"registry"`
}
AppModel is the machine-readable model of an app (REQ-AI-01). `gx describe --json` prints it and the dev MCP server answers with it. The CLI adds the icon sets and the registry items from gx.lock.
type CheckOptions ¶
type CheckOptions struct {
// ExternalLinks requests every external content link.
ExternalLinks bool
// Client is the HTTP client of the external check.
Client *http.Client
}
CheckOptions configure one check (REQ-CNT-10).
type CollectionRef ¶
type CollectionRef struct {
Dir string
Components []ContentComponent
}
CollectionRef is one gx.Collection declaration (REQ-CNT-03).
type ComponentModel ¶
type ComponentModel struct {
Name string `json:"name"`
Package string `json:"package"`
File string `json:"file"`
Props []PropModel `json:"props"`
Signals []SignalModel `json:"signals"`
Fragments []FragmentModel `json:"fragments"`
// Fixtures holds the fixture names of the component, in order.
Fixtures []string `json:"fixtures"`
}
ComponentModel is one .gx component.
type ComponentRef ¶
ComponentRef is one component of the module (REQ-DEV-08).
type ContentComponent ¶
ContentComponent is one component a collection allows in its Markdown files.
type Diagnostic ¶
Diagnostic is one compiler message.
func Check ¶
func Check(root string) []Diagnostic
Check checks every .gx package under root and returns the diagnostics, ordered by file, line and column. It checks props and components, then type-checks every server expression.
func CheckApp ¶
func CheckApp(root string, opt CheckOptions) []Diagnostic
CheckApp is the whole check of an app, as `gx check` runs it: every diagnostic of CheckWith, the diagnostics that only the generator can give, and a GX1002 for each generated file that is missing or stale. A diagnostic that both passes find is reported once.
func CheckWith ¶
func CheckWith(root string, opt CheckOptions) []Diagnostic
CheckWith checks a module with options (REQ-CNT-10).
func FormatSource ¶
func FormatSource(file string, src []byte) ([]byte, []Diagnostic)
FormatSource parses src and returns its canonical form (REQ-AUT-17). It returns the parse diagnostics instead when src does not parse.
func Generate ¶
func Generate(root string) (map[string][]byte, []Diagnostic)
Generate generates Go source for every .gx file under root. The result maps each generated file path to its source. It returns diagnostics instead when any check fails.
func Stale ¶
func Stale(root string) []Diagnostic
Stale returns GX1002 for every .gx file whose generated file is missing or out of date (REQ-AUT-18).
func (Diagnostic) Doc ¶
func (d Diagnostic) Doc() string
Doc returns the documentation path of the diagnostic (REQ-AUT-19).
func (Diagnostic) String ¶
func (d Diagnostic) String() string
type Element ¶
type Element struct {
At Pos
Name string
Attrs []Attr
Children []Node
RawText string
HasRaw bool
SelfClose bool
}
Element is an HTML element, a component tag or a slot.
type Field ¶
Field is one props or signals field. Doc is the text of the // lines above it, one line per comment line.
type FieldModel ¶
type FieldModel struct {
Name string `json:"name"`
Type string `json:"type"`
// Source is "path", "query", "signal" or "form".
Source string `json:"source"`
// Key is the name of the field in its source.
Key string `json:"key"`
Default string `json:"default,omitempty"`
}
FieldModel is one input field of a route.
type File ¶
type File struct {
File string
Package string
Imports []Import
HasProps bool
Props []Field
HasSignals bool
Signals []Field
Body []Node
}
File is the parsed form of one .gx file or fragment.
type FormFieldModel ¶
type FormFieldModel struct {
Name string `json:"name"`
Type string `json:"type"`
Rules []string `json:"rules"`
}
FormFieldModel is one form field. Name is the form control name; a nested field has a dotted name.
type FormModel ¶
type FormModel struct {
Handler string `json:"handler"`
Route string `json:"route"`
Method string `json:"method"`
Pattern string `json:"pattern"`
Fields []FormFieldModel `json:"fields"`
}
FormModel is one gx.Form with the rules of its input.
type FragmentModel ¶
type FragmentModel struct {
Name string `json:"name"`
// Func is the generated function, for example "CartTotal".
Func string `json:"func"`
// Params is the parameter list of the generated function.
Params []ParamModel `json:"params"`
// Keyed is true when the first parameter is the instance or loop key.
Keyed bool `json:"keyed"`
}
FragmentModel is one #fragment of a component and its generated function.
type HTMLComment ¶
HTMLComment is a <!-- ... --> comment. It renders.
func (*HTMLComment) Position ¶
func (n *HTMLComment) Position() Pos
type IconSetModel ¶
IconSetModel is one pinned icon set.
type Import ¶
type Import struct {
Raw string
}
Import is one import spec, as written, without the import keyword.
type IslandModel ¶
type IslandModel struct {
Name string `json:"name"`
Package string `json:"package"`
File string `json:"file"`
}
IslandModel is one TypeScript island. Islands arrive with release 0.2.0, so the list is empty until then.
type Model ¶
type Model struct {
Root string
Files []*File
Components []ComponentRef
Routes []RouteRef
Symbols []Symbol
Packages map[string][]string // import path -> exported names
Collections []CollectionRef // gx.Collection declarations (REQ-CNT-03)
}
Model is the whole-module index of one analysis pass (REQ-DEV-11). The LSP answers completion, hover and definition from it.
type Node ¶
type Node interface {
Position() Pos
// contains filtered or unexported methods
}
Node is a markup node.
func VisibleNodes ¶
VisibleNodes returns the nodes a renderer emits: all nodes except {/* ... */} comments (REQ-AUT-16).
type Package ¶
type Package struct {
Dir string
Module *Module
Files map[string]*File // base name without .gx
Diags []Diagnostic
}
Package is the parsed .gx files of one directory.
type ParamModel ¶
ParamModel is one parameter of a generated function.
type PropModel ¶
type PropModel struct {
Name string `json:"name"`
Type string `json:"type"`
Required bool `json:"required"`
Default string `json:"default,omitempty"`
Doc string `json:"doc,omitempty"`
}
PropModel is one prop of a component.
type RegistryModel ¶
type RegistryModel struct {
Name string `json:"name"`
Version string `json:"version"`
Files []string `json:"files"`
}
RegistryModel is one installed registry item.
type RouteModel ¶
type RouteModel struct {
// Type is the import path and the type name.
Type string `json:"type"`
Method string `json:"method"`
Pattern string `json:"pattern"`
Fields []FieldModel `json:"fields"`
// Kind is "page", "action", "form" or "none" when nothing handles the
// route.
Kind string `json:"kind"`
// Handler is the package-level value that handles the route.
Handler string `json:"handler,omitempty"`
Prefix string `json:"prefix,omitempty"`
Layouts []string `json:"layouts,omitempty"`
Middleware []string `json:"middleware,omitempty"`
}
RouteModel is one route type.
type RouteRef ¶
type RouteRef struct {
Key string // import path and type name
PkgPath string
Name string
Pattern string
Method string
File string
Line int
Col int
Fields []string
Registered bool
}
RouteRef is one route struct of the module (REQ-DEV-08, REQ-DEV-11).
type RouteReport ¶
type RouteReport struct {
Type string `json:"type"`
Method string `json:"method"`
Pattern string `json:"pattern"`
Fields []string `json:"fields"`
Page string `json:"page,omitempty"`
Prefix string `json:"prefix,omitempty"`
Layouts []string `json:"layouts,omitempty"`
Middleware []string `json:"middleware,omitempty"`
}
RouteReport is one entry of `gx routes` (REQ-RTE-14).
type Session ¶
type Session struct {
// contains filtered or unexported fields
}
Session caches one module analysis across Generate calls, so a change to one .gx body re-checks that file alone and regenerates that file alone (NFR-05, REQ-DEV-03). A change to a Go file, to a .gx signature or to the file set falls back to a full Generate.
func (*Session) ClearOverlay ¶
ClearOverlay drops the in-memory content of one file.
func (*Session) Content ¶
func (s *Session) Content(root string) []Diagnostic
Content returns the diagnostics of the Markdown files of every gx.Collection declaration (REQ-CNT-03). An open buffer wins over disk.
func (*Session) Diagnostics ¶
func (s *Session) Diagnostics() []Diagnostic
Diagnostics returns the diagnostics of the last Generate call.
func (*Session) Generate ¶
func (s *Session) Generate(root string) (map[string][]byte, []Diagnostic)
Generate returns the generated files and diagnostics of root, reusing the previous analysis when only .gx bodies changed (NFR-05).
func (*Session) Model ¶
func (s *Session) Model(root string) (*Model, []Diagnostic)
Model runs the analysis and returns the whole-module index. It reuses the session cache, so an edit only re-checks the changed file (REQ-DEV-11).
func (*Session) SetOverlay ¶
SetOverlay records in-memory content for one input file. The next Generate call reads the overlay instead of the file on disk.
type SignalModel ¶
type SignalModel struct {
Name string `json:"name"`
Type string `json:"type"`
Default string `json:"default"`
Doc string `json:"doc,omitempty"`
}
SignalModel is one client signal of a component.
type Symbol ¶
type Symbol struct {
File string
Line int
Col int
EndCol int
Text string
Type string
ObjectFile string
ObjectLine int
ObjectCol int
}
Symbol is one typed identifier in a .gx file (REQ-DEV-08). ObjectFile is the definition position, mapped through the //line directives.
type TransitionModel ¶
type TransitionModel struct {
// Name is the package-level value.
Name string `json:"name"`
// Base is the view-transition base name.
Base string `json:"base"`
// Key is the key type.
Key string `json:"key"`
}
TransitionModel is one gx.Transition value.