Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 32 additions & 7 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,47 @@ import { type TemplateInput, Typebars } from "./src";

const tp = new Typebars();

const params = { accountId: "{{accountId}}", ok: "salut" };
const params = {
names: '{{#if hasUsers}}{{map users "name"}}{{else}}{{defaultNames}}{{/if}}',
};

const coerceSchema = {
const coerceSchema = {} as JSONSchema7;

const inputSchema = {
type: "object",
properties: {
ok: { type: "string", constraints: "IsUuid" },
accountId: { type: "string", constraints: "IsUuid" },
defaultNames: {
type: "array",
items: {
type: "string",
},
},
hasUsers: {
type: "boolean",
},
users: {
type: "array",
items: {
type: "object",
properties: {
name: {
type: "string",
},
},
},
},
},
} as JSONSchema7;

const result = tp.analyzeAndExecute(
params as TemplateInput,
undefined,
{},
inputSchema,
{
hasUsers: false,
defaultNames: ["alice", "bob"],
users: [],
},
{
excludeTemplateExpression: true,
coerceSchema,
},
);
Expand Down
88 changes: 86 additions & 2 deletions src/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,16 @@ export function executeFromAst(
}

// ── Case 3: single block (possibly surrounded by whitespace) ─────────
// Render via Handlebars then attempt to coerce the result to the
// detected literal type (number, boolean, null).
// For conditional blocks (#if/#unless), try to evaluate the condition
// and execute the selected branch directly to preserve non-string types
// (e.g. arrays from `map`). Falls back to Handlebars rendering.
const singleBlock = getEffectivelySingleBlock(ast);
if (singleBlock) {
const directResult = tryDirectBlockExecution(singleBlock, data, ctx);
if (directResult !== undefined) {
return directResult.value;
}

const merged = mergeDataWithIdentifiers(data, identifierData);
const raw = renderWithHandlebars(template, merged, ctx);
return coerceValue(raw, ctx?.coerceSchema);
Expand Down Expand Up @@ -584,6 +590,84 @@ export function clearCompilationCache(): void {
globalCompilationCache.clear();
}

// ─── Direct Block Execution ──────────────────────────────────────────────────
// For conditional blocks (#if/#unless), we can evaluate the condition directly
// and execute the selected branch through the type-preserving execution paths.
// This avoids Handlebars stringification when the branch contains helpers that
// return non-primitive values (e.g. `map` returning arrays).

/**
* Attempts to execute a conditional block directly by evaluating its condition
* and executing the selected branch through type-preserving paths.
*
* Only handles `#if` and `#unless` blocks. Returns `{ value }` if the branch
* was executed directly, or `undefined` to fall back to Handlebars rendering.
*/
function tryDirectBlockExecution(
block: hbs.AST.BlockStatement,
data: unknown,
ctx?: ExecutorContext,
): { value: unknown } | undefined {
if (block.path.type !== "PathExpression") return undefined;
const helperName = (block.path as hbs.AST.PathExpression).original;

// Only handle built-in conditional blocks
if (helperName !== "if" && helperName !== "unless") return undefined;
if (block.params.length !== 1) return undefined;

// Evaluate the condition
const condition = resolveExpression(
block.params[0] as hbs.AST.Expression,
data,
ctx?.identifierData,
ctx?.helpers,
);

// Handlebars truthiness: empty arrays are falsy
let isTruthy: boolean;
if (Array.isArray(condition)) {
isTruthy = condition.length > 0;
} else {
isTruthy = !!condition;
}
if (helperName === "unless") isTruthy = !isTruthy;

const branch = isTruthy ? block.program : block.inverse;
if (!branch) {
// No matching branch (e.g. falsy #if with no {{else}}) → empty string
return { value: "" };
}

// Try to execute the branch as a single expression (preserves types)
const singleExpr = getEffectivelySingleExpression(branch);
if (singleExpr) {
if (singleExpr.params.length === 0 && !singleExpr.hash) {
return {
value: resolveExpression(
singleExpr.path,
data,
ctx?.identifierData,
ctx?.helpers,
),
};
}
// Single expression with helper (e.g. {{map users "name"}})
if (singleExpr.params.length > 0 || singleExpr.hash) {
const directResult = tryDirectHelperExecution(singleExpr, data, ctx);
if (directResult !== undefined) return directResult;
}
}

// Try to execute the branch as a nested conditional block (recursive)
const nestedBlock = getEffectivelySingleBlock(branch);
if (nestedBlock) {
return tryDirectBlockExecution(nestedBlock, data, ctx);
}

// Branch is too complex for direct execution → fall back
return undefined;
}

// ─── Direct Helper Execution ─────────────────────────────────────────────────
// Some helpers (e.g. `map`) return non-primitive values (arrays, objects)
// that Handlebars would stringify. For these helpers, we resolve their
Expand Down
Loading
Loading