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, 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( + + + + + + + + + + + + + const v1 = ; + const v1_1 = ; + + const v2 = ; + const v3 = ; + const v4 = ; + + + , + ); + + 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(