-
Notifications
You must be signed in to change notification settings - Fork 277
Add typekit to list types under a container (namespace/interface) #7391
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
Draft
Copilot
wants to merge
6
commits into
main
Choose a base branch
from
copilot/fix-7367
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
cba85eb
Initial plan for issue
Copilot 9db8493
Add type.listUnder utility function to list types under a container
Copilot 78bf8bf
Remove top-level describe from type-utils.test.ts
Copilot 747a22a
Address review feedback
Copilot 9a633f8
Move type-utils.test.ts to correct location
Copilot b4d3801
Move type.test.ts from typekit/kits to typekit directory
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@typespec/compiler": minor | ||
--- | ||
|
||
@chronus/chronus | ||
Added `$.type.listUnder()` utility function to allow listing all types under a container (namespace/interface) that match a filter criteria. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import { Interface, Namespace, Type } from "../types.js"; | ||
import { isTemplateDeclaration } from "../type-utils.js"; | ||
|
||
/** | ||
* List types under the given container. Will list types recursively. | ||
* @param container Container. | ||
* @param filter Function to filter types. | ||
*/ | ||
export function listTypesUnder<T extends Type = Type>( | ||
container: Namespace | Interface, | ||
filter: (type: Type) => type is T, | ||
): T[]; | ||
|
||
/** | ||
* List types under the given container. Will list types recursively. | ||
* @param container Container. | ||
* @param filter Function to filter types. | ||
*/ | ||
export function listTypesUnder( | ||
container: Namespace | Interface, | ||
filter: (type: Type) => boolean, | ||
): Type[]; | ||
|
||
export function listTypesUnder( | ||
container: Namespace | Interface, | ||
filter: (type: Type) => boolean, | ||
): Type[] { | ||
const types: Type[] = []; | ||
|
||
function addTypes(current: Namespace | Interface) { | ||
if (isTemplateDeclaration(current)) { | ||
// Skip template types | ||
return; | ||
} | ||
|
||
// For interfaces, we only have operations | ||
if (current.kind === "Interface") { | ||
for (const op of current.operations.values()) { | ||
if (filter(op)) { | ||
types.push(op); | ||
} | ||
} | ||
return; | ||
} | ||
|
||
// For namespaces, check all contained type collections | ||
const namespace = current; | ||
|
||
// Check models | ||
for (const model of namespace.models.values()) { | ||
if (filter(model)) { | ||
types.push(model); | ||
} | ||
} | ||
|
||
// Check operations | ||
for (const op of namespace.operations.values()) { | ||
if (filter(op)) { | ||
types.push(op); | ||
} | ||
} | ||
|
||
// Check scalars | ||
for (const scalar of namespace.scalars.values()) { | ||
if (filter(scalar)) { | ||
types.push(scalar); | ||
} | ||
} | ||
|
||
// Check enums | ||
for (const enumType of namespace.enums.values()) { | ||
if (filter(enumType)) { | ||
types.push(enumType); | ||
} | ||
} | ||
|
||
// Check unions | ||
for (const union of namespace.unions.values()) { | ||
if (filter(union)) { | ||
types.push(union); | ||
} | ||
} | ||
|
||
// Check interfaces | ||
for (const iface of namespace.interfaces.values()) { | ||
if (filter(iface)) { | ||
types.push(iface); | ||
} | ||
} | ||
|
||
// Recursively check sub-namespaces | ||
for (const subNamespace of namespace.namespaces.values()) { | ||
if ( | ||
!( | ||
subNamespace.name === "Prototypes" && | ||
subNamespace.namespace?.name === "TypeSpec" && | ||
subNamespace.namespace.namespace?.name === "" | ||
) | ||
) { | ||
if (filter(subNamespace)) { | ||
types.push(subNamespace); | ||
} | ||
addTypes(subNamespace); | ||
} | ||
} | ||
|
||
// Recursively check operations in interfaces | ||
for (const iface of namespace.interfaces.values()) { | ||
addTypes(iface); | ||
} | ||
} | ||
|
||
addTypes(container); | ||
return types; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { Interface, Model, Namespace, Operation } from "../../../src/core/types.js"; | ||
import { createTestHost, TestHost } from "../../../src/testing/index.js"; | ||
import { listTypesUnder } from "../../../src/core/helpers/type-utils.js"; | ||
|
||
let host: TestHost; | ||
|
||
beforeEach(async () => { | ||
host = await createTestHost(); | ||
}); | ||
|
||
it("lists all types in a namespace", async () => { | ||
const testCode = ` | ||
namespace A { | ||
model M1 {} | ||
model M2 {} | ||
} | ||
`; | ||
|
||
const { A } = (await host.compile(testCode)) as { A: Namespace }; | ||
const types = listTypesUnder(A, (t) => true); | ||
|
||
// Should include 2 models plus namespace A | ||
expect(types.length).toBe(3); | ||
}); | ||
|
||
it("filters models", async () => { | ||
const testCode = ` | ||
namespace A { | ||
model M1 {} | ||
model M2 {} | ||
scalar S {} | ||
} | ||
`; | ||
|
||
const { A } = (await host.compile(testCode)) as { A: Namespace }; | ||
const models = listTypesUnder(A, (t): t is Model => t.kind === "Model"); | ||
|
||
expect(models.length).toBe(2); | ||
expect(models.every(m => m.kind === "Model")).toBe(true); | ||
}); | ||
|
||
it("handles nested namespaces", async () => { | ||
const testCode = ` | ||
namespace A { | ||
model M1 {} | ||
|
||
namespace B { | ||
model M2 {} | ||
} | ||
} | ||
`; | ||
|
||
const { A } = (await host.compile(testCode)) as { A: Namespace }; | ||
const models = listTypesUnder(A, (t): t is Model => t.kind === "Model"); | ||
|
||
expect(models.length).toBe(2); | ||
}); | ||
|
||
it("handles interfaces and operations", async () => { | ||
const testCode = ` | ||
namespace A { | ||
interface I1 { | ||
op1(): void; | ||
op2(): string; | ||
} | ||
} | ||
`; | ||
|
||
const { A } = (await host.compile(testCode)) as { A: Namespace }; | ||
const operations = listTypesUnder(A, (t): t is Operation => t.kind === "Operation"); | ||
const interfaces = listTypesUnder(A, (t): t is Interface => t.kind === "Interface"); | ||
|
||
expect(operations.length).toBe(2); | ||
expect(interfaces.length).toBe(1); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we use
@chronus/chronus
for changelogThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated to use
@chronus/chronus
for the changelog in 747a22a.