Skip to content

Instantly share code, notes, and snippets.

@alexeychikk
Created August 27, 2023 20:58
Show Gist options
  • Save alexeychikk/f71b2ad41e885c6aa46170ad1f73c792 to your computer and use it in GitHub Desktop.
Save alexeychikk/f71b2ad41e885c6aa46170ad1f73c792 to your computer and use it in GitHub Desktop.
const options = {
...this.compilerOptions,
target: ts.ScriptTarget.ES5,
};
// @ts-expect-error foo
const scriptTransformers = ts.getTransformers(options).scriptTransformers;
const transformer: ts.TransformerFactory<ts.SourceFile> = (context) => {
return (sourceFile) => {
const visitor = (node: ts.Node): ts.Node => {
if (ts.isParameter(node)) {
return context.factory.updateParameterDeclaration(
node,
node.modifiers,
node.dotDotDotToken,
'foo',
node.questionToken,
node.type,
node.initializer,
);
}
return ts.visitEachChild(node, visitor, context);
};
return ts.visitNode(sourceFile, visitor) as ts.SourceFile;
};
};
const customTransformers = scriptTransformers.slice(3, 11);
const { transformed } = ts.transform(this.sourceFile, customTransformers);
const printer = ts.createPrinter();
const str = printer.printNode(
ts.EmitHint.SourceFile,
transformed[0],
transformed[0],
);
console.log(str);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment