Package hydra.typeScript
Interface Coder
-
public interface CoderTypeScript code generator: emits TypeScript type declarations from Hydra modules
-
-
Method Summary
Static Methods Modifier and Type Method Description static <T0> Either<T0,FunctionStructure<Graph>>analyzeTypeScriptFunction(InferenceContext cx, Graph g, Term term)Analyze a term as a TypeScript function, using the Graph as the analysis environmentstatic java.util.List<Name>collectForallParams(Type t)Collect the bound parameter names from a chain of nested foralls, in outer-to-inner order; stops at the first non-forall typestatic java.util.Set<Name>collectImports(ModuleName currentNs, Type t)Collect the names of every type referenced in a type tree that belongs to a different namespace than the current module, for computing the imports needed at the top of the emitted .ts filestatic java.util.Set<Name>collectInnerTypeImports(ModuleName currentNs, Term term)Collect type imports referenced inside a term tree (lambda bodies, type applications, let-binding schemes), supplementing the top-level typeScheme walkstatic java.util.Set<Name>collectTermImports(ModuleName currentNs, Term t)Like collectImports but walks a Term, gathering free term-level variables that resolve to a different modulestatic java.util.Set<Name>eagerFreeVariablesInTerm(Term term)Find the free variables of a term that are referenced eagerly, i.e.static <T0> java.util.Set<Name>eagerFreeVariablesInTerm_dfltVars(java.util.function.Function<Term,java.util.List<Term>> hydra_rewriting_subterms, java.util.function.Function<Term,java.util.Set<Name>> hydra_typeScript_coder_eagerFreeVariablesInTerm, Term term, T0 ignored)static StatementencodeBindingAsStatement(InferenceContext cx, Graph g, ModuleName currentNs, Binding b)Encode a let-binding as a TS statement inside an enclosing function body, hoisting lambda-valued bindings as nested function declarationsstatic ExpressionencodeLazyCall(InferenceContext cx, Graph g, ModuleName currentNs, Term headTerm, java.util.List<Term> args, java.util.List<java.lang.Boolean> lazyFlags)Emit a fully-applied primitive call with selected arguments wrapped in thunks, per a parallel list of laziness flagsstatic ExpressionencodeLiteral(Literal lit)Render a Hydra literal as a TypeScript expressionstatic TypeExpressionencodeLiteralType(LiteralType lt)Map a Hydra literal type to a TypeScript type expressionstatic <T0,T1>
PatternencodeParam(T0 cx, T1 g, ModuleName currentNs, Name pname, Type dom)Build a TS function parameter as a pattern, typed unless the domain is the analyze pass's untyped-variable sentinelstatic ExpressionencodeTerm(InferenceContext cx, Graph g, ModuleName currentNs, Term term)Render a Hydra term as a TypeScript expressionstatic <T0> Either<T0,FunctionStructure<Graph>>encodeTerm_fsLE(InferenceContext cx, Graph g, Term lamTerm)static Pair<Optional<java.lang.String>,ModuleItem>encodeTermDefinition(InferenceContext cx, Graph g, ModuleName currentNs, TermDefinition td)Render a Hydra term definition as a TypeScript module itemstatic <T0,T1,T2>
Either<T2,TypeExpression>encodeType(T0 cx, T1 g, ModuleName currentNs, Type t)Map a Hydra type to a TypeScript type expressionstatic <T0> Either<Error_,Pair<Optional<java.lang.String>,ModuleItem>>encodeTypeDefinition(T0 cx, Graph g, ModuleName currentNs, TypeDefinition tdef)Encode a Hydra type definition as an interface declaration, a discriminated-union type alias, or a plain type aliasstatic <T0,T1>
TypeExpressionencodeTypeOrAny(T0 cx, T1 g, ModuleName currentNs, Type typ)Try to encode a Hydra type as a TS type expression, falling back to any if encodeType failsstatic java.util.Set<Name>filterNonLocalNames(ModuleName currentNs, java.util.Set<Name> names)Keep only names whose module differs from the current module'sstatic Pair<Term,java.util.List<Term>>flattenApplication(Term t)Walk an application spine, returning the innermost head term and its arguments in application orderstatic TermforceLazyRefs(java.util.Set<Name> targets0, Term term0)Rewrite free occurrences of the given target names from x to x(undefined), forcing a thunked binding created by thunkLazyLetstatic FunctionDeclarationfunctionDeclarationFromTerm(InferenceContext cx, Graph g, ModuleName currentNs, java.lang.String lname, Term term, Optional<Type> _mScheme)Build a TS function declaration from a Hydra term by peeling lambdas and let bindings into explicit parameters, statements, and a bodystatic <T0> Either<T0,FunctionStructure<Graph>>functionDeclarationFromTerm_fsE(InferenceContext cx, Graph g, Term term)static java.lang.StringimportsToText(java.util.Set<java.lang.String> overlaySubs, java.lang.String kind, ModuleName currentNs, java.util.Set<Name> names)Render a set of qualified names as TypeScript import statements grouped by source modulestatic java.util.List<java.lang.Boolean>lazyFlagsForPrimitive(Graph g, Name name)Look up a primitive by name and return its per-parameter laziness flags in parameter orderstatic java.lang.BooleanletBindingIsThunkCandidate(Binding b)True when a let-binding's value should be thunked to preserve Haskell's lazy evaluation semanticsstatic Optional<DocumentationComment>mkDocComment(Optional<java.lang.String> mdesc)Build a documentation comment from an optional description string, or nothing if the description is missing or emptystatic Either<Error_,java.util.Map<java.lang.String,java.lang.String>>moduleToTypeScript(java.util.Set<java.lang.String> overlaySubs, Module mod, java.util.List<Definition> defs, InferenceContext cx, Graph g)Convert a Hydra module to a map from .ts file path to TypeScript sourcestatic java.lang.StringprintInterfaceDeclaration(InterfaceDeclaration decl)Render an interface declarationstatic java.lang.StringprintLiteral(Literal lit)Render a TypeScript literal as source textstatic java.lang.StringprintModuleItem(ModuleItem mi)Render a top-level module itemstatic java.lang.StringprintPropertySignature(PropertySignature ps)Render a property signature, with an optional JSDoc comment prependedstatic java.lang.StringprintTypeAliasDeclaration(TypeAliasDeclaration decl)Render a type alias declarationstatic java.lang.StringprintTypeExpression(TypeExpression t)Render a TypeScript type expression as source textstatic java.lang.StringprintTypeParameter(TypeParameter tp)Render one generic type parameter, with an optional extends constraintstatic java.lang.StringprintTypeParameterList(java.util.List<TypeParameter> tps)Render a generic parameter list, or the empty string when there are no parametersstatic java.lang.StringsanitizeParamName(Name n)Sanitize a Hydra parameter name into a valid, non-reserved TS identifierstatic java.util.List<Binding>sortBindingsTopologically(java.util.List<Binding> bindings)Reorder let-bindings so each appears after the sibling bindings it depends on, avoiding JS temporal-dead-zone errorsstatic <T0> java.util.List<TermDefinition>sortTermDefsTopologically(T0 currentNs, java.util.List<TermDefinition> tdefs)Reorder term definitions so each appears after the intra-module definitions it depends onstatic TypestripForalls(Type t)Strip leading forall quantifiers from a type, returning the innermost bodystatic Optional<Name>termHeadVariable(Term t)If a term reduces, through annotations and type applications, to a variable reference, return its namestatic Pair<java.util.List<Binding>,Term>thunkLazyBindings(java.util.List<Binding> bindings, Term body)The bindings-and-body core of thunkLazyLet, shared by the Term_let encoder and the lifted-binding path in functionDeclarationFromTermstatic LetthunkLazyLet(Let lt)Thunk every let-binding for which letBindingIsThunkCandidate holds, and rewrite forced references to it accordinglystatic ExpressiontsArray(java.util.List<Expression> elems)An array expression: [e1, e2, ...]static ExpressiontsArrow(java.util.List<java.lang.String> params, Expression body)An untyped arrow function with an expression bodystatic ExpressiontsArrowTyped(java.util.List<Pattern> patterns, Expression body)A typed arrow function with typed parameter patternsstatic ExpressiontsAsAny(Expression e)Wrap an expression in a TypeScript 'as any' caststatic ExpressiontsCall(Expression callee, java.util.List<Expression> args)A call expression, parenthesizing the callee when it's an arrow function or other ambiguous call targetstatic ExpressiontsCond(Expression test, Expression cons, Expression alt)A conditional (ternary) expressionstatic java.lang.StringtsDocEntityRef(EntityReference v1)Render aEntityReferenceas TSDoc link syntaxstatic <T0> T0tsEnvGetGraph(T0 g)Identity getter for analyzeFunctionTerm when the environment is a Graphstatic <T0,T1>
T0tsEnvSetGraph(T0 newG, T1 _old)Setter for analyzeFunctionTerm when the environment is a Graphstatic java.lang.StringtsEscapeString(java.lang.String s)Escape a Hydra string for embedding as a double-quoted TypeScript string literalstatic ExpressiontsExprIdent(java.lang.String s)A bare identifier expressionstatic ExpressiontsExprStr(java.lang.String s)A string-literal expressionstatic IdentifiertsIdent(java.lang.String s)Wrap a string into a TypeScript identifierstatic ExpressiontsMember(Expression obj, java.lang.String prop)A member-access expression: obj.propstatic TypeExpressiontsNamedType(java.lang.String n)A bare named type referencestatic ExpressiontsNew(Expression callee, java.util.List<Expression> args)A new-expression: new C(args)static ExpressiontsObject(java.util.List<Pair<java.lang.String,Expression>> props)An object literal with identifier-style keysstatic TypeParametertsParam(java.lang.String n)A generic type parameter with no constraintstatic TypeExpressiontsParamApp1(java.lang.String n, TypeExpression arg)A parameterized type with one type argumentstatic TypeExpressiontsParamApp2(java.lang.String n, TypeExpression a, TypeExpression b)A parameterized type with two type argumentsstatic ExpressiontsParen(Expression e)Wrap an expression in parentheses when its serialized form needs groupingstatic PropertySignaturetsPropSig(java.lang.String name, java.lang.Boolean optional, TypeExpression typ)A readonly property signature, with the name sanitized for TS reserved wordsstatic PropertySignaturetsPropSigWithDoc(java.lang.String name, java.lang.Boolean optional, TypeExpression typ, Optional<DocumentationComment> mcomments)A readonly property signature with an optional JSDoc comment above the property linestatic TypeExpressiontsReadonlyMap(TypeExpression k, TypeExpression v)A ReadonlyMap type with the given key and value typesstatic TypeExpressiontsReadonlySet(TypeExpression t)A ReadonlySet type with the given element typestatic TypeExpressiontsTuple(java.util.List<TypeExpression> ts)A tuple typestatic PatterntsTypedIdent(java.lang.String name, TypeExpression typ)A typed-identifier pattern for a function parameter with a known domain typestatic ExpressiontsUndefined()The undefined value as an expression
-
-
-
Method Detail
-
analyzeTypeScriptFunction
static <T0> Either<T0,FunctionStructure<Graph>> analyzeTypeScriptFunction(InferenceContext cx, Graph g, Term term)
Analyze a term as a TypeScript function, using the Graph as the analysis environment
-
collectForallParams
static java.util.List<Name> collectForallParams(Type t)
Collect the bound parameter names from a chain of nested foralls, in outer-to-inner order; stops at the first non-forall type
-
collectImports
static java.util.Set<Name> collectImports(ModuleName currentNs, Type t)
Collect the names of every type referenced in a type tree that belongs to a different namespace than the current module, for computing the imports needed at the top of the emitted .ts file
-
collectInnerTypeImports
static java.util.Set<Name> collectInnerTypeImports(ModuleName currentNs, Term term)
Collect type imports referenced inside a term tree (lambda bodies, type applications, let-binding schemes), supplementing the top-level typeScheme walk
-
collectTermImports
static java.util.Set<Name> collectTermImports(ModuleName currentNs, Term t)
Like collectImports but walks a Term, gathering free term-level variables that resolve to a different module
-
eagerFreeVariablesInTerm
static java.util.Set<Name> eagerFreeVariablesInTerm(Term term)
Find the free variables of a term that are referenced eagerly, i.e. outside of any nested lambda body; used to break false dependency cycles caused by DSL-level thunks such as hydra.parsers.lazy
-
eagerFreeVariablesInTerm_dfltVars
static <T0> java.util.Set<Name> eagerFreeVariablesInTerm_dfltVars(java.util.function.Function<Term,java.util.List<Term>> hydra_rewriting_subterms, java.util.function.Function<Term,java.util.Set<Name>> hydra_typeScript_coder_eagerFreeVariablesInTerm, Term term, T0 ignored)
-
encodeBindingAsStatement
static Statement encodeBindingAsStatement(InferenceContext cx, Graph g, ModuleName currentNs, Binding b)
Encode a let-binding as a TS statement inside an enclosing function body, hoisting lambda-valued bindings as nested function declarations
-
encodeLazyCall
static Expression encodeLazyCall(InferenceContext cx, Graph g, ModuleName currentNs, Term headTerm, java.util.List<Term> args, java.util.List<java.lang.Boolean> lazyFlags)
Emit a fully-applied primitive call with selected arguments wrapped in thunks, per a parallel list of laziness flags
-
encodeLiteral
static Expression encodeLiteral(Literal lit)
Render a Hydra literal as a TypeScript expression
-
encodeLiteralType
static TypeExpression encodeLiteralType(LiteralType lt)
Map a Hydra literal type to a TypeScript type expression
-
encodeParam
static <T0,T1> Pattern encodeParam(T0 cx, T1 g, ModuleName currentNs, Name pname, Type dom)
Build a TS function parameter as a pattern, typed unless the domain is the analyze pass's untyped-variable sentinel
-
encodeTerm
static Expression encodeTerm(InferenceContext cx, Graph g, ModuleName currentNs, Term term)
Render a Hydra term as a TypeScript expression
-
encodeTermDefinition
static Pair<Optional<java.lang.String>,ModuleItem> encodeTermDefinition(InferenceContext cx, Graph g, ModuleName currentNs, TermDefinition td)
Render a Hydra term definition as a TypeScript module item
-
encodeTerm_fsLE
static <T0> Either<T0,FunctionStructure<Graph>> encodeTerm_fsLE(InferenceContext cx, Graph g, Term lamTerm)
-
encodeType
static <T0,T1,T2> Either<T2,TypeExpression> encodeType(T0 cx, T1 g, ModuleName currentNs, Type t)
Map a Hydra type to a TypeScript type expression
-
encodeTypeDefinition
static <T0> Either<Error_,Pair<Optional<java.lang.String>,ModuleItem>> encodeTypeDefinition(T0 cx, Graph g, ModuleName currentNs, TypeDefinition tdef)
Encode a Hydra type definition as an interface declaration, a discriminated-union type alias, or a plain type alias
-
encodeTypeOrAny
static <T0,T1> TypeExpression encodeTypeOrAny(T0 cx, T1 g, ModuleName currentNs, Type typ)
Try to encode a Hydra type as a TS type expression, falling back to any if encodeType fails
-
filterNonLocalNames
static java.util.Set<Name> filterNonLocalNames(ModuleName currentNs, java.util.Set<Name> names)
Keep only names whose module differs from the current module's
-
flattenApplication
static Pair<Term,java.util.List<Term>> flattenApplication(Term t)
Walk an application spine, returning the innermost head term and its arguments in application order
-
forceLazyRefs
static Term forceLazyRefs(java.util.Set<Name> targets0, Term term0)
Rewrite free occurrences of the given target names from x to x(undefined), forcing a thunked binding created by thunkLazyLet
-
functionDeclarationFromTerm
static FunctionDeclaration functionDeclarationFromTerm(InferenceContext cx, Graph g, ModuleName currentNs, java.lang.String lname, Term term, Optional<Type> _mScheme)
Build a TS function declaration from a Hydra term by peeling lambdas and let bindings into explicit parameters, statements, and a body
-
functionDeclarationFromTerm_fsE
static <T0> Either<T0,FunctionStructure<Graph>> functionDeclarationFromTerm_fsE(InferenceContext cx, Graph g, Term term)
-
importsToText
static java.lang.String importsToText(java.util.Set<java.lang.String> overlaySubs, java.lang.String kind, ModuleName currentNs, java.util.Set<Name> names)Render a set of qualified names as TypeScript import statements grouped by source module
-
lazyFlagsForPrimitive
static java.util.List<java.lang.Boolean> lazyFlagsForPrimitive(Graph g, Name name)
Look up a primitive by name and return its per-parameter laziness flags in parameter order
-
letBindingIsThunkCandidate
static java.lang.Boolean letBindingIsThunkCandidate(Binding b)
True when a let-binding's value should be thunked to preserve Haskell's lazy evaluation semantics
-
mkDocComment
static Optional<DocumentationComment> mkDocComment(Optional<java.lang.String> mdesc)
Build a documentation comment from an optional description string, or nothing if the description is missing or empty
-
moduleToTypeScript
static Either<Error_,java.util.Map<java.lang.String,java.lang.String>> moduleToTypeScript(java.util.Set<java.lang.String> overlaySubs, Module mod, java.util.List<Definition> defs, InferenceContext cx, Graph g)
Convert a Hydra module to a map from .ts file path to TypeScript source
-
printInterfaceDeclaration
static java.lang.String printInterfaceDeclaration(InterfaceDeclaration decl)
Render an interface declaration
-
printLiteral
static java.lang.String printLiteral(Literal lit)
Render a TypeScript literal as source text
-
printModuleItem
static java.lang.String printModuleItem(ModuleItem mi)
Render a top-level module item
-
printPropertySignature
static java.lang.String printPropertySignature(PropertySignature ps)
Render a property signature, with an optional JSDoc comment prepended
-
printTypeAliasDeclaration
static java.lang.String printTypeAliasDeclaration(TypeAliasDeclaration decl)
Render a type alias declaration
-
printTypeExpression
static java.lang.String printTypeExpression(TypeExpression t)
Render a TypeScript type expression as source text
-
printTypeParameter
static java.lang.String printTypeParameter(TypeParameter tp)
Render one generic type parameter, with an optional extends constraint
-
printTypeParameterList
static java.lang.String printTypeParameterList(java.util.List<TypeParameter> tps)
Render a generic parameter list, or the empty string when there are no parameters
-
sanitizeParamName
static java.lang.String sanitizeParamName(Name n)
Sanitize a Hydra parameter name into a valid, non-reserved TS identifier
-
sortBindingsTopologically
static java.util.List<Binding> sortBindingsTopologically(java.util.List<Binding> bindings)
Reorder let-bindings so each appears after the sibling bindings it depends on, avoiding JS temporal-dead-zone errors
-
sortTermDefsTopologically
static <T0> java.util.List<TermDefinition> sortTermDefsTopologically(T0 currentNs, java.util.List<TermDefinition> tdefs)
Reorder term definitions so each appears after the intra-module definitions it depends on
-
stripForalls
static Type stripForalls(Type t)
Strip leading forall quantifiers from a type, returning the innermost body
-
termHeadVariable
static Optional<Name> termHeadVariable(Term t)
If a term reduces, through annotations and type applications, to a variable reference, return its name
-
thunkLazyBindings
static Pair<java.util.List<Binding>,Term> thunkLazyBindings(java.util.List<Binding> bindings, Term body)
The bindings-and-body core of thunkLazyLet, shared by the Term_let encoder and the lifted-binding path in functionDeclarationFromTerm
-
thunkLazyLet
static Let thunkLazyLet(Let lt)
Thunk every let-binding for which letBindingIsThunkCandidate holds, and rewrite forced references to it accordingly
-
tsArray
static Expression tsArray(java.util.List<Expression> elems)
An array expression: [e1, e2, ...]
-
tsArrow
static Expression tsArrow(java.util.List<java.lang.String> params, Expression body)
An untyped arrow function with an expression body
-
tsArrowTyped
static Expression tsArrowTyped(java.util.List<Pattern> patterns, Expression body)
A typed arrow function with typed parameter patterns
-
tsAsAny
static Expression tsAsAny(Expression e)
Wrap an expression in a TypeScript 'as any' cast
-
tsCall
static Expression tsCall(Expression callee, java.util.List<Expression> args)
A call expression, parenthesizing the callee when it's an arrow function or other ambiguous call target
-
tsCond
static Expression tsCond(Expression test, Expression cons, Expression alt)
A conditional (ternary) expression
-
tsDocEntityRef
static java.lang.String tsDocEntityRef(EntityReference v1)
Render aEntityReferenceas TSDoc link syntax
-
tsEnvGetGraph
static <T0> T0 tsEnvGetGraph(T0 g)
Identity getter for analyzeFunctionTerm when the environment is a Graph
-
tsEnvSetGraph
static <T0,T1> T0 tsEnvSetGraph(T0 newG, T1 _old)Setter for analyzeFunctionTerm when the environment is a Graph
-
tsEscapeString
static java.lang.String tsEscapeString(java.lang.String s)
Escape a Hydra string for embedding as a double-quoted TypeScript string literal
-
tsExprIdent
static Expression tsExprIdent(java.lang.String s)
A bare identifier expression
-
tsExprStr
static Expression tsExprStr(java.lang.String s)
A string-literal expression
-
tsIdent
static Identifier tsIdent(java.lang.String s)
Wrap a string into a TypeScript identifier
-
tsMember
static Expression tsMember(Expression obj, java.lang.String prop)
A member-access expression: obj.prop
-
tsNamedType
static TypeExpression tsNamedType(java.lang.String n)
A bare named type reference
-
tsNew
static Expression tsNew(Expression callee, java.util.List<Expression> args)
A new-expression: new C(args)
-
tsObject
static Expression tsObject(java.util.List<Pair<java.lang.String,Expression>> props)
An object literal with identifier-style keys
-
tsParam
static TypeParameter tsParam(java.lang.String n)
A generic type parameter with no constraint
-
tsParamApp1
static TypeExpression tsParamApp1(java.lang.String n, TypeExpression arg)
A parameterized type with one type argument
-
tsParamApp2
static TypeExpression tsParamApp2(java.lang.String n, TypeExpression a, TypeExpression b)
A parameterized type with two type arguments
-
tsParen
static Expression tsParen(Expression e)
Wrap an expression in parentheses when its serialized form needs grouping
-
tsPropSig
static PropertySignature tsPropSig(java.lang.String name, java.lang.Boolean optional, TypeExpression typ)
A readonly property signature, with the name sanitized for TS reserved words
-
tsPropSigWithDoc
static PropertySignature tsPropSigWithDoc(java.lang.String name, java.lang.Boolean optional, TypeExpression typ, Optional<DocumentationComment> mcomments)
A readonly property signature with an optional JSDoc comment above the property line
-
tsReadonlyMap
static TypeExpression tsReadonlyMap(TypeExpression k, TypeExpression v)
A ReadonlyMap type with the given key and value types
-
tsReadonlySet
static TypeExpression tsReadonlySet(TypeExpression t)
A ReadonlySet type with the given element type
-
tsTuple
static TypeExpression tsTuple(java.util.List<TypeExpression> ts)
A tuple type
-
tsTypedIdent
static Pattern tsTypedIdent(java.lang.String name, TypeExpression typ)
A typed-identifier pattern for a function parameter with a known domain type
-
tsUndefined
static Expression tsUndefined()
The undefined value as an expression
-
-