ts-creator
A code generator to generate TypeScript code generator from TypeScript code
Try It!
How to use it:
npm install ts-creator
1. generate from code
import creator from 'ts-creator'
const generatedFactoryCode = creator(`const foo = "your code here"`)
2. transform source file
import { transformSourceFile } from 'ts-creator'
declare const file: ts.SourceFile
const factoryFile = transformSourceFile(file)
3. transform node
import { transformNode } from 'ts-creator'
declare const node: ts.Expression
const factoryNode = transformNode(node)
How does it work?
If you want to write a TypeScript codegen.
You got TypeScript code:
function foo(bar: number): number {
return bar + 1
}
ts-creator generate TypeScript factory from given code to:
ts.createFunctionDeclaration(
undefined,
undefined,
undefined,
ts.createIdentifier('foo'),
undefined,
[
ts.createParameter(
undefined,
undefined,
undefined,
ts.createIdentifier('bar'),
undefined,
ts.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword),
undefined
)
],
ts.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword),
ts.createBlock(
[
ts.createReturn(
ts.createBinary(
ts.createIdentifier('bar'),
ts.createToken(ts.SyntaxKind.PlusToken),
ts.createNumericLiteral('1')
)
)
],
true
)
)
Result after run the generated factory code:
function foo(bar: number): number {
return bar + 1;
}
TODO: