This commit is contained in:
Matthew Griffith 2021-07-10 11:00:04 -04:00
commit 2abb7d8781
2 changed files with 12 additions and 1 deletions

View File

@ -1,6 +1,6 @@
import ts, { isIdentifier } from 'typescript';
import { ast } from './utils/create';
import { ast, astNodes } from './utils/create';
export const replace = (
replacements: { [name: string]: string }
@ -13,6 +13,12 @@ export const replace = (
const key = name.text as keyof typeof replacements;
return ast(replacements[key]);
}
} else if (ts.isFunctionDeclaration(node)) {
const name = node.name;
if (isIdentifier(name) && name.text in replacements) {
const key = name.text as keyof typeof replacements;
return astNodes(replacements[key]);
}
}
return ts.visitEachChild(node, visitor, context);
};

View File

@ -6,6 +6,11 @@ export const ast = (sourceText: string): ts.Node => {
return source.statements[0];
};
export const astNodes = (sourceText: string): [ts.Node] => {
const source = ts.createSourceFile('bla', sourceText, ts.ScriptTarget.ES2018);
return source.statements;
}
export function create(name: string, body: ts.Node): ts.Node {
if (
ts.isExpressionStatement(body) &&