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

fix [#3438](https://github.com/microsoft/typespec/issues/3438)
This commit is contained in:
Timothee Guerin 2024-08-12 14:56:46 -07:00 коммит произвёл GitHub
Родитель 275d76e66a
Коммит 88d22b0f48
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
3 изменённых файлов: 33 добавлений и 1 удалений

Просмотреть файл

@ -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

Просмотреть файл

@ -2409,6 +2409,7 @@ export function createChecker(program: Program): Checker {
function getParentNamespaceType(
node:
| AliasStatementNode
| ModelStatementNode
| ScalarStatementNode
| NamespaceStatementNode
@ -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 ||

Просмотреть файл

@ -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,
@ -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",