Created
September 23, 2019 16:50
-
-
Save vkbansal/3f944b8d5b20e6a0f1ca49e90e447571 to your computer and use it in GitHub Desktop.
[TS tranformer] TS tranformer for codemod #typescript #codemod #ast
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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