Skip to content

Commit

Permalink
Fix import scope (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
bterlson authored Sep 23, 2024
1 parent 5e7b1e4 commit 68153e4
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
7 changes: 7 additions & 0 deletions .chronus/changes/fix-import-scope-2024-8-23-14-56-56.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: fix
packages:
- "@alloy-js/typescript"
---

Fix scope for local import symbols.
3 changes: 2 additions & 1 deletion packages/typescript/src/symbols/ts-module-scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)!;
}
Expand All @@ -49,6 +49,7 @@ export function createTSModuleScope(

const localSymbol = createTSSymbol({
binder,
scope: this,
name: targetSymbol.name,
refkey: refkey({}),
tsFlags: TSSymbolFlags.LocalImportSymbol,
Expand Down
57 changes: 57 additions & 0 deletions packages/typescript/test/imports.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down

0 comments on commit 68153e4

Please sign in to comment.