Skip to content

Instantly share code, notes, and snippets.

@vkbansal
Last active April 17, 2019 14:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vkbansal/379460892f12e0b8bb50434e52e6368f to your computer and use it in GitHub Desktop.
Save vkbansal/379460892f12e0b8bb50434e52e6368f to your computer and use it in GitHub Desktop.
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