Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add utility to convert AST in easier to process data structures #20

Merged
merged 3 commits into from
Jan 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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';