Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix expression defined in alias doesn't have namespace assigned #4146

Merged
merged 2 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading