Skip to content

Commit

Permalink
Merge pull request #1688 from mike-engel/glimmer-branch-elimination
Browse files Browse the repository at this point in the history
Fix branch elimination for `macroDependencySatisfies`
  • Loading branch information
mansona authored Dec 13, 2023
2 parents 496ab87 + 746124b commit 8b8f731
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
13 changes: 11 additions & 2 deletions packages/macros/src/glimmer/ast-transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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) {
Expand Down
5 changes: 5 additions & 0 deletions packages/macros/tests/glimmer/dependency-satisfies.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}}');
Expand Down

0 comments on commit 8b8f731

Please sign in to comment.