velt/generate
API referenceWhat a source generator is written with. A library declares a generic function without a body, and a generator, a Velt function that reads a description of the types, writes the body for each type the program uses the function with. For a walkthrough, read the guide.
Exports
s as a double-quoted Velt (and JavaScript) string literal.serveThe host's loop (the compiler builds a program whose main calls it with the generators it needs): reads the requests from standard input, runs each generator, and writes the bodies, the further instances they ask for and their diagnostics to standard output.EnumMemberA member of an enum: its name and its value as JavaScript source.FieldA field of an object, class or struct type.ModelEvery type a request reaches.OutputWhere a generator writes the instance's body, helpers and diagnostics.InstanceRequestWhat the compiler asks a generator for: the body of one instance.MethodA method of a class that std's operations call by name (toJSON, toString, the inspect hook keyed by Symbol.for("nodejs.util.inspect.custom")), when it can be called that way: its name, how many parameters it takes and its result type.SessionOne run of the host: the model, the instances generated and queued, the output.TypeOne type of the model: what a generator may know about it.GeneratorEntryA generated function the host can generate: its generator's key, its name, the generator.Re-exported from velt/generate/output
function quote(s: string): strings as a double-quoted Velt (and JavaScript) string literal.
Re-exported from velt/generate/serve
function serve(generators: GeneratorEntry[]): voidThe host's loop (the compiler builds a program whose main calls it with the generators it
needs): reads the requests from standard input, runs each generator, and writes the bodies,
the further instances they ask for and their diagnostics to standard output.
Re-exported from velt/generate/enum_member
A member of an enum: its name and its value as JavaScript source.
Used byType.members
name: string = ""No doc comment yetvalue: string = ""No doc comment yetRe-exported from velt/generate/field
A field of an object, class or struct type.
Used byType.fields
name: string = ""No doc comment yettype: i64 = 0No doc comment yetoptional: bool = falseNo doc comment yetreadonly: bool = falseNo doc comment yetisPrivate: bool = falseprivate, or an ES private field (#x, whose name keeps the #).
isEsPrivate: bool = falseNo doc comment yethasDefault: bool = falseNo doc comment yetisGetter: bool = falseAn object literal's getter (get k() { … }): type is what it returns.
stdPrivate: bool = falseA private field a std type, or a type of a package with a native library, declares (its runtime state: a handle), also in a class extending it.
via: i64 = -1A field the type's objects hold for an extension (interface E extends T with optional
fields, whose objects are T's): code reads it from the value seen as type via (-1:
a field of the type itself).
at: i64 = -1No doc comment yetRe-exported from velt/generate/model
Every type a request reaches.
Re-exported from velt/generate/output
Where a generator writes the instance's body, helpers and diagnostics.
Used byGeneratorEntry
constructor(session: Session, name: string)No doc comment yetbody(code: string): voidAppends code to the body.
line(code: string): voidAppends one line of code to the body.
helper(code: string): voidAdds a top-level declaration (a helper function or constant) next to the instance.
fresh(base: string): stringA name for a helper that no other instance uses.
instance(fn: string, args: i64[]): stringThe name of the function generated for instance fn<args…> of a generated function of
the same generator module (generated in the same run when it is new): code calls it with
the arguments fn takes.
typeName(id: i64): stringHow generated code writes model type id.
literal(s: string): strings as a Velt string literal.
error(message: string, at: i64, labels: [i64, string][] = []): voidReports an error at at (a type's or a field's at; -1: at each call that needs the
instance). The first line of message is the error, each further line a note; labels
point at other places of the model ([at, text], such as a field's declaration).
takeBody(): stringThe body written so far.
takeHelpers(): stringNo doc comment yetRe-exported from velt/generate/instance_request
What the compiler asks a generator for: the body of one instance.
Used byGeneratorEntry
Re-exported from velt/generate/method
A method of a class that std's operations call by name (toJSON, toString, the inspect
hook keyed by Symbol.for("nodejs.util.inspect.custom")),
when it can be called that way: its name, how many parameters it takes and its result type.
Used byType.methods
name: string = ""No doc comment yetparams: i64 = 0No doc comment yetret: i64 = 0No doc comment yetRe-exported from velt/generate/session
One run of the host: the model, the instances generated and queued, the output.
Parameter ofOutput.constructor
prefix: string = ""No doc comment yetkey: string = ""The generator key of the instance being generated.
names: Map<string, string> = new Map()No doc comment yetparams: Map<string, string[]> = new Map()No doc comment yetqueue: Pending[] = []No doc comment yetnext: i64 = 0No doc comment yetinstance(fn: string, args: i64[]): stringNo doc comment yetreport(kind: string, name: string, at: i64, message: string): voidNo doc comment yetRe-exported from velt/generate/type
One type of the model: what a generator may know about it.
Returned byModel.typeUsed byModel.types
id: i64 = 0No doc comment yetkind: string = ""number, boolean, string, void, never, symbol, literal, enum, array,
tuple, option, union, object, class, struct, interface, map, record,
set, shared, date, function (also a function with properties), generator, any,
unknown, promise or opaque.
display: string = ""As messages show it (User, number[]).
qualifiedName: string = ""<module path>::<name> of a declared type, else "".
spelling: string = ""How generated code writes the type ("" when it cannot be named).
repr: string = ""f64, i32, … for numbers.
args: i64[] = []Element (array, set), payload (option, promise, shared), key and value (map,
record), tuple elements or type arguments, by kind.
arms: i64[] = []The members of a union, in order.
fields: Field[] = []Fields of objects, classes and structs, inherited ones first.
base: i64 = -1A class's base class (-1: none) and the classes of the program that extend it.
subclasses: i64[] = []No doc comment yetmembers: EnumMember[] = []Members of an enum.
literal: string = ""The value of a literal type as JavaScript source ("a", 1, true); a string is also its
JSON text. For an option: what it holds when empty (null, undefined or null | undefined).
methods: Method[] = []The methods std's operations call by name (toJSON, toString, the inspect hook keyed by
Symbol.for("nodejs.util.inspect.custom")).
mayBeProxy: bool = falseWhether a value of this type may be a typed Proxy standing for it (a class or object type
in a program that uses Proxy): only then does a generator need a path for proxies.
hasProperties: bool = falseA function with properties (Object.assign(f, { … })).
classValue: bool = falseA class used as a value (typeof C: its constructor and statics).
dynamic: bool = falseA dynamic type: any, unknown, and also object and Function (kind interface), whose
values say at run time what they hold.
privateField: string = ""For a std type, or a type of a package with a native library, whose structure the model
does not show (opaque, set): the private field that keeps its state (a runtime
handle), else "".
ctorAccess: string = ""A struct's or class's constructor when it is not public (private, protected), else "":
code that builds a value without running the constructor (__construct) bypasses it.
factories: string[] = []The public static methods of a class that return one (its factories), sorted.
at: i64 = -1Where it is declared, for Output.error (-1: nowhere).
Re-exported from velt/generate/serve
type GeneratorEntry = { key: string; fn: string; run: (req: InstanceRequest, out: Output) => void throws Error }A generated function the host can generate: its generator's key, its name, the generator.
Parameter ofserve