RTI API Reference

Asserter

Summary

Properties

addLaterImportNamespaceSpecifier
parent
stats
typedefs

Methods

ArrowFunctionExpression

We expand type-asserted ArrowFunctionExpressions in order to add type assertions.

BinaryExpression
ClassDeclaration
File
findParentOfType

Finds the closest ancestor of the given node that matches the specified type.

generateTypeChecks

Generates a string containing type checks for a given Babel AST node based on associated JSDoc information.

getHeader

Alternatively "import * as rti from .

getJSDoc
getLeadingComment
getLeadingCommentsNodeForArrowFunctionExpression

Retrieves the node associated with the leading comments for an arrow function expression.

getName
getNameForFunctionExpression
getNameOfParam

Retrieves the name of a parameter from a Babel AST node.

getStatsForNode

Retrieves statistical information for a given Babel AST node of this instance.

ImportDeclaration
ImportNamespaceSpecifier
MemberExpression
nodeHasParamName

Checks if a provided Babel AST node has a parameter with the given name.

VariableDeclaration
warn

Emits a warning message to the console, optionally prefixed with the instance's filename.

Details

Constructor

Asserter([options]) #

Parameters

optionsOptions

The options.

Properties

string[]addLaterImportNamespaceSpecifier #

Methods

ArrowFunctionExpression(node) #

We expand type-asserted ArrowFunctionExpressions in order to add type assertions.

Parameters

nodeimport("@babel/types").ArrowFunctionExpression

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.

ClassDeclaration(node) #

Parameters

nodeimport("@babel/types").ClassDeclaration

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.

findParentOfType(node, type) #

Finds the closest ancestor of the given node that matches the specified type.

Parameters

nodeNode

The starting node to search from.

typeT

Type name of the node to search for.

Returns

Extract<Node, {type: T}>, undefined

The first ancestor node of the specified type, or undefined if none is found.

generateTypeChecks(node) #

Generates a string containing type checks for a given Babel AST node based on associated JSDoc information.

This function analyzes the node and its JSDoc annotations to construct runtime type check expressions. It handles various parameter patterns and outputs code that performs actual type assertions. If a node does not correspond to any known or supported pattern, it returns an empty string.

Parameters

nodeNode

The Babel AST node for which to generate type checks.

Returns

string

A string of code with type check assertions, based on the JSDoc comments associated with the given node.

getHeader() #

Alternatively "import * as rti from ..." would also prevent "Unused external imports" warning... or keeping log of every single call during RTI parsing.

Parameters

Returns

string

The import declaration header for importing RTI.

getJSDoc(node) #

Parameters

nodeNode

The Babel AST node.

Returns

Record<string, any>, undefined

asd

getLeadingComment(node) #

Parameters

nodeNode

The Babel AST node.

Returns

string, undefined

The JSDoc comment of node.

getLeadingCommentsNodeForArrowFunctionExpression(node) #

Retrieves the node associated with the leading comments for an arrow function expression.

This method travels up the syntax tree from the given node to find a parent node with leading comments. This search is bounded by function boundaries or a 'CallExpression' node, as per the logic defined within the loop.

Parameters

nodeNode

The node representing the arrow function expression for which to find the leading comments node.

Returns

Node, undefined

The node that contains the leading comments, or undefined if none is found before reaching a different function or 'CallExpression'.

getName(node) #

Parameters

nodeNode

The Babel AST node.

Returns

string

Stringification of the node.

getNameForFunctionExpression(node) #

Parameters

nodeNode

The Babel AST node.

Returns

string

Best possible human-readable name of given node.

getNameOfParam(param) #

Retrieves the name of a parameter from a Babel AST node.

This function expects a node representing a function parameter and attempts to extract the parameter's name directly or from an AssignmentPattern.

Parameters

paramNode

The AST node representing the function parameter from which to extract the name.

Returns

string

The name of the parameter as a string, or the parameter's source code if the extraction fails.

getStatsForNode(node) #

Retrieves statistical information for a given Babel AST node of this instance.

Parameters

nodeNode

The Babel AST node for which the statistical data is retrieved.

Returns

Stat

An object containing the statistical data for the specified node. If the node type is unhandled, defaults to returning a dummy object with 'checked' and 'unchecked' properties both set to 0.

ImportDeclaration(node) #

Parameters

nodeimport("@babel/types").ImportDeclaration

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.

MemberExpression(node) #

Parameters

nodeimport("@babel/types").MemberExpression

The Babel AST node.

Returns

string

Stringification of the node.

nodeHasParamName(node, name) #

Checks if a provided Babel AST node has a parameter with the given name.

This function will look at the node's parameters if available and determine whether one of them matches the provided name. Supports various parameter types such as Identifiers and AssignmentPatterns.

Parameters

nodeNode

The Babel AST node to inspect. If it's a BlockStatement, the parent node is used instead.

namestring

The name of the parameter to look for within the node's parameters.

Returns

boolean

True if the node has a parameter with the given name; false otherwise.

VariableDeclaration(node) #

Parameters

nodeimport("@babel/types").VariableDeclaration

The Babel AST node.

Returns

string

Stringification of the node.

warn(args) #

Emits a warning message to the console, optionally prefixed with the instance's filename.

Parameters

argsany

A list of arguments to be passed to the console.warn function.