diff --git a/packages/macros/src/glimmer/ast-transform.ts b/packages/macros/src/glimmer/ast-transform.ts index b7d0a3aa9..8fb071e96 100644 --- a/packages/macros/src/glimmer/ast-transform.ts +++ b/packages/macros/src/glimmer/ast-transform.ts @@ -77,7 +77,7 @@ export function makeFirstTransform(opts: FirstTransformParams) { } }, }, - SubExpression(node: any) { + SubExpression(node: any, walker: { parent: { node: any } }) { if (node.path.type !== 'PathExpression') { return; } @@ -97,7 +97,16 @@ export function makeFirstTransform(opts: FirstTransformParams) { ); } if (node.path.original === 'macroDependencySatisfies') { - return literal(dependencySatisfies(node, opts.packageRoot, moduleName, packageCache), env.syntax.builders); + const staticValue = literal( + dependencySatisfies(node, opts.packageRoot, moduleName, packageCache), + env.syntax.builders + ); + // If this is a macro expression by itself, then turn it into a macroCondition for the second pass to prune. + // Otherwise assume it's being composed with another macro and evaluate it as a literal + if (walker.parent.node.path.original === 'if') { + return env.syntax.builders.sexpr('macroCondition', [staticValue]); + } + return staticValue; } }, MustacheStatement(node: any) { diff --git a/packages/macros/tests/glimmer/dependency-satisfies.test.ts b/packages/macros/tests/glimmer/dependency-satisfies.test.ts index e8c389dc8..0531aa520 100644 --- a/packages/macros/tests/glimmer/dependency-satisfies.test.ts +++ b/packages/macros/tests/glimmer/dependency-satisfies.test.ts @@ -29,6 +29,11 @@ describe('dependency satisfies', () => { expect(result).toMatch(/@a=\{\{true\}\}/); }); + test('in branch', () => { + let result = transform(`{{#if (macroDependencySatisfies 'qunit' '^2.8.0')}}red{{else}}blue{{/if}}`, { filename }); + expect(result).toEqual('red'); + }); + test('emits false for out-of-range package', () => { let result = transform(`{{macroDependencySatisfies 'qunit' '^10.0.0'}}`, { filename }); expect(result).toEqual('{{false}}');