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

[Bug] Fix semantic walker not firing exitOperation or exitModelProperty #4381

Merged
merged 2 commits into from
Sep 10, 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: fix
packages:
- "@typespec/compiler"
---

Fix Semantic walker doesn't fire exitOperation or exitModelProperty
2 changes: 2 additions & 0 deletions packages/compiler/src/core/semantic-walker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ function navigateOperationType(operation: Operation, context: NavigationContext)
if (operation.sourceOperation) {
navigateTypeInternal(operation.sourceOperation, context);
}
context.emit("exitOperation", operation);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the late comment - but I think the exit methods still won't be executed if the pre-order callback sets no recursion. Maybe not a real issue if it's unlikely someone would use both pre and post-order callbacks.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh I thought that was the expected behavior, but makes sense, I will follow up on this

}

function navigateModelType(model: Model, context: NavigationContext) {
Expand Down Expand Up @@ -257,6 +258,7 @@ function navigateModelTypeProperty(property: ModelProperty, context: NavigationC
}
if (context.emit("modelProperty", property) === ListenerFlow.NoRecursion) return;
navigateTypeInternal(property.type, context);
context.emit("exitModelProperty", property);
}

function navigateScalarType(scalar: Scalar, context: NavigationContext) {
Expand Down
67 changes: 67 additions & 0 deletions packages/compiler/test/semantic-walker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@ describe("compiler: semantic walker", () => {
function createCollector(customListener?: SemanticNodeListener) {
const result = {
models: [] as Model[],
exitModels: [] as Model[],
modelProperties: [] as ModelProperty[],
exitModelProperties: [] as ModelProperty[],
namespaces: [] as Namespace[],
operations: [] as Operation[],
exitOperations: [] as Operation[],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any reason not to extend the post-order callbacks to every type? Looks like we might still be missing some like namespaces, interfaces, etc.

interfaces: [] as Interface[],
unions: [] as Union[],
unionVariants: [] as UnionVariant[],
Expand All @@ -46,14 +49,26 @@ describe("compiler: semantic walker", () => {
result.operations.push(x);
return customListener?.operation?.(x);
},
exitOperation: (x) => {
result.exitOperations.push(x);
return customListener?.exitOperation?.(x);
},
model: (x) => {
result.models.push(x);
return customListener?.model?.(x);
},
exitModel: (x) => {
result.exitModels.push(x);
return customListener?.exitModel?.(x);
},
modelProperty: (x) => {
result.modelProperties.push(x);
return customListener?.modelProperty?.(x);
},
exitModelProperty: (x) => {
result.exitModelProperties.push(x);
return customListener?.exitModelProperty?.(x);
},
union: (x) => {
result.unions.push(x);
return customListener?.union?.(x);
Expand Down Expand Up @@ -100,6 +115,25 @@ describe("compiler: semantic walker", () => {
strictEqual(result.models[2].name, "Bar");
});

it("finds exit models", async () => {
const result = await runNavigator(`
model Foo {
nested: {
inline: true
}
}

model Bar {
name: Foo;
}
`);

strictEqual(result.exitModels.length, 3);
strictEqual(result.exitModels[0].name, "", "Inline models don't have name");
strictEqual(result.exitModels[1].name, "Foo");
strictEqual(result.exitModels[2].name, "Bar");
});

it("finds operations", async () => {
const result = await runNavigator(`
op foo(): true;
Expand All @@ -114,6 +148,20 @@ describe("compiler: semantic walker", () => {
strictEqual(result.operations[1].name, "bar");
});

it("finds exit operations", async () => {
const result = await runNavigator(`
op foo(): true;

namespace Nested {
op bar(): true;
}
`);

strictEqual(result.exitOperations.length, 2);
strictEqual(result.exitOperations[0].name, "foo");
strictEqual(result.exitOperations[1].name, "bar");
});

it("finds namespaces", async () => {
const result = await runNavigator(`
namespace Global.My;
Expand Down Expand Up @@ -158,6 +206,25 @@ describe("compiler: semantic walker", () => {
strictEqual(result.modelProperties[2].name, "name");
});

it("finds exit model properties", async () => {
const result = await runNavigator(`
model Foo {
nested: {
inline: true
}
}

model Bar {
name: Foo;
}
`);

strictEqual(result.exitModelProperties.length, 3);
strictEqual(result.exitModelProperties[0].name, "inline");
strictEqual(result.exitModelProperties[1].name, "nested");
strictEqual(result.exitModelProperties[2].name, "name");
});

it("finds unions", async () => {
const result = await runNavigator(`
union A {
Expand Down
Loading