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

[Compiler] Semanti Walker to visit model derived types #5790

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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,7 @@
---
changeKind: feature
packages:
- "@typespec/compiler"
---

Add option for semantic walker to visit model derived types
11 changes: 11 additions & 0 deletions packages/compiler/src/core/semantic-walker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ export interface NavigationOptions {
* Skip non instantiated templates.
*/
includeTemplateDeclaration?: boolean;
/**
* Visit derived types.
*/
visitDerivedTypes?: boolean;
}

export interface NamespaceNavigationOptions {
Expand Down Expand Up @@ -253,6 +257,13 @@ function navigateModelType(model: Model, context: NavigationContext) {
if (model.indexer && model.indexer.value) {
navigateTypeInternal(model.indexer.value, context);
}

if (context.options.visitDerivedTypes) {
for (const derived of model.derivedModels) {
navigateModelType(derived, context);
}
}

context.emit("exitModel", model);
}

Expand Down
104 changes: 103 additions & 1 deletion packages/compiler/test/semantic-walker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,23 @@ import {
import {
getProperty,
navigateProgram,
navigateType,
navigateTypesInNamespace,
} from "../src/core/semantic-walker.js";
import { TestHost, createTestHost } from "../src/testing/index.js";
import {
BasicTestRunner,
TestHost,
createTestHost,
createTestRunner,
} from "../src/testing/index.js";

describe("compiler: semantic walker", () => {
let host: TestHost;
let runner: BasicTestRunner;

beforeEach(async () => {
host = await createTestHost();
runner = await createTestRunner();
});

function createCollector(customListener?: SemanticNodeListener) {
Expand Down Expand Up @@ -138,6 +146,100 @@ describe("compiler: semantic walker", () => {
return result;
}

it("finds derived models", async () => {
const { Bird } = (await runner.compile(`
namespace Test;

@discriminator("kind")
@test
model Bird {
kind: string;
wingspan: int32;
}

model SeaGull extends Bird {
kind: "seagull";
}

model Sparrow extends Bird {
kind: "sparrow";
}

model Goose extends Bird {
kind: "goose";
}

model Eagle extends Bird {
kind: "eagle";
friends?: Bird[];
hate?: Record<Bird>;
partner?: Bird;
}
`)) as { Bird: Model };

const visitedModels: Model[] = [];
navigateType(
Bird,
{
model(model) {
visitedModels.push(model);
},
},
{ includeTemplateDeclaration: false, visitDerivedTypes: true },
);

const expectedModels = ["Bird", "SeaGull", "Sparrow", "Goose", "Eagle"];
strictEqual(
expectedModels.every((element) => visitedModels.map((m) => m.name).includes(element)),
true,
);
});

it("doesn't visit derived models without the option", async () => {
const { Bird } = (await runner.compile(`
namespace Test;

@discriminator("kind")
@test
model Bird {
kind: string;
wingspan: int32;
}

model SeaGull extends Bird {
kind: "seagull";
}

model Sparrow extends Bird {
kind: "sparrow";
}

model Goose extends Bird {
kind: "goose";
}

model Eagle extends Bird {
kind: "eagle";
friends?: Bird[];
hate?: Record<Bird>;
partner?: Bird;
}
`)) as { Bird: Model };

const visitedModels: Model[] = [];
navigateType(
Bird,
{
model(model) {
visitedModels.push(model);
},
},
{ visitDerivedTypes: false },
);

strictEqual(visitedModels.length, 1);
});

it("finds models", async () => {
const result = await runNavigator(`
model Foo {
Expand Down
Loading