Skip to content

propero-oss/typescript-transformer-source-templates

Repository files navigation

@propero/typescript-transformer-source-templates

Maintainability Test Coverage Build Status

Generate fresh ASTs from source strings

npm i @propero/typescript-transformer-source-templates

Example Usage

import { template } from "@propero/typescript-transformer-source-templates";

const aggregationTemplate = template(`
let VARIABLE = control.getAggregation("AGGREGATION");
if (!Array.isArray(VARIABLE)) VARIABLE = VARIABLE == null ? [] : [VARIABLE];
VARIABLE.forEach(it => rm.renderControl(it));
`);
const headerNodes = aggregationTemplate.statements({ AGGREGATION: 'header', VARIABLE: "_header" });
const contentNodes = aggregationTemplate.statements({ AGGREGATION: 'content', VARIABLE: () => "_content" });
const footerNodes = aggregationTemplate.statements({ AGGREGATION: 'footer' });

Casting and member templates

import { template } from "@propero/typescript-transformer-source-templates";
import { MethodDeclaration } from "typescript";

const methodTemplate = template("render() {RENDER_CODE}");
const render = methodTemplate.objectMember<MethodDeclaration>({ RENDER_CODE: "return 'hello world';" });