Skip to content

Commit

Permalink
Add utility to convert AST in easier to process data structures (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
ajafff authored Jan 1, 2018
1 parent 8d1bcd4 commit fd76045
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
71 changes: 71 additions & 0 deletions util/convert-ast.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import * as ts from 'typescript';

/** Wraps an AST node. Can be used as a tree using `children` or a linked list using `next` and `skip`. */
export interface NodeWrap {
/** The real AST node. */
node: ts.Node;
/** All immediate children of `node` that would be visited by `ts.forEachChild(node, cb)`. */
children: NodeWrap[];
/** Link to the next NodeWrap, depth-first. */
next?: NodeWrap;
/** Link to the next NodeWrap skipping all children of the current node. */
skip?: NodeWrap;
/** Link to the parent NodeWrap */
parent?: NodeWrap;
}

export interface ConvertedAst {
/** nodes wrapped in a data structure with useful links */
wrapped: NodeWrap;
/** depth-first array of all nodes */
flat: ReadonlyArray<ts.Node>;
}

/**
* Takes a `ts.SourceFile` and creates data structures that are easier (or more performant) to traverse.
* Note that there is only a performance gain if you can reuse these structures. It's not recommended for one-time AST walks.
*/
export function convertAst(sourceFile: ts.SourceFile): ConvertedAst {
const wrapped: NodeWrap = {
node: sourceFile,
parent: undefined,
children: [],
next: undefined,
skip: undefined,
};
const flat: ts.Node[] = [];
let current = wrapped;
let previous = current;
ts.forEachChild(sourceFile, function wrap(node) {
flat.push(node);
const parent = current;
previous.next = current = {
node,
parent,
children: [],
next: undefined,
skip: undefined,
};
if (previous !== parent)
setSkip(previous, current);

previous = current;
parent.children.push(current);

ts.forEachChild(node, wrap);

current = parent;
});

return {
wrapped,
flat,
};
}

function setSkip(node: NodeWrap, skip: NodeWrap) {
do {
node.skip = skip;
node = node.parent!;
} while (node !== skip.parent);
}
1 change: 1 addition & 0 deletions util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from './util';
export * from './usage';
export * from './control-flow';
export * from './type';
export * from './convert-ast';

0 comments on commit fd76045

Please sign in to comment.