Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix branch elimination for macroDependencySatisfies #1688

Merged
merged 4 commits into from
Dec 13, 2023
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
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