Skip to content

Instantly share code, notes, and snippets.

@dherges
Last active November 12, 2017 18:23
Show Gist options
  • Save dherges/df915f2c613f9d7b8d68c0464fcf1814 to your computer and use it in GitHub Desktop.
Save dherges/df915f2c613f9d7b8d68c0464fcf1814 to your computer and use it in GitHub Desktop.
TypeScript Transformations API
/**
* Visits a Node using the supplied visitor, possibly returning a new Node in its place.
*/
function visitNode<T extends Node>(node: T, visitor: Visitor): T;
/**
* Visits a NodeArray using the supplied visitor, possibly returning a new NodeArray in
* its place.
*/
function visitNodes<T extends Node>(nodes: NodeArray<T>, visitor: Visitor): NodeArray<T>;
/**
* Visits each child of a Node using the supplied visitor, possibly returning a new Node
* of the same kind in its place.
*/
function visitEachChild<T extends Node>(node: T, visitor: Visitor,
context: TransformationContext): T;
/**
* A function that accepts and possibly transforms a node.
*/
type Visitor =
(node: Node) => Node | Node[];
/**
* A function that transforms a node.
*/
type Transformer<T extends Node> =
(node: T) => T;
/**
* A function that is used to initialize and return a `Transformer` callback, which
* in turn will be used to transform one or more nodes.
*/
type TransformerFactory<T extends Node> =
(context: TransformationContext) => Transformer<T>;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment