Skip to content

Commit dafe6e1

Browse files
Merge pull request #865 from mogstad/push-uovqtzurwlpu
Prefer variables in scope over global variables
2 parents faab42c + 2c36249 commit dafe6e1

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -581,10 +581,10 @@ export function templateToTypescript(
581581
function treatAsGlobal(name: string): boolean {
582582
if (globals) {
583583
// If we have a known set of global identifiers, we should only treat
584-
// members of that set as global and assume everything else is local.
585-
// This is typically true in environments that capture scope, like
586-
// strict-mode Ember.
587-
return globals.includes(name);
584+
// members of that set as global, unless the identifier is in scope,
585+
// and assume everything else is local. This is typically true in
586+
// environments that capture scope, like strict-mode Ember.
587+
return globals.includes(name) && !scope.hasBinding(name);
588588
} else {
589589
// Otherwise, we assume everything is global unless we can see it
590590
// in scope as a block variable. This is the case in resolver-based

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

+22
Original file line numberDiff line numberDiff line change
@@ -1560,4 +1560,26 @@ describe('Transform: rewriteTemplate', () => {
15601560
]);
15611561
});
15621562
});
1563+
1564+
describe('global variables', () => {
1565+
test('uses vaariable in scope over global variable', () => {
1566+
let template = `
1567+
{{action "action"}}
1568+
{{#each actions as |action|}}
1569+
{{action}}
1570+
{{/each}}`;
1571+
1572+
expect(templateBody(template, { globals: ['action'] })).toMatchInlineSnapshot(`
1573+
"__glintDSL__.emitContent(__glintDSL__.resolve(__glintDSL__.Globals["action"])("action"));
1574+
{
1575+
const __glintY__ = __glintDSL__.emitComponent(__glintDSL__.resolve(each)(actions));
1576+
{
1577+
const [action] = __glintY__.blockParams["default"];
1578+
__glintDSL__.emitContent(__glintDSL__.resolveOrReturn(action)());
1579+
}
1580+
each;
1581+
}"
1582+
`);
1583+
});
1584+
});
15631585
});

0 commit comments

Comments
 (0)