Skip to content

Instantly share code, notes, and snippets.

@vkbansal
Created September 23, 2019 16:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vkbansal/3f944b8d5b20e6a0f1ca49e90e447571 to your computer and use it in GitHub Desktop.
Save vkbansal/3f944b8d5b20e6a0f1ca49e90e447571 to your computer and use it in GitHub Desktop.
[TS tranformer] TS tranformer for codemod #typescript #codemod #ast
function transformer<T extends ts.Node>(): ts.TransformerFactory<T> {
return context => {
const visit: ts.Visitor = node => {
switch (node.kind) {
case ts.SyntaxKind.Parameter:
if (!(node as any).type) {
const newNode = ts.createParameter(
(node as ts.ParameterDeclaration).decorators,
(node as ts.ParameterDeclaration).modifiers,
(node as ts.ParameterDeclaration).dotDotDotToken,
(node as ts.ParameterDeclaration).name,
(node as ts.ParameterDeclaration).questionToken,
ts.createTypeReferenceNode('TodoAny', []),
(node as ts.ParameterDeclaration).initializer
)
return newNode
}
return node
default:
return ts.visitEachChild(node, child => visit(child), context)
}
}
return node => ts.visitNode(node, visit)
}
}
const result = ts.transform(sourceFile, [transformer<ts.SourceFile>()], {})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment