-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Adds the visitor API foundation for tree transformations. #6892
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
Conversation
@mhegazy, @ahejlsberg I will be publishing a series of pull requests targeting the |
Another PR with the second layer of the transformations API will be published tomorrow. |
CC: @yuit, @DanielRosenwasser, @RyanCavanaugh, @vladima I want to make sure I have a lot of eyes on this implementation as we move forward. |
} | ||
|
||
export function createNodeArrayNode<T extends Node>(elements?: (T | NodeArrayNode<T>)[]): NodeArrayNode<T> { | ||
const array = <NodeArrayNode<T>>createNodeArray(elements); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you feed NodeArrayNode<T>
as a type argument instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would end up being createNodeArray<T | NodeArrayNode<T>, NodeArrayNode<T>>(elements))
, which isn't as clear.
CC: @sandersn |
/* @internal */ | ||
namespace ts { | ||
let NodeConstructor: new (kind: SyntaxKind, pos: number, end: number) => Node; | ||
let SourceFileConstructor: new (kind: SyntaxKind, pos: number, end: number) => Node; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any reason you need to do this again instead of leveraging ts.createNode
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is partly performance related, as the ts
namespace is horribly deoptimized in v8, but also due to the fact I have a slightly different call signature.
Adds the Module transformers
Adds the ES6 transformer
Adds the ES7 transformer
Adds the JSX transformer
Adds the TypeScript transformer
Adds a simplified pretty printer for tree transformations
Adds the transformFiles API for tree transformations
Adds the Transform Flags concept for tree transformations
Adds the visitor API foundation for tree transformations.
This provides the basic foundation for tree transformations. The additions include:
factory.ts
- Which will include a growing list of node factory functions used by various transformations.visitor.ts
- The various visitor functions used for visiting the tree.The
visitNode
function is used to visit single-node branches of a tree. It also provides a facility for converting an array of nodes (in the form of aNodeArrayNode
) into a single node, for cases such as converting a single-statement branch of anIterationStatement
into aBlock
.The
visitNodes
function is used to visit an array of nodes. If there are no changes to any node in the array (all visited nodes have the same reference and the same number of nodes were written), then the original array is returned. This allows for faster change detection invisitEachChild
by way of reference equality checks.The
visitEachNode
function is used as a fallback mechanism to recursively apply a visitor to each branch of the tree. This function leveragesnodeEdgeTraversalMap
, which is used to describe which properties should be traversed as well as how to verify the tree.The
LexicalEnvironment
interface will be leveraged in a later revision to provide a mechanism for hoisting temporary variables and function declarations.Examples
The following is an example of a
visitor
that could be used with these helpers:Related Pull Requests: