Repository: alloy-framework/alloy
Pull request #34 (2024-09-23 22:00:47)

Fix import scope

github-actions[bot]: <!--chronus-github-change-commenter-->
All changed packages have been documented.
 - :white_check_mark: `@alloy-js/typescript`

<details>
<summary>Show changes</summary>

### `@alloy-js/typescript` - _fix_ [✏️](https://github.com/bterlson/alloy/edit/fix-import-scope/.chronus/changes/fix-import-scope-2024-8-23-14-56-56.md?pr=/alloy-framework/alloy/pull/34)
> Fix scope for local import symbols.
</details>


Patch:
diff --git a/.chronus/changes/fix-import-scope-2024-8-23-14-56-56.md b/.chronus/changes/fix-import-scope-2024-8-23-14-56-56.md
new file mode 100644
index 0000000..66d41f4
--- /dev/null
+++ b/.chronus/changes/fix-import-scope-2024-8-23-14-56-56.md
@@ -0,0 +1,7 @@
+---
+changeKind: fix
+packages:
+  - "@alloy-js/typescript"
+---
+
+Fix scope for local import symbols.
\ No newline at end of file
diff --git a/packages/typescript/src/symbols/ts-module-scope.ts b/packages/typescript/src/symbols/ts-module-scope.ts
index 3d4c37c..4d2c7bc 100644
--- a/packages/typescript/src/symbols/ts-module-scope.ts
+++ b/packages/typescript/src/symbols/ts-module-scope.ts
@@ -34,7 +34,7 @@ export function createTSModuleScope(
     exportedSymbols: new Map(),
     importedSymbols: new Map(),
     importedModules: new Map(),
-    addImport(targetSymbol, targetModule) {
+    addImport(this: TSModuleScope, targetSymbol, targetModule) {
       if (this.importedSymbols.has(targetSymbol)) {
         return this.importedSymbols.get(targetSymbol)!;
       }
@@ -49,6 +49,7 @@ export function createTSModuleScope(
 
       const localSymbol = createTSSymbol({
         binder,
+        scope: this,
         name: targetSymbol.name,
         refkey: refkey({}),
         tsFlags: TSSymbolFlags.LocalImportSymbol,


Test patch:
diff --git a/packages/typescript/test/imports.test.tsx b/packages/typescript/test/imports.test.tsx
index 106c692..803b398 100644
--- a/packages/typescript/test/imports.test.tsx
+++ b/packages/typescript/test/imports.test.tsx
@@ -146,6 +146,63 @@ it("works with default and named imports and name conflicts", () => {
   });
 });
 
+it("works with default and named imports and name conflicts and references in nested scopes", () => {
+  const res = render(
+    <Output nameConflictResolver={tsNameConflictResolver}>
+      <ts.SourceFile path="test1.ts">
+        <ts.FunctionDeclaration export default name="test1" />
+        <ts.FunctionDeclaration export name="test2" />
+      </ts.SourceFile>
+
+      <ts.SourceFile path="test2.ts">
+        <ts.FunctionDeclaration export default name="test1" refkey={refkey("test3")} />
+        <ts.FunctionDeclaration export name="test2" refkey={refkey("test4")} />
+      </ts.SourceFile>
+
+      <ts.SourceFile path="test3.ts">
+        const v1 = <Reference refkey={refkey("test1")} />;
+        const v1_1 = <Reference refkey={refkey("test2")}/>;
+        <ts.FunctionDeclaration name="foo">
+          const v2 = <Reference refkey={refkey("test3")} />;
+          const v3 = <Reference refkey={refkey("test3")}/>;
+          const v4 = <Reference refkey={refkey("test4")} />;
+        </ts.FunctionDeclaration>
+      </ts.SourceFile>
+    </Output>,
+  );
+
+  assertFileContents(res, {
+    "test1.ts": `
+      export default function test1() {
+        
+      }
+      export function test2() {
+        
+      }
+    `,
+    "test2.ts": `
+      export default function test1() {
+        
+      }
+      export function test2() {
+        
+      }
+    `,
+    "test3.ts": `
+      import test1_1, { test2 as test2_1 } from "./test1.js";
+      import test1_2, { test2 as test2_2 } from "./test2.js";
+
+      const v1 = test1_1;
+      const v1_1 = test2_1;
+      function foo() {
+        const v2 = test1_2;
+        const v3 = test1_2;
+        const v4 = test2_2;
+      }
+    `,
+  });
+});
+
 it("works with imports from different directories", () => {
   const res = render(
     <Output>