@rekajs/types
Package for creating and interacting with Reka data types (ie: the AST and the View)
Installation
npm install @rekajs/types
Creating a new Type
To create a new type, use the builder function which will return a new Type instance:
tsximport * as t from '@rekajs/types';// Using the builder functionconst literal = t.literal({value: 0});// or manually: new Literal({ value: 0 })
Checking an instance's type
tsximport * as t from '@rekajs/types';const literal = t.literal({ value : 0 });console.log(literal instanceof t.Literal); // trueconsole.log(literal instanceof t.Expression); // trueconsole.log(literal instanceof t.RekaComponent); // false
API
State
Class- 
program:
Program - 
extensions:
Record<string,ExtensionState> 
ASTNode
Class- 
meta:
Record<string,any> 
Program
Class- 
globals:
Array<Val> - 
components:
Array<RekaComponent> 
Kind
ClassAnyKind
ClassStringKind
ClassNumberKind
Class- 
min:
number | null - 
max:
number | null 
BooleanKind
ClassArrayKind
Class- 
elements:
Kind 
OptionKind
Class- 
options:
Record<string,string> 
CustomKind
Class- 
name:
string 
Expression
ClassIdentifiable
Class- 
name:
string 
Variable
Class- 
kind:
Kind - 
init:
Expression | null 
Literal
Class- 
value:
string | number | boolean 
String
Class- 
value:
Array<union> 
Identifier
Class- 
name:
string - 
external:
boolean - 
identifiable:
Identifiable | null 
Val
ClassArrayExpression
Class- 
elements:
Array<Expression> 
BinaryExpression
Class- 
left:
Expression - 
operator:
"+" | "-" | "*" | "/" | "!=" | "==" | "<" | "<=" | ">" | ">=" | "||" | "&&" | "??" | "^" | "%" - 
right:
Expression 
ObjectExpression
Class- 
properties:
Record<string,Expression> 
Block
Class- 
statements:
Array<Expression> 
Param
ClassFunc
Class- 
name:
string | null - 
params:
Array<Param> - 
body:
Block 
CallExpression
Class- 
identifier:
Identifier - 
arguments:
Array<Expression> 
UnaryExpression
Class- 
operator:
"-" | "+" - 
argument:
Expression 
ConditionalExpression
Class- 
condition:
Expression - 
consequent:
Expression - 
alternate:
Expression 
IfStatement
Class- 
condition:
Expression - 
consequent:
Block 
Assignment
Class- 
left:
Identifier | MemberExpression - 
operator:
"=" | "+=" | "-=" | "*=" | "/=" | "^=" | "%=" - 
right:
Expression 
MemberExpression
Class- 
object:
Identifier | MemberExpression - 
property:
Expression 
ComponentProp
Class- 
bindable:
boolean 
Component
ClassRekaComponent
Class- 
template:
Template | null - 
state:
Array<Val> - 
props:
Array<ComponentProp> 
ExternalComponent
Class- 
render:
any - 
props:
Array<ComponentProp> 
PropBinding
Class- 
identifier:
Identifier 
Template
Class- 
props:
Record<string,Expression> - 
if:
Expression | null - 
each:
ElementEach | null - 
classList:
ObjectExpression | null 
SlottableTemplate
Class- 
children:
Array<Template> - 
slots:
Record<string,Array> 
FragmentTemplate
ClassRootTemplate
ClassTagTemplate
Class- 
tag:
string 
ComponentTemplate
Class- 
component:
Identifier 
SlotTemplate
Class- 
name:
string | null - 
accepts:
Identifier | null 
ElementEachAlias
ClassElementEachIndex
ClassElementEach
Class- 
alias:
ElementEachAlias - 
index:
ElementEachIndex | null - 
iterator:
Expression 
View
Class- 
key:
string - 
template:
Template - 
frame:
string - 
owner:
ComponentView | null 
SlottableView
Class- 
children:
Array<View> 
TagView
Class- 
tag:
string - 
props:
Record<string,any> - 
bindings:
Record<string,Function> 
ComponentView
Class- 
component:
Component 
FragmentView
ClassFrameView
ClassRekaComponentView
Class- 
render:
Array<View> 
ExternalComponentView
Class- 
props:
Record<string,any> 
SlotView
Class- 
children:
Array<View> 
SystemView
ClassEachSystemView
Class- 
children:
Array<View> 
ErrorSystemView
Class- 
error:
string 
ExtensionState
Class- 
value:
Record | null 
ExternalState
Class- 
init:
any 
ExternalFunc
Class- 
func:
Function 
Type
Class- 
type:
string - 
id:
string 
Utilities
match(node: Any, visitor: Partial<Visitor>): void
Function
Match a callback to a Type
tsximport * as t from '@rekajs/types';const expr = t.literal({ value: 0 });t.match(expr, {Literal: (type) => {// do stuff},});// If a callback for Literal is not specified,// But a callback for the parent Type of Literal (in this case, Expression) will be usedt.match(expr, {Expression: (type) => {// do stuff},});
isObjectLiteral(t: any): boolean
Function
toJS(value: any): any
Function
merge(a: any, b: any, opts?: Object): any
Function
Compare 2 Types and merge differences
tsximport * as t from '@rekajs/types';const a = t.binaryExpression({left: t.literal({ value: 2 }),operator: '<',right: t.literal({ value: 4 }),});const b = t.binaryExpression({left: t.literal({ value: 10 }),operator: '<',right: t.literal({ value: 4 }),});t.merge(a, b);// Changes from 'b' is applied on aconsole.log(a.left.value === b.left.value); // true
flatten(root: Type): Object
Function
Flatten a Type and its children
tsximport * as t from '@rekajs/types';const expr = t.binaryExpression({left: t.literal({ value: 2 }),operator: '+',right: t.literal({ value: 4 })});t.flatten(expr);// flattened{root: "root-id",types: {"root-id": {type: "BinaryExpression",left: {$$typeId: "left-id",},operator: "+",right: {$$typeId: "right-id",}},"left-id": {type: "Literal",value: 2},"right-id": {type: "Literal",value: 4}}}
unflatten(flattenedType: FlattenedType): any
Function
Restore a flattend Type
tsximport * as t from '@rekajs/types';const flattened = { types: {...}, root: "..." };const type = t.unflatten(flattened)console.log(type instanceof t.Type) // true
collect(type: Type): Array<Type>
Function
Collect all child Types within a given Type
tsximport * as t from '@rekajs/types';const expr = t.binaryExpression({left: t.literal({ value: 1 }),operator: '+',right: t.literal({ value: 2 }),});const collectedTypes = t.collect(expr);// collectedTypes = [t.BinaryExpression(...), t.Literal({ value : 1 }), t.Literal({ value: 2 })]
is<T extends Type>(value: any, type: intersection<Function,Object>): boolean
Function
assert<T extends Type>(value: any, assertedType: intersection<Function,Object>): T
assert<T extends Type, C extends Function>(value: any, assertedType: intersection<Function,Object>, cb: C): ReturnType<C>
Function
Assert a value's Type
tsximport * as t from '@rekajs/types';t.assert(t.literal({ value: 0 }), t.Literal); // okt.assert(t.identifier({ name: 'counter' }), t.Literal); // throws// Specify an optional callback to traverse and return a value from the asserted typeconst expr = t.assert(t.binaryExpression({left: t.literal({ value: 1 }),operator: '+',right: t.literal({ value: 2 }),}),t.BinaryExpression,(value) => {return t.left;});console.log(expr instanceof t.Expression); // true
getRootIdentifierInMemberExpression(expr: MemberExpression): Identifier
Function
clone(value: any, options?: Partial<Object>): any
Function
Clone a primitive value, object literal or a Reka data type
tsxconst original = t.binaryExpression({left: t.literal({ value: 1 }),operator: '+',right: t.literal({ vlalue: 2 }),});const clone = t.clone(original);console.log(original === clone); // false// By default, the clone() util maintains the original idsconsole.log(original.id === clone.id); // true// Clone with new ids via the `replaceExistingIds` option:const cloneWithNewIds = t.clone(original, {replaceExistingIds: true,});console.log(original.id === cloneWithNewIds.id); // false
toStringType(node: Expression): String
Function
isSameType(nodes: Array<Type>): boolean
Function
Given an array of Type instances, check if they are all the same Type