Is your feature request related to a problem? Please describe.
PrintHandlers are a powerful option in ts printers enabling users to modify the print results. Today ts-morph doesn't support this and the transformation support is experimental. If users want to print modified AST they need to rely on the typescript compiler API. A small change to printNode could significantly improve this.
Describe the solution you'd like
ts.createPrinter supports two sets of options. The first is supported today. The second, handlers, is not.
I'd like printNode to accept an interface similar to PrintHandlers which provides callbacks with ts-morph Nodes rather than ts.Node. For example:
substituteNode?(hint: EmitHint, node: tsMorph.Node): ts.Node;
Describe alternatives you've considered
Currently this can be achieved in projects by using Node.compilerNode to leave the ts-morph ecosystem and use the typescript compiler API directly. However, this presents a painful issue because any usage of PrintHandlers.substituteNode or PrintHandlers.isEmitNotificationEnabled or PrintHandlers.onEmitNode forces projects into one of two scenarios:
- Ensure handlers have no ts-morph dependencies and can operate on just the typescript compiler API
- Maintain their own mapping of typescript nodes to ts-morph nodes for the entire AST
Both of these options are impractical. (1) doesn't suit all use cases especially for substituteNode which often relies on linking previously analyzed nodes to visited nodes and (2) would be better served by exposing the ts-morph internal helpers such as getNodeFromCompilerNode
Is your feature request related to a problem? Please describe.
PrintHandlersare a powerful option in ts printers enabling users to modify the print results. Today ts-morph doesn't support this and the transformation support is experimental. If users want to print modified AST they need to rely on the typescript compiler API. A small change toprintNodecould significantly improve this.Describe the solution you'd like
ts.createPrintersupports two sets of options. The first is supported today. The second, handlers, is not.I'd like
printNodeto accept an interface similar toPrintHandlerswhich provides callbacks with ts-morph Nodes rather than ts.Node. For example:Describe alternatives you've considered
Currently this can be achieved in projects by using
Node.compilerNodeto leave the ts-morph ecosystem and use the typescript compiler API directly. However, this presents a painful issue because any usage ofPrintHandlers.substituteNodeorPrintHandlers.isEmitNotificationEnabledorPrintHandlers.onEmitNodeforces projects into one of two scenarios:Both of these options are impractical. (1) doesn't suit all use cases especially for
substituteNodewhich often relies on linking previously analyzed nodes to visited nodes and (2) would be better served by exposing the ts-morph internal helpers such asgetNodeFromCompilerNode