Skip to content

Commit

Permalink
Fix expression defined in alias doesn't have namespace assigned (micr…
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheeguerin authored and sarangan12 committed Sep 16, 2024
1 parent b7f6bb0 commit 9255c52
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
8 changes: 8 additions & 0 deletions .chronus/changes/fix-alias-expr-2024-7-12-19-19-19.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking
changeKind: fix
packages:
- "@typespec/compiler"
---

Fix model expression defined in alias will resolve its namespace from the namespace where the alias was declared
2 changes: 2 additions & 0 deletions packages/compiler/src/core/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2409,6 +2409,7 @@ export function createChecker(program: Program): Checker {

function getParentNamespaceType(
node:
| AliasStatementNode
| ModelStatementNode
| ScalarStatementNode
| NamespaceStatementNode
Expand All @@ -2432,6 +2433,7 @@ export function createChecker(program: Program): Checker {
let parent: Node | undefined = node.parent;
while (parent !== undefined) {
if (
parent.kind === SyntaxKind.AliasStatement ||
parent.kind === SyntaxKind.ModelStatement ||
parent.kind === SyntaxKind.ScalarStatement ||
parent.kind === SyntaxKind.OperationStatement ||
Expand Down
24 changes: 23 additions & 1 deletion packages/compiler/test/checker/alias.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ok, strictEqual } from "assert";
import { beforeEach, describe, it } from "vitest";
import { Model, Type, Union } from "../../src/core/types.js";
import { Model, Namespace, Type, Union } from "../../src/core/types.js";
import {
TestHost,
createTestHost,
Expand Down Expand Up @@ -194,6 +194,28 @@ describe("compiler: aliases", () => {
strictEqual(Baz.properties.get("x")!.type, Bar);
});

it("model expression defined in alias use containing namespace", async () => {
testHost.addTypeSpecFile(
"main.tsp",
`
@test namespace Foo {
alias B = {a: string};
}
@test model Test {
prop: Foo.B;
}
`
);

const { Test, Foo } = (await testHost.compile("./")) as {
Foo: Namespace;
Test: Model;
};

const expr = Test.properties.get("prop")!.type as Model;
strictEqual(expr.namespace, Foo);
});

it("emit diagnostics if assign itself", async () => {
testHost.addTypeSpecFile(
"main.tsp",
Expand Down

0 comments on commit 9255c52

Please sign in to comment.