Documentation
¶
Index ¶
- Constants
- func List[F any, T any](list []F, convert func(F) T) []T
- func ListLazy[F any, T any](convert func(F) T) func(list []F) []T
- func ToInt[T ~int](a T) int
- func ToStrings[T ~string](a []T) []string
- type Alias
- type ArrayLiteralType
- type ArrayType
- type ArrowFunction
- type Bindings
- func (b *Bindings) Alias(alias *Alias) (*goja.Object, error)
- func (b *Bindings) Array(arrType ExpressionType) (*goja.Object, error)
- func (b *Bindings) ArrayLiteral(value *ArrayLiteralType) (*goja.Object, error)
- func (b *Bindings) ArrowFunction(af *ArrowFunction) (*goja.Object, error)
- func (b *Bindings) BooleanLiteral(value int) (*goja.Object, error)
- func (b *Bindings) CallExpression(expr *CallExpression) (*goja.Object, error)
- func (b *Bindings) CommentGojaObject(comments []SyntheticComment, object *goja.Object) (*goja.Object, error)
- func (b *Bindings) EnumDeclaration(e *Enum) (*goja.Object, error)
- func (b *Bindings) EnumMember(value *EnumMember) (*goja.Object, error)
- func (b *Bindings) FloatLiteral(value float64) (*goja.Object, error)
- func (b *Bindings) HeritageClause(clause *HeritageClause) (*goja.Object, error)
- func (b *Bindings) Identifier(name string) (*goja.Object, error)
- func (b *Bindings) IdentifierExpression(expr *IdentifierExpression) (*goja.Object, error)
- func (b *Bindings) ImportDeclaration(decl *ImportDeclaration) (*goja.Object, error)
- func (b *Bindings) ImportSpecifier(spec *ImportSpecifier) (*goja.Object, error)
- func (b *Bindings) Interface(ti *Interface) (*goja.Object, error)
- func (b *Bindings) LiteralKeyword(word *LiteralKeyword) (*goja.Object, error)
- func (b *Bindings) Modifier(name Modifier) (*goja.Object, error)
- func (b *Bindings) Null() (*goja.Object, error)
- func (b *Bindings) NumericLiteral(value int64) (*goja.Object, error)
- func (b *Bindings) ObjectLiteralExpression(expr *ObjectLiteralExpression) (*goja.Object, error)
- func (b *Bindings) OperatorNode(value *OperatorNodeType) (*goja.Object, error)
- func (b *Bindings) Parameter(p *Parameter) (*goja.Object, error)
- func (b *Bindings) PropertyAccessExpression(expr *PropertyAccessExpression) (*goja.Object, error)
- func (b *Bindings) PropertyAssignment(pa *PropertyAssignment) (*goja.Object, error)
- func (b *Bindings) PropertySignature(sig *PropertySignature) (*goja.Object, error)
- func (b *Bindings) Reference(ref *ReferenceType) (*goja.Object, error)
- func (b *Bindings) SerializeToTypescript(node *goja.Object) (string, error)
- func (b *Bindings) StringLiteral(value string) (*goja.Object, error)
- func (b *Bindings) ToTypescriptDeclarationNode(ety DeclarationType) (*goja.Object, error)
- func (b *Bindings) ToTypescriptExpressionNode(ety ExpressionType) (*goja.Object, error)
- func (b *Bindings) ToTypescriptNode(ety Node) (*goja.Object, error)
- func (b *Bindings) Tuple(length int, tupleType ExpressionType) (*goja.Object, error)
- func (b *Bindings) TypeIntersection(node *TypeIntersection) (*goja.Object, error)
- func (b *Bindings) TypeLiteralNode(node *TypeLiteralNode) (*goja.Object, error)
- func (b *Bindings) TypeParameter(ty *TypeParameter) (*goja.Object, error)
- func (b *Bindings) TypeQuery(tq *TypeQuery) (*goja.Object, error)
- func (b *Bindings) Union(ty *UnionType) (*goja.Object, error)
- func (b *Bindings) VariableDeclaration(decl *VariableDeclaration) (*goja.Object, error)
- func (b *Bindings) VariableDeclarationList(list *VariableDeclarationList) (*goja.Object, error)
- func (b *Bindings) VariableStatement(stmt *VariableStatement) (*goja.Object, error)
- type CallExpression
- type Commentable
- type DeclarationType
- type Enum
- type EnumMember
- type ExpressionType
- type ExpressionWithTypeArguments
- type HasSource
- type HeritageClause
- type HeritageType
- type Identifier
- type IdentifierExpression
- type ImportDeclaration
- type ImportSpecifier
- type Interface
- type LiteralKeyword
- type LiteralType
- type Modifier
- type Node
- type NodeFlags
- type Null
- type ObjectLiteralExpression
- type OperatorNodeType
- type Parameter
- type PropertyAccessExpression
- type PropertyAssignment
- type PropertySignature
- type ReferenceType
- type Source
- type SupportComments
- type SyntheticComment
- type TupleType
- type TypeIntersection
- type TypeLiteralNode
- type TypeParameter
- type TypeQuery
- type UnionType
- type VariableDeclaration
- type VariableDeclarationList
- type VariableStatement
Constants ¶
const ( ModifierAbstract = "AbstractKeyword" ModifierAccessor = "AccessorKeyword" ModifierAsync = "AsyncKeyword" ModifierConst = "ConstKeyword" ModifierDeclare = "DeclareKeyword" ModifierDefault = "DefaultKeyword" ModifierExport = "ExportKeyword" ModifierIn = "InKeyword" ModifierPrivate = "PrivateKeyword" ModifierProtected = "ProtectedKeyword" ModifierPublic = "PublicKeyword" ModifierReadonly = "ReadonlyKeyword" ModifierOut = "OutKeyword" ModifierOverride = "OverrideKeyword" ModifierStatic = "StaticKeyword" )
const (
NodeFlagsConstant = 2
)
Variables ¶
This section is empty.
Functions ¶
func List ¶
List is a helper function to reduce boilerplate when converting slices of database types to slices of codersdk types. Only works if the function takes a single argument.
Types ¶
type Alias ¶
type Alias struct {
Name Identifier
Modifiers []Modifier
Type ExpressionType
Parameters []*TypeParameter
SupportComments
Source
}
type ArrayLiteralType ¶
type ArrayLiteralType struct {
Elements []ExpressionType
}
func (ArrayLiteralType) String ¶
func (a ArrayLiteralType) String() string
type ArrayType ¶
type ArrayType struct {
Node ExpressionType
}
func Array ¶
func Array(node ExpressionType) *ArrayType
type ArrowFunction ¶ added in v1.7.1
type ArrowFunction struct {
Parameters []*Parameter
ReturnType ExpressionType
Body ExpressionType
}
ArrowFunction is `(parameters): returnType => body`. ReturnType may be nil to omit the annotation. Body is currently required to be a single expression; statement bodies are not yet modeled.
type Bindings ¶
type Bindings struct {
// contains filtered or unexported fields
}
func (*Bindings) ArrayLiteral ¶
func (b *Bindings) ArrayLiteral(value *ArrayLiteralType) (*goja.Object, error)
func (*Bindings) ArrowFunction ¶ added in v1.7.1
func (b *Bindings) ArrowFunction(af *ArrowFunction) (*goja.Object, error)
ArrowFunction builds `(parameters): returnType => body`.
func (*Bindings) BooleanLiteral ¶
func (*Bindings) CallExpression ¶ added in v1.7.1
func (b *Bindings) CallExpression(expr *CallExpression) (*goja.Object, error)
CallExpression builds `<expression>(args...)`.
func (*Bindings) CommentGojaObject ¶ added in v1.6.0
func (*Bindings) EnumDeclaration ¶ added in v1.3.0
func (*Bindings) EnumMember ¶ added in v1.3.0
func (b *Bindings) EnumMember(value *EnumMember) (*goja.Object, error)
func (*Bindings) FloatLiteral ¶
func (*Bindings) HeritageClause ¶
func (b *Bindings) HeritageClause(clause *HeritageClause) (*goja.Object, error)
func (*Bindings) IdentifierExpression ¶ added in v1.7.1
func (b *Bindings) IdentifierExpression(expr *IdentifierExpression) (*goja.Object, error)
IdentifierExpression builds the goja node for a value-position identifier such as `z` or `BaseSchema`.
func (*Bindings) ImportDeclaration ¶ added in v1.7.1
func (b *Bindings) ImportDeclaration(decl *ImportDeclaration) (*goja.Object, error)
ImportDeclaration builds the goja ImportDeclaration for a top-level `import { ... } from "module"` statement, or a side-effect import (`import "module"`) when decl.SideEffect is true.
func (*Bindings) ImportSpecifier ¶ added in v1.7.1
func (b *Bindings) ImportSpecifier(spec *ImportSpecifier) (*goja.Object, error)
ImportSpecifier builds the goja ImportSpecifier for a single named import.
When Alias is empty, this emits `Name`. When Alias is set, this emits `Name as Alias` by passing Name as the TypeScript propertyName and Alias as the local binding name.
func (*Bindings) LiteralKeyword ¶
func (b *Bindings) LiteralKeyword(word *LiteralKeyword) (*goja.Object, error)
func (*Bindings) NumericLiteral ¶
func (*Bindings) ObjectLiteralExpression ¶ added in v1.7.1
func (b *Bindings) ObjectLiteralExpression(expr *ObjectLiteralExpression) (*goja.Object, error)
ObjectLiteralExpression builds `{ k: v, ... }` in expression position.
func (*Bindings) OperatorNode ¶
func (b *Bindings) OperatorNode(value *OperatorNodeType) (*goja.Object, error)
func (*Bindings) Parameter ¶ added in v1.7.1
Parameter builds a single `name: type` arrow-function parameter.
func (*Bindings) PropertyAccessExpression ¶ added in v1.7.1
func (b *Bindings) PropertyAccessExpression(expr *PropertyAccessExpression) (*goja.Object, error)
PropertyAccessExpression builds `<expression>.<name>`.
func (*Bindings) PropertyAssignment ¶ added in v1.7.1
func (b *Bindings) PropertyAssignment(pa *PropertyAssignment) (*goja.Object, error)
PropertyAssignment builds `<name>: <initializer>` for an ObjectLiteralExpression child.
func (*Bindings) PropertySignature ¶
func (b *Bindings) PropertySignature(sig *PropertySignature) (*goja.Object, error)
func (*Bindings) SerializeToTypescript ¶
func (*Bindings) StringLiteral ¶
func (*Bindings) ToTypescriptDeclarationNode ¶
func (b *Bindings) ToTypescriptDeclarationNode(ety DeclarationType) (*goja.Object, error)
func (*Bindings) ToTypescriptExpressionNode ¶
func (b *Bindings) ToTypescriptExpressionNode(ety ExpressionType) (*goja.Object, error)
func (*Bindings) ToTypescriptNode ¶
func (*Bindings) TypeIntersection ¶ added in v1.6.0
func (b *Bindings) TypeIntersection(node *TypeIntersection) (*goja.Object, error)
func (*Bindings) TypeLiteralNode ¶ added in v1.6.0
func (b *Bindings) TypeLiteralNode(node *TypeLiteralNode) (*goja.Object, error)
func (*Bindings) TypeParameter ¶
func (b *Bindings) TypeParameter(ty *TypeParameter) (*goja.Object, error)
func (*Bindings) VariableDeclaration ¶
func (b *Bindings) VariableDeclaration(decl *VariableDeclaration) (*goja.Object, error)
func (*Bindings) VariableDeclarationList ¶
func (b *Bindings) VariableDeclarationList(list *VariableDeclarationList) (*goja.Object, error)
func (*Bindings) VariableStatement ¶
func (b *Bindings) VariableStatement(stmt *VariableStatement) (*goja.Object, error)
type CallExpression ¶ added in v1.7.1
type CallExpression struct {
Expression ExpressionType
Arguments []ExpressionType
}
CallExpression is `<expression>(args...)`. It composes with PropertyAccessExpression to build chained calls like `z.string().optional()`.
type Commentable ¶ added in v1.6.0
type Commentable interface {
AppendComment(comment SyntheticComment)
Comments() []SyntheticComment
}
Commentable indicates if the AST node supports adding comments. Any number of comments are supported and can be attached to a typescript AST node.
type DeclarationType ¶
type DeclarationType interface {
Node
// contains filtered or unexported methods
}
DeclarationType is any type that can exist at the top level of a AST. Meaning it can be serialized into valid Typescript.
type Enum ¶ added in v1.3.0
type Enum struct {
Name Identifier
Modifiers []Modifier
Members []*EnumMember
SupportComments
Source
}
type EnumMember ¶ added in v1.3.0
type EnumMember struct {
Name string
// Value is allowed to be nil, which results in `undefined`.
Value ExpressionType
SupportComments
}
type ExpressionType ¶
type ExpressionType interface {
Node
// contains filtered or unexported methods
}
ExpressionType
type ExpressionWithTypeArguments ¶
type ExpressionWithTypeArguments struct {
Expression ExpressionType
Arguments []ExpressionType
}
type HasSource ¶ added in v1.6.0
type HasSource interface {
SourceComment() (SyntheticComment, bool)
}
type HeritageClause ¶
type HeritageClause struct {
Token HeritageType
Args []ExpressionType
}
HeritageClause interface Foo extends Bar, Baz {}
func HeritageClauseExtends ¶
func HeritageClauseExtends(args ...ExpressionType) *HeritageClause
type HeritageType ¶
type HeritageType string
const ( HeritageTypeExtends HeritageType = "extends" HeritageTypeImplements HeritageType = "implements" )
type Identifier ¶
Identifier is a name given to a variable, function, class, etc. Identifiers should be unique within a package. Package information is included to help with disambiguation in the case of name collisions.
func (Identifier) GoName ¶
func (i Identifier) GoName() string
GoName should be a unique name for the identifier across all Go packages.
func (Identifier) PkgName ¶
func (i Identifier) PkgName() string
func (Identifier) Ref ¶
func (i Identifier) Ref() string
Ref returns the identifier reference to be used in the generated code. This is the identifier to be used in typescript, since all generated code lands in the same namespace.
func (Identifier) String ¶
func (i Identifier) String() string
type IdentifierExpression ¶ added in v1.7.1
type IdentifierExpression struct {
Name Identifier
}
IdentifierExpression is a value-position TypeScript identifier such as the `z` in `z.string()` or `BaseSchema` in `BaseSchema.extend({...})`.
It is distinct from bindings.Identifier despite the similar name. Identifier is parser-layer plumbing: a qualified-name handle (Name + Package + Prefix) used to resolve and disambiguate references across Go packages, and it is not itself a Node. IdentifierExpression is tree-layer plumbing: a Node that implements ExpressionType so callers can place a value-position identifier inside expression slots like CallExpression.Expression.
The Name field is itself an Identifier so cross-package prefixing flows through .Ref() the same way it does for ReferenceType.Name and VariableDeclaration.Name.
type ImportDeclaration ¶ added in v1.7.1
type ImportDeclaration struct {
// Module is the module specifier (the right-hand side of `from`).
Module string
// Named is the list of imported names. Order is preserved while merging;
// the final emitted order is sorted alphabetically in Serialize.
// Ignored when SideEffect is true.
Named []*ImportSpecifier
// IsTypeOnly emits `import type { ... }` instead of `import { ... }`.
// Ignored when SideEffect is true (`import type "x"` is not valid TS).
IsTypeOnly bool
// SideEffect emits the bare form `import "module"` with no import clause.
// When true, Named and IsTypeOnly are ignored.
SideEffect bool
SupportComments
Source
}
ImportDeclaration is a top-level ECMAScript import statement.
import { z } from "zod"
import { Foo as Bar } from "./schemas"
import type { Baz } from "./types"
import "polyfill"
Only the named-imports and side-effect forms are modeled. Default imports (`import foo from "x"`) and namespace imports (`import * as ns from "x"`) are not yet supported; add them if you need them.
type ImportSpecifier ¶ added in v1.7.1
ImportSpecifier is a single named import entry inside the braces of an `import { ... }` clause.
Name="foo", Alias="" -> foo Name="foo", Alias="bar" -> foo as bar IsTypeOnly=true -> type foo, type foo as bar
type Interface ¶
type Interface struct {
Name Identifier
Modifiers []Modifier
Fields []*PropertySignature
Parameters []*TypeParameter
Heritage []*HeritageClause
SupportComments
Source
}
type LiteralKeyword ¶
type LiteralKeyword string
const ( KeywordVoid LiteralKeyword = "VoidKeyword" KeywordAny LiteralKeyword = "AnyKeyword" KeywordBoolean LiteralKeyword = "BooleanKeyword" KeywordIntrinsic LiteralKeyword = "IntrinsicKeyword" KeywordNever LiteralKeyword = "NeverKeyword" KeywordNumber LiteralKeyword = "NumberKeyword" KeywordObject LiteralKeyword = "ObjectKeyword" KeywordString LiteralKeyword = "StringKeyword" KeywordSymbol LiteralKeyword = "SymbolKeyword" KeywordUndefined LiteralKeyword = "UndefinedKeyword" KeywordUnknown LiteralKeyword = "UnknownKeyword" KeywordBigInt LiteralKeyword = "BigIntKeyword" KeywordReadonly LiteralKeyword = "ReadonlyKeyword" KeywordUnique LiteralKeyword = "UniqueKeyword" KeywordKeyOf LiteralKeyword = "KeyOfKeyword" )
func ToTypescriptLiteralKeyword ¶
func ToTypescriptLiteralKeyword(word string) (LiteralKeyword, error)
func (LiteralKeyword) String ¶
func (k LiteralKeyword) String() string
type LiteralType ¶
type LiteralType struct {
Value any // should be some constant value
}
type ObjectLiteralExpression ¶ added in v1.7.1
type ObjectLiteralExpression struct {
Properties []*PropertyAssignment
}
ObjectLiteralExpression is `{ k: v, ... }` in expression position. It is distinct from TypeLiteralNode, which emits a TypeScript object type.
Only the PropertyAssignment form is modeled. ShorthandPropertyAssignment (`{ x }`), SpreadAssignment (`{ ...rest }`), MethodDeclaration, and accessor properties are not yet supported; add them if you need them.
type OperatorNodeType ¶
type OperatorNodeType struct {
Keyword LiteralKeyword
Type ExpressionType
}
func OperatorNode ¶
func OperatorNode(keyword LiteralKeyword, node ExpressionType) *OperatorNodeType
OperatorNode allows adding a keyword to a type Keyword must be "KeyOfKeyword" | "UniqueKeyword" | "ReadonlyKeyword"
func (OperatorNodeType) String ¶
func (o OperatorNodeType) String() string
type Parameter ¶ added in v1.7.1
type Parameter struct {
Name string
Type ExpressionType
}
Parameter is a single parameter in an ArrowFunction signature. Name is required; Type may be nil to omit the annotation.
type PropertyAccessExpression ¶ added in v1.7.1
type PropertyAccessExpression struct {
Expression ExpressionType
Name string
}
PropertyAccessExpression is `<expression>.<name>`, used to chain method names or member references such as `z.string` or `BaseSchema.extend`.
type PropertyAssignment ¶ added in v1.7.1
type PropertyAssignment struct {
Name string
Initializer ExpressionType
}
PropertyAssignment is `<name>: <initializer>` inside an ObjectLiteralExpression. It is a node but not an ExpressionType or a DeclarationType because it only appears as a child of ObjectLiteralExpression.
type PropertySignature ¶
type PropertySignature struct {
// Name is the field name
Name string
Modifiers []Modifier
QuestionToken bool
Type ExpressionType
SupportComments
}
PropertySignature is a field in an interface
func (PropertySignature) String ¶
func (f PropertySignature) String() string
type ReferenceType ¶
type ReferenceType struct {
Name Identifier `json:"name"`
// TODO: Generics
Arguments []ExpressionType `json:"arguments"`
}
ReferenceType can be used to reference another type by name
func Reference ¶
func Reference(name Identifier, args ...ExpressionType) *ReferenceType
func (ReferenceType) String ¶
func (r ReferenceType) String() string
type Source ¶
Source is the golang file that an entity is sourced from.
func (Source) SourceComment ¶ added in v1.6.0
func (s Source) SourceComment() (SyntheticComment, bool)
SourceComment returns a synthetic comment indicating the source file. If the source file is empty, the second return is false.
type SupportComments ¶ added in v1.6.0
type SupportComments struct {
// contains filtered or unexported fields
}
func (*SupportComments) AppendComment ¶ added in v1.6.0
func (s *SupportComments) AppendComment(comment SyntheticComment)
func (*SupportComments) AppendComments ¶ added in v1.6.0
func (s *SupportComments) AppendComments(comments []SyntheticComment)
func (*SupportComments) Comments ¶ added in v1.6.0
func (s *SupportComments) Comments() []SyntheticComment
func (*SupportComments) LeadingComment ¶ added in v1.6.0
func (s *SupportComments) LeadingComment(text string)
type SyntheticComment ¶ added in v1.6.0
type SyntheticComment struct {
Leading bool
SingleLine bool
Text string
TrailingNewLine bool
// DoNotFormat indicates this comment should not be attempted to be reformatted.
// Guts will attempt to format comments into JSDoc style comments where possible.
DoNotFormat bool
}
SyntheticComment is the state of a comment for a given AST node. See the compiler for how these are serialized.
type TupleType ¶ added in v1.2.0
type TupleType struct {
// TODO: Technically tuples can be heterogeneous, but golang does not really
// support that. So just assume that all elements are the same type.
Node ExpressionType
Length int
}
func HomogeneousTuple ¶ added in v1.2.0
func HomogeneousTuple(length int, node ExpressionType) *TupleType
type TypeIntersection ¶ added in v1.6.0
type TypeIntersection struct {
Types []ExpressionType
}
type TypeLiteralNode ¶ added in v1.6.0
type TypeLiteralNode struct {
Members []*PropertySignature
}
TypeLiteralNode represents an object type literal like { name: string }
type TypeParameter ¶
type TypeParameter struct {
Name Identifier
Modifiers []Modifier
Type ExpressionType
// DefaultType does not map to any Golang concepts and will never be
// used.
DefaultType ExpressionType
}
TypeParameter are generics in Go Foo[T comparable] -> - name: T - Modifiers: [] - Type: Comparable - DefaultType: nil
func Simplify ¶
func Simplify(p []*TypeParameter) ([]*TypeParameter, error)
Simplify removes duplicate type parameters
func (TypeParameter) String ¶
func (p TypeParameter) String() string
type TypeQuery ¶ added in v1.7.1
type TypeQuery struct {
Name Identifier
}
TypeQuery is `typeof <name>`. It appears in type position, typically as a generic argument such as the `typeof FooSchema` inside `z.infer<typeof FooSchema>`.
Name is an Identifier so cross-package prefixing flows through .Ref(), matching the rest of the AST. Without this, a TypeQuery for a prefixed declaration would emit `typeof Foo` while the matching value-position IdentifierExpression emits `ExternalFoo`, and the two would not line up.
type UnionType ¶
type UnionType struct {
Types []ExpressionType
}
func Union ¶
func Union(types ...ExpressionType) *UnionType
type VariableDeclaration ¶
type VariableDeclaration struct {
Name Identifier
ExclamationMark bool
Type ExpressionType
Initializer ExpressionType
}
func (VariableDeclaration) String ¶
func (v VariableDeclaration) String() string
type VariableDeclarationList ¶
type VariableDeclarationList struct {
Declarations []*VariableDeclaration
Flags NodeFlags
}
type VariableStatement ¶
type VariableStatement struct {
Modifiers []Modifier
Declarations *VariableDeclarationList
Source
SupportComments
}
VariableStatement is a top level declaration of a variable var foo: string = "bar" const foo: string = "bar"