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

Resolve alias of alias #2627

Merged
merged 2 commits into from
Nov 2, 2023
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@typespec/compiler",
"comment": "",
"type": "none"
}
],
"packageName": "@typespec/compiler"
}
17 changes: 12 additions & 5 deletions packages/compiler/src/core/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2344,12 +2344,19 @@ export function createChecker(program: Program): Checker {
mapper: TypeMapper | undefined,
options: SymbolResolutionOptions
): Sym | undefined {
const node = aliasSymbol.declarations[0];
const targetNode = node.kind === SyntaxKind.AliasStatement ? node.value : node;
const sym = resolveTypeReferenceSymInternal(targetNode as any, mapper, options);
if (sym === undefined) {
return undefined;
let current = aliasSymbol;
while (current.flags & SymbolFlags.Alias) {
const node = current.declarations[0];
const targetNode = node.kind === SyntaxKind.AliasStatement ? node.value : node;
const sym = resolveTypeReferenceSymInternal(targetNode as any, mapper, options);
if (sym === undefined) {
return undefined;
}
current = sym;
}
const sym = current;
const node = aliasSymbol.declarations[0];

const resolvedTargetNode = sym.declarations[0];
if (!options.checkTemplateTypes || !isTemplatedNode(resolvedTargetNode)) {
return sym;
Expand Down
24 changes: 24 additions & 0 deletions packages/compiler/test/checker/references.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ describe("compiler: references", () => {
ref: "MyModel.x",
}));

describe("member of alias of alias of model", () =>
itCanReference({
code: `
model MyModel {
@test("target") x: string;
}
alias Alias1 = MyModel;
alias MyModelAlias = Alias1;
`,
ref: "MyModelAlias.x",
}));

describe("spread property from model defined before", () =>
itCanReference({
code: `
Expand Down Expand Up @@ -421,6 +433,18 @@ describe("compiler: references", () => {
ref: "MyInterfaceAlias.operation",
}));

describe("member of alias of alias of interface", () =>
itCanReference({
code: `
interface MyInterface {
@test("target") operation(): void;
}
alias MyInterfaceAlias1 = MyInterface;
alias MyInterfaceAlias = MyInterfaceAlias1;
`,
ref: "MyInterfaceAlias.operation",
}));

describe("member of templated interface instance", () =>
itCanReference({
code: `
Expand Down