From 2c36249014cd67c3d9ec234e10e9144c6a1a388d Mon Sep 17 00:00:00 2001 From: Bjarne Mogstad Date: Sun, 30 Mar 2025 13:33:42 +0200 Subject: [PATCH] Prefer variables in scope over global variables --- .../template/template-to-typescript.ts | 8 +++---- .../transform/template-to-typescript.test.ts | 22 +++++++++++++++++++ 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/packages/core/src/transform/template/template-to-typescript.ts b/packages/core/src/transform/template/template-to-typescript.ts index 2060f8623..414368b24 100644 --- a/packages/core/src/transform/template/template-to-typescript.ts +++ b/packages/core/src/transform/template/template-to-typescript.ts @@ -574,10 +574,10 @@ export function templateToTypescript( function treatAsGlobal(name: string): boolean { if (globals) { // If we have a known set of global identifiers, we should only treat - // members of that set as global and assume everything else is local. - // This is typically true in environments that capture scope, like - // strict-mode Ember. - return globals.includes(name); + // members of that set as global, unless the identifier is in scope, + // and assume everything else is local. This is typically true in + // environments that capture scope, like strict-mode Ember. + return globals.includes(name) && !scope.hasBinding(name); } else { // Otherwise, we assume everything is global unless we can see it // in scope as a block variable. This is the case in resolver-based diff --git a/test-packages/package-test-core/__tests__/transform/template-to-typescript.test.ts b/test-packages/package-test-core/__tests__/transform/template-to-typescript.test.ts index 416e3b2d5..aa9a65ad6 100644 --- a/test-packages/package-test-core/__tests__/transform/template-to-typescript.test.ts +++ b/test-packages/package-test-core/__tests__/transform/template-to-typescript.test.ts @@ -1491,4 +1491,26 @@ describe('Transform: rewriteTemplate', () => { ]); }); }); + + describe('global variables', () => { + test('uses vaariable in scope over global variable', () => { + let template = ` + {{action "action"}} + {{#each actions as |action|}} + {{action}} + {{/each}}`; + + expect(templateBody(template, { globals: ['action'] })).toMatchInlineSnapshot(` + "__glintDSL__.emitContent(__glintDSL__.resolve(__glintDSL__.Globals["action"])("action")); + { + const __glintY__ = __glintDSL__.emitComponent(__glintDSL__.resolve(each)(actions)); + { + const [action] = __glintY__.blockParams["default"]; + __glintDSL__.emitContent(__glintDSL__.resolveOrReturn(action)()); + } + each; + }" + `); + }); + }); });