Skip to content

Commit 67868cf

Browse files
committed
oh
1 parent 45026b1 commit 67868cf

File tree

2 files changed

+93
-12
lines changed

2 files changed

+93
-12
lines changed

packages/core/__tests__/transform/template-to-typescript.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,15 @@ describe('Transform: rewriteTemplate', () => {
836836
}"
837837
`);
838838
});
839+
840+
test('in a conditional expression', () => {
841+
let template = `{{(if @onSelect (modifier on "click" @onSelect))}}`;
842+
let specialForms = { if: 'if' } as const;
843+
844+
expect(templateBody(template, { globals: ['on'], specialForms })).toMatchInlineSnapshot(`
845+
"__glintDSL__.emitContent(__glintDSL__.resolveOrReturn((__glintDSL__.noop(__if), (__glintRef__.args.onSelect) ? (__glintDSL__.applyModifier(__glintDSL__.resolve(__glintDSL__.Globals["on"])(__glintY__.element, "click", __glintRef__.args.onSelect))) : (null)))());"
846+
`);
847+
});
839848
});
840849

841850
describe('subexpressions', () => {

packages/core/src/transform/template/template-to-typescript.ts

Lines changed: 84 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -355,20 +355,92 @@ export function templateToTypescript(
355355
node.params.length >= 2,
356356
() => `{{${formInfo.name}}} requires at least two parameters`,
357357
);
358-
359-
mapper.text('(');
360-
emitExpression(node.params[0]);
361-
mapper.text(') ? (');
362-
emitExpression(node.params[1]);
363-
mapper.text(') : (');
364-
365-
if (node.params[2]) {
366-
emitExpression(node.params[2]);
358+
359+
// Check if this is a conditional modifier case
360+
const isModifierHelper =
361+
node.params[1].type === 'SubExpression' &&
362+
node.params[1].path.type === 'PathExpression' &&
363+
node.params[1].path.original === 'modifier';
364+
365+
if (isModifierHelper) {
366+
// For conditional modifiers, we need to handle them specially
367+
// to avoid type instantiation depth issues
368+
mapper.text('(');
369+
emitExpression(node.params[0]);
370+
mapper.text(') ? (');
371+
372+
// For the truthy case, we emit the modifier directly
373+
if (node.params[1].type === 'SubExpression') {
374+
// Extract the modifier and its arguments
375+
const modifierExpr = node.params[1];
376+
if (modifierExpr.params.length > 0 && modifierExpr.params[0].type === 'PathExpression') {
377+
mapper.text('__glintDSL__.applyModifier(__glintDSL__.resolve(');
378+
emitExpression(modifierExpr.params[0]);
379+
mapper.text(')(__glintY__.element, ');
380+
381+
// Skip the first param (the modifier itself) and emit the rest
382+
const restParams = modifierExpr.params.slice(1);
383+
for (let [index, param] of restParams.entries()) {
384+
if (index > 0) {
385+
mapper.text(', ');
386+
}
387+
emitExpression(param);
388+
}
389+
390+
// Handle named args if any
391+
if (modifierExpr.hash.pairs.length) {
392+
if (restParams.length) {
393+
mapper.text(', ');
394+
}
395+
396+
mapper.text('{ ');
397+
for (let [index, pair] of modifierExpr.hash.pairs.entries()) {
398+
mapper.text(`${pair.key}: `);
399+
emitExpression(pair.value);
400+
401+
if (index < modifierExpr.hash.pairs.length - 1) {
402+
mapper.text(', ');
403+
}
404+
}
405+
mapper.text(' }');
406+
}
407+
408+
mapper.text('))');
409+
} else {
410+
// Fallback to normal emission if structure is unexpected
411+
emitExpression(node.params[1]);
412+
}
413+
} else {
414+
// Fallback to normal emission if structure is unexpected
415+
emitExpression(node.params[1]);
416+
}
417+
418+
mapper.text(') : (');
419+
420+
// For the falsy case, emit null or the else branch
421+
if (node.params[2]) {
422+
emitExpression(node.params[2]);
423+
} else {
424+
mapper.text('null');
425+
}
426+
427+
mapper.text(')');
367428
} else {
368-
mapper.text('undefined');
429+
// Standard if expression handling (unchanged)
430+
mapper.text('(');
431+
emitExpression(node.params[0]);
432+
mapper.text(') ? (');
433+
emitExpression(node.params[1]);
434+
mapper.text(') : (');
435+
436+
if (node.params[2]) {
437+
emitExpression(node.params[2]);
438+
} else {
439+
mapper.text('undefined');
440+
}
441+
442+
mapper.text(')');
369443
}
370-
371-
mapper.text(')');
372444
});
373445
}
374446

0 commit comments

Comments
 (0)