Skip to content

Instantly share code, notes, and snippets.

@vkbansal vkbansal/.gitignore
Last active Apr 17, 2019

Embed
What would you like to do?
Printing TypeScript code from AST
node_modules
import ts from 'typescript';
// Create a Printer
const printer = ts.createPrinter({
newLine: ts.NewLineKind.LineFeed,
removeComments: false,
omitTrailingSemicolon: true
});
// Create a source file
const sourceFile = ts.createSourceFile(
'someFileName.ts',
'',
ts.ScriptTarget.ESNext,
/*setParentNodes*/ false,
ts.ScriptKind.TS
);
/**
* Outputs
* export function add (x: number, y: number): number {
* return x + y;
* }
*/
function makeAddFuntion() {
const parameters: ReadonlyArray<ts.ParameterDeclaration> = [
ts.createParameter(
/* decorators */ undefined,
/* modifiers */ undefined,
/* dot dot token */ undefined,
/* name */ ts.createIdentifier('x'),
/* questionToken */ undefined,
/* type */ ts.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword)
),
ts.createParameter(
/* decorators */ undefined,
/* modifiers */ undefined,
/* dot dot token */ undefined,
/* name */ ts.createIdentifier('y'),
/* questionToken */ undefined,
/* type */ ts.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword)
)
];
const statements: ts.Statement[] = [
ts.createReturn(
ts.createBinary(
ts.createIdentifier('x'),
ts.SyntaxKind.PlusToken,
ts.createIdentifier('y')
)
)
];
return ts.createFunctionDeclaration(
/* decorators */ undefined,
/* modifiers */ [ts.createModifier(ts.SyntaxKind.ExportKeyword)],
/* asteriskToken */ undefined,
/* name */ 'add',
/* typeParameters/ generics */ [],
/* parameters */ parameters,
/* returnType */ ts.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword),
ts.createBlock(statements, /*multiline*/ true)
);
}
/**
* Outputs
* function add (x: number, y: number): number {
* return x + y;
* }
*/
function makeMultiplyFuntion() {
const parameters: ReadonlyArray<ts.ParameterDeclaration> = [
ts.createParameter(
/* decorators */ undefined,
/* modifiers */ undefined,
/* dot dot token */ undefined,
/* name */ ts.createIdentifier('x'),
/* questionToken */ undefined,
/* type */ ts.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword)
),
ts.createParameter(
/* decorators */ undefined,
/* modifiers */ undefined,
/* dot dot token */ undefined,
/* name */ ts.createIdentifier('y'),
/* questionToken */ undefined,
/* type */ ts.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword)
)
];
const statements: ts.Statement[] = [
ts.createReturn(
ts.createBinary(
ts.createIdentifier('x'),
ts.SyntaxKind.AsteriskToken,
ts.createIdentifier('y')
)
)
];
return ts.createFunctionDeclaration(
/* decorators */ undefined,
/* modifiers */ [ts.createModifier(ts.SyntaxKind.ExportKeyword)],
/* asteriskToken */ undefined,
/* name */ 'multiply',
/* typeParameters/ generics */ [],
/* parameters */ parameters,
/* returnType */ ts.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword),
ts.createBlock(statements, /*multiline*/ true)
);
}
// Update the source file statements
sourceFile.statements = ts.createNodeArray([makeAddFuntion(), makeMultiplyFuntion()]);
// Print the new code
console.log(printer.printFile(sourceFile));
{
"name": "printing-ts-ast",
"private": true,
"scripts": {
"start": "ts-node --project tsconfig.json astPrinter.ts"
},
"dependencies": {
"ts-node": "^8.1.0",
"typescript": "^3.4.3"
}
}
{
"compilerOptions": {
"target": "esnext",
"module": "commonjs",
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"moduleResolution": "node",
"noUnusedLocals": true,
"noUnusedParameters": true,
"esModuleInterop": true,
"noErrorTruncation": true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.