RTI API Reference

Stringifier

Class for converting AST into string.

Summary

Properties

addReactImport

If we encounter JSX elements, we add React's createElement as import.

parents
spaces

A string of two spaces per indentation.

Methods

ArrayExpression
ArrayPattern

for (const [a, b] in c) --> [a, b] is the array pattern

ArrowFunctionExpression
AssignmentExpression
AssignmentPattern
AwaitExpression

await something();

BigIntLiteral
BinaryExpression
BlockStatement
BooleanLiteral
BreakStatement
CallExpression
CatchClause
ClassBody
ClassDeclaration
ClassExpression
ClassMethod
ClassPrivateMethod
ClassPrivateProperty
ClassProperty

asd; asd = 1; static asd; static asd = 1;

CommentBlock
CommentLine
commentToSource
ConditionalExpression
ContinueStatement
DebuggerStatement
Directive

'use strict';

DirectiveLiteral

'use strict';

DoWhileStatement
EmptyStatement

showAST("if (true);");

ExportAllDeclaration
ExportDefaultDeclaration
ExportNamedDeclaration
ExportNamespaceSpecifier
ExportSpecifier
ExpressionStatement
File
ForInStatement
ForOfStatement
ForStatement

TODO TEST: can init be undefined in for(;;)

FunctionDeclaration
FunctionDeclarationParams

Converts an array of Babel AST nodes representing function parameters into a comma-separated string.

FunctionExpression
generateTypeChecks

Generates a string representing type checks for a given Babel AST node.

getHeader
Identifier
IfStatement
Import
ImportDeclaration
ImportDefaultSpecifier
ImportExpression
ImportNamespaceSpecifier
ImportSpecifier
JSXAttribute
JSXElement
JSXExpressionContainer
JSXFragment
JSXIdentifier
JSXMemberExpression
JSXNamespacedName
JSXText
LabeledStatement
LogicalExpression

if (.

mapToSource
MemberExpression
MetaProperty
NewExpression
NullLiteral
NumericLiteral
ObjectExpression

We replicate the exact AST for validation: x = 1; y = {x,} z = {y};

ObjectMethod
ObjectPattern

E.

ObjectProperty
OptionalCallExpression

version?.

OptionalMemberExpression

x?.

ParenthesizedExpression
PrivateName
Program
RegExpLiteral
RestElement
ReturnStatement
SequenceExpression

for (var i=0, n=arr.

SpreadElement
StringLiteral
Super
SwitchCase
SwitchStatement
TaggedTemplateExpression
TemplateElement
TemplateLiteral
ThisExpression
ThrowStatement
toSource
toSource_
toSourceCurly

Only add { and } when requested (e.

TryStatement
UnaryExpression
UpdateExpression

showAST("++i")

VariableDeclaration
VariableDeclarator
WhileStatement
YieldExpression

Details

Constructor

Stringifier() #

Properties

addReactImport #

If we encounter JSX elements, we add React's createElement as import.

Node[]parents #

stringspaces #

A string of two spaces per indentation.

Methods

ArrayExpression(node) #

Parameters

nodeimport("@babel/types").ArrayExpression

The Babel AST node.

Returns

string

Stringification of the node.

ArrayPattern(node) #

for (const [a, b] in c) --> [a, b] is the array pattern

Parameters

nodeimport("@babel/types").ArrayPattern

The Babel AST node.

Returns

string

Stringification of the node.

ArrowFunctionExpression(node) #

Parameters

nodeimport("@babel/types").ArrowFunctionExpression

The Babel AST node.

Returns

string

Stringification of the node.

AssignmentExpression(node) #

Parameters

nodeimport("@babel/types").AssignmentExpression

The Babel AST node.

Returns

string

Stringification of the node.

AssignmentPattern(node) #

Parameters

nodeimport("@babel/types").AssignmentPattern

The Babel AST node.

Returns

string

Stringification of the node.

AwaitExpression(node) #

await something();

Parameters

nodeimport("@babel/types").AwaitExpression

The Babel AST node.

Returns

string

Stringification of the node.

BigIntLiteral(node) #

Parameters

nodeimport("@babel/types").BigIntLiteral

The Babel AST node.

Returns

string

Stringification of the node.

BinaryExpression(node) #

Parameters

nodeimport("@babel/types").BinaryExpression

The Babel AST node.

Returns

string

Stringification of the node.

BlockStatement(node) #

Parameters

nodeimport("@babel/types").BlockStatement

The Babel AST node.

Returns

string

Stringification of the node.

BooleanLiteral(node) #

Parameters

nodeimport("@babel/types").BooleanLiteral

The Babel AST node.

Returns

string

Stringification of the node.

BreakStatement(node) #

Parameters

nodeimport("@babel/types").BreakStatement

The Babel AST node.

Returns

string

Stringification of the node.

CallExpression(node) #

Parameters

nodeimport("@babel/types").CallExpression

The Babel AST node.

Returns

string

Stringification of the node.

CatchClause(node) #

Parameters

nodeimport("@babel/types").CatchClause

The Babel AST node.

Returns

string

Stringification of the node.

ClassBody(node) #

Parameters

nodeimport("@babel/types").ClassBody

The Babel AST node.

Returns

string

Stringification of the node.

ClassDeclaration(node) #

Parameters

nodeimport("@babel/types").ClassDeclaration

The Babel AST node.

Returns

string

Stringification of the node.

ClassExpression(node) #

Parameters

nodeimport("@babel/types").ClassExpression

The Babel AST node.

Returns

string

Stringification of the node.

ClassMethod(node) #

Parameters

nodeimport("@babel/types").ClassMethod

The Babel AST node.

Returns

string

Stringification of the node.

ClassPrivateMethod(node) #

Parameters

nodeimport("@babel/types").ClassPrivateMethod

The Babel AST node.

Returns

string

Stringification of the node.

ClassPrivateProperty(node) #

Parameters

nodeimport("@babel/types").ClassPrivateProperty

The Babel AST node.

Returns

string

Stringification of the node.

ClassProperty(node) #

asd; asd = 1; static asd; static asd = 1;

Parameters

nodeimport("@babel/types").ClassProperty

The Babel AST node.

Returns

string

Stringification of the node.

CommentBlock(node) #

Parameters

nodeimport("@babel/types").CommentBlock

The Babel AST node.

Returns

string

Stringification of the node.

CommentLine(node) #

Parameters

nodeimport("@babel/types").CommentLine

The Babel AST node.

Returns

string

Stringification of the node.

commentToSource(comment, pos) #

Parameters

comment*

The comment "node" (not a real Babel Node node though, hence this special treatment)

pos'leading', 'trailing'

The position, either 'leading' or 'trailing'.

Returns

string

The comment "node" as a string.

ConditionalExpression(node) #

Parameters

nodeimport("@babel/types").ConditionalExpression

The Babel AST node.

Returns

string

Stringification of the node.

ContinueStatement(node) #

Parameters

nodeimport("@babel/types").ContinueStatement

The Babel AST node.

Returns

string

Stringification of the node.

DebuggerStatement(node) #

Parameters

nodeimport("@babel/types").DebuggerStatement

The Babel AST node.

Returns

string

Stringification of the node.

Directive(node) #

'use strict';

Parameters

nodeimport("@babel/types").Directive

The Babel AST node.

Returns

string

Stringification of the node.

DirectiveLiteral(node) #

'use strict';

Parameters

nodeimport("@babel/types").DirectiveLiteral

The Babel AST node.

Returns

string

Stringification of the node.

DoWhileStatement(node) #

Parameters

nodeimport("@babel/types").DoWhileStatement

The Babel AST node.

Returns

string

Stringification of the node.

EmptyStatement(node) #

showAST("if (true);");

Parameters

nodeimport("@babel/types").EmptyStatement

The Babel AST node.

Returns

string

Stringification of the node.

ExportAllDeclaration(node) #

Parameters

nodeimport("@babel/types").ExportAllDeclaration

The Babel AST node.

Returns

string

Stringification of the node.

ExportDefaultDeclaration(node) #

Parameters

nodeimport("@babel/types").ExportDefaultDeclaration

The Babel AST node.

Returns

string

Stringification of the node.

ExportNamedDeclaration(node) #

Parameters

nodeimport("@babel/types").ExportNamedDeclaration

The Babel AST node.

Returns

string

Stringification of the node.

ExportNamespaceSpecifier(node) #

Parameters

nodeimport("@babel/types").ExportNamespaceSpecifier

The Babel AST node.

Returns

string

Stringification of the node.

ExportSpecifier(node) #

Parameters

nodeimport("@babel/types").ExportSpecifier

The Babel AST node.

Returns

string

Stringification of the node.

ExpressionStatement(node) #

Parameters

nodeimport("@babel/types").ExpressionStatement

The Babel AST node.

Returns

string

Stringification of the node.

File(node) #

Parameters

nodeimport("@babel/types").File

The Babel AST node.

Returns

string

Stringification of the node.

ForInStatement(node) #

Parameters

nodeimport("@babel/types").ForInStatement

The Babel AST node.

Returns

string

Stringification of the node.

ForOfStatement(node) #

Parameters

nodeimport("@babel/types").ForOfStatement

The Babel AST node.

Returns

string

Stringification of the node.

ForStatement(node) #

TODO TEST: can init be undefined in for(;;)

Parameters

nodeimport("@babel/types").ForStatement

The Babel AST node.

Returns

string

Stringification of the node.

FunctionDeclaration(node) #

Parameters

nodeimport("@babel/types").FunctionDeclaration

The Babel AST node.

Returns

string

Stringification of the node.

FunctionDeclarationParams(params) #

Converts an array of Babel AST nodes representing function parameters into a comma-separated string.

Each parameter node is converted to its source representation and combined into a single string suitable for inserting into a function declaration's parentheses.

Parameters

paramsNode[]

An array of Babel AST nodes representing the function parameters to be stringified.

Returns

string

A string representing the serialized parameters, enclosed in parentheses.

FunctionExpression(node) #

Parameters

nodeimport("@babel/types").FunctionExpression

The Babel AST node.

Returns

string

Stringification of the node.

generateTypeChecks(node) #

Generates a string representing type checks for a given Babel AST node.

Note: This method serves as a stub and should be overridden in subclasses. The actual implementation is expected to be provided in Asserter.js, where it would create runtime type assertions based on the AST node provided.

Parameters

nodeNode

The Babel AST node for which to generate type checks.

Returns

string

A placeholder string, as this stub implementation does nothing; expected to be overridden.

getHeader() #

Parameters

Returns

string

The header.

Identifier(node) #

Parameters

nodeimport("@babel/types").Identifier

The Babel AST node.

Returns

string

Stringification of the node.

IfStatement(node) #

Parameters

nodeimport("@babel/types").IfStatement

The Babel AST node.

Returns

string

Stringification of the node.

Import(node) #

Parameters

nodeimport("@babel/types").Import

The Babel AST node.

Returns

string

Stringification of the node.

ImportDeclaration(node) #

Parameters

nodeimport("@babel/types").ImportDeclaration

The Babel AST node.

Returns

string

Stringification of the node.

ImportDefaultSpecifier(node) #

Parameters

nodeimport("@babel/types").ImportDefaultSpecifier

The Babel AST node.

Returns

string

Stringification of the node.

ImportExpression(node) #

Parameters

nodeimport("@babel/types").ImportExpression

The Babel AST node.

Returns

string

Stringification of the node.

ImportNamespaceSpecifier(node) #

Parameters

nodeimport("@babel/types").ImportNamespaceSpecifier

The Babel AST node.

Returns

string

Stringification of the node.

ImportSpecifier(node) #

Parameters

nodeimport("@babel/types").ImportSpecifier

The Babel AST node.

Returns

string

Stringification of the node.

JSXAttribute(node) #

Parameters

nodeimport("@babel/types").JSXAttribute

The Babel AST node.

Returns

string

Stringification of the node.

JSXElement(node) #

Parameters

nodeimport("@babel/types").JSXElement

The Babel AST node.

Returns

string

Stringification of the node.

JSXExpressionContainer(node) #

Parameters

nodeimport("@babel/types").JSXExpressionContainer

The Babel AST node.

Returns

string

Stringification of the node.

JSXFragment(node) #

Parameters

nodeimport("@babel/types").JSXFragment

The Babel AST node.

Returns

string

Stringification of the node.

JSXIdentifier(node) #

Parameters

nodeimport("@babel/types").JSXIdentifier

The Babel AST node.

Returns

string

Stringification of the node.

JSXMemberExpression(node) #

Parameters

nodeimport("@babel/types").JSXMemberExpression

The Babel AST node.

Returns

string

Stringification of the node.

JSXNamespacedName(node) #

Parameters

nodeimport("@babel/types").JSXNamespacedName

The Babel AST node.

Returns

string

Stringification of the node.

JSXText(node) #

Parameters

nodeimport("@babel/types").JSXText

The Babel AST node.

Returns

string

Stringification of the node.

LabeledStatement(node) #

Parameters

nodeimport("@babel/types").LabeledStatement

The Babel AST node.

Returns

string

Stringification of the node.

LogicalExpression(node) #

if (...)

Parameters

nodeimport("@babel/types").LogicalExpression

The Babel AST node.

Returns

string

Stringification of the node.

mapToSource(arr) #

Parameters

arrNode[]

Array of nodes to convert.

Returns

string[]

Array of strings for each node.

MemberExpression(node) #

Parameters

nodeimport("@babel/types").MemberExpression

The Babel AST node.

Returns

string

Stringification of the node.

MetaProperty(node) #

Parameters

nodeimport("@babel/types").MetaProperty

The Babel AST node.

Returns

string

Stringification of the node.

NewExpression(node) #

Parameters

nodeimport("@babel/types").NewExpression

The Babel AST node.

Returns

string

Stringification of the node.

NullLiteral(node) #

Parameters

nodeimport("@babel/types").NullLiteral

The Babel AST node.

Returns

string

Stringification of the node.

NumericLiteral(node) #

Parameters

nodeimport("@babel/types").NumericLiteral

The Babel AST node.

Returns

string

Stringification of the node.

ObjectExpression(node) #

We replicate the exact AST for validation: x = 1; y = {x,} z = {y};

Parameters

nodeimport("@babel/types").ObjectExpression

The Babel AST node.

Returns

string

Stringification of the node.

ObjectMethod(node) #

Parameters

nodeimport("@babel/types").ObjectMethod

The Babel AST node.

Returns

string

Stringification of the node.

ObjectPattern(node) #

E.g. function testComma({x,y,z,}) {}

Parameters

nodeimport("@babel/types").ObjectPattern

The Babel AST node.

Returns

string

Stringification of the node.

ObjectProperty(node) #

Parameters

nodeimport("@babel/types").ObjectProperty

The Babel AST node.

Returns

string

Stringification of the node.

OptionalCallExpression(node) #

version?.indexOf('$'); version?.indexOf?.('$'); this.passEncoder?.pushDebugGroup(name);

Parameters

nodeimport("@babel/types").OptionalCallExpression

The Babel AST node.

Returns

string

Stringification of the node.

OptionalMemberExpression(node) #

x?.y x?.y.z x[y?.z] x?.[y?.z] b?.file?.variants[variant]

Parameters

nodeimport("@babel/types").OptionalMemberExpression

The Babel AST node.

Returns

string

Stringification of the node.

ParenthesizedExpression(node) #

Parameters

nodeimport("@babel/types").ParenthesizedExpression

The Babel AST node.

Returns

string

Stringification of the node.

PrivateName(node) #

Parameters

nodeimport("@babel/types").PrivateName

The Babel AST node.

Returns

string

Stringification of the node.

Program(node) #

Parameters

nodeimport("@babel/types").Program

The Babel AST node.

Returns

string

Stringification of the node.

RegExpLiteral(node) #

Parameters

nodeimport("@babel/types").RegExpLiteral

The Babel AST node.

Returns

string

Stringification of the node.

RestElement(node) #

Parameters

nodeimport("@babel/types").RestElement

The Babel AST node.

Returns

string

Stringification of the node.

ReturnStatement(node) #

Parameters

nodeimport("@babel/types").ReturnStatement

The Babel AST node.

Returns

string

Stringification of the node.

SequenceExpression(node) #

for (var i=0, n=arr.length; i<n; i++) sequence expression: var i=0, n=arr.length

Parameters

nodeimport("@babel/types").SequenceExpression

The Babel AST node.

Returns

string

Stringification of the node.

SpreadElement(node) #

Parameters

nodeimport("@babel/types").SpreadElement

The Babel AST node.

Returns

string

Stringification of the node.

StringLiteral(node) #

Parameters

nodeimport("@babel/types").StringLiteral

The Babel AST node.

Returns

string

Stringification of the node.

Super(node) #

Parameters

nodeimport("@babel/types").Super

The Babel AST node.

Returns

string

Stringification of the node.

SwitchCase(node) #

Parameters

nodeimport("@babel/types").SwitchCase

The Babel AST node.

Returns

string

Stringification of the node.

SwitchStatement(node) #

Parameters

nodeimport("@babel/types").SwitchStatement

The Babel AST node.

Returns

string

Stringification of the node.

TaggedTemplateExpression(node) #

Parameters

nodeimport("@babel/types").TaggedTemplateExpression

The Babel AST node.

Returns

string

Stringification of the node.

TemplateElement(node) #

Parameters

nodeimport("@babel/types").TemplateElement

The Babel AST node.

Returns

string

Stringification of the node.

TemplateLiteral(node) #

console.log(ast2json(parseSync("`${1} b ${2+3}c`").program.body[0]));

Parameters

nodeimport("@babel/types").TemplateLiteral

The Babel AST node.

Returns

string

Stringification of the node.

ThisExpression(node) #

Parameters

nodeimport("@babel/types").ThisExpression

The Babel AST node.

Returns

string

Stringification of the node.

ThrowStatement(node) #

Parameters

nodeimport("@babel/types").ThrowStatement

The Babel AST node.

Returns

string

Stringification of the node.

toSource(node) #

Parameters

nodeNode

The Babel AST node.

Returns

string

Stringification of the node.

toSource_(node) #

Parameters

nodeNode

The Babel AST node.

Returns

string

Stringification of the node.

toSourceCurly(node) #

Only add { and } when requested (e.g. for Asserter). showAST("if (true) 2;") vs showAST("if (true) {2}")

Parameters

nodeNode

The Babel AST node.

Returns

string

Stringification of the node.

TryStatement(node) #

Parameters

nodeimport("@babel/types").TryStatement

The Babel AST node.

Returns

string

Stringification of the node.

UnaryExpression(node) #

ts = require("typescript");
ts.createSourceFile("repl.ts", "a = 1", ts.ScriptTarget.Latest);
ts.createSourceFile("repl.ts", "!!(a = 1 + 2)", ts.ScriptTarget.Latest);
showAST('!!(a = 1)');

Parameters

nodeimport("@babel/types").UnaryExpression

The Babel AST node.

Returns

string

Stringification of the node.

UpdateExpression(node) #

showAST("++i")

Parameters

nodeimport("@babel/types").UpdateExpression

The Babel AST node.

Returns

string

Stringification of the node.

VariableDeclaration(node) #

Parameters

nodeimport("@babel/types").VariableDeclaration

The Babel AST node.

Returns

string

Stringification of the node.

VariableDeclarator(node) #

Parameters

nodeimport("@babel/types").VariableDeclarator

The Babel AST node.

Returns

string

Stringification of the node.

WhileStatement(node) #

Parameters

nodeimport("@babel/types").WhileStatement

The Babel AST node.

Returns

string

Stringification of the node.

YieldExpression(node) #

Parameters

nodeimport("@babel/types").YieldExpression

The Babel AST node.

Returns

string

Stringification of the node.