Skip to content

Commit 33ee635

Browse files
Add discriminated union support with when clauses (#17)
Co-authored-by: Claude Haiku 4.5 <noreply@anthropic.com>
1 parent 23d00fb commit 33ee635

18 files changed

+465
-36
lines changed

.changeset/upset-eels-ask.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"urlspec-vscode-extension": minor
3+
"@urlspec/language": minor
4+
"@urlspec/builder": minor
5+
---
6+
7+
feat: support discriminated union with `when` syntax in page parameter

packages/builder/src/index.ts

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@ import {
1414
createTypeReference,
1515
createUnionType,
1616
createURLSpecDocument,
17+
createWhenClause,
1718
type GlobalBlock,
1819
type PageDeclaration,
1920
type ParameterDeclaration,
2021
type ParamTypeDeclaration,
2122
print,
2223
type Type,
2324
type URLSpecDocument,
25+
type WhenClause,
2426
} from "@urlspec/language";
2527

2628
// Re-export AST types
@@ -37,6 +39,7 @@ export type {
3739
TypeReference,
3840
UnionType,
3941
URLSpecDocument,
42+
WhenClause,
4043
} from "@urlspec/language";
4144
// Re-export AST builder functions for convenience
4245
export {
@@ -49,6 +52,7 @@ export {
4952
createTypeReference,
5053
createUnionType,
5154
createURLSpecDocument,
55+
createWhenClause,
5256
parsePath,
5357
} from "@urlspec/language";
5458

@@ -61,10 +65,21 @@ export interface ParameterDefinition {
6165
comment?: string;
6266
}
6367

68+
export interface VariantDefinition {
69+
value: string;
70+
parameters?: ParameterDefinition[];
71+
}
72+
73+
export interface DiscriminatedVariants {
74+
discriminant: string;
75+
variants: VariantDefinition[];
76+
}
77+
6478
export interface PageDefinition {
6579
name: string;
6680
path: string;
6781
parameters?: ParameterDefinition[];
82+
when?: DiscriminatedVariants;
6883
comment?: string;
6984
}
7085

@@ -120,14 +135,24 @@ export class URLSpec {
120135
}
121136

122137
// Build pages
123-
const pages: PageDeclaration[] = this.pages.map((page) =>
124-
createPageDeclaration(
138+
const pages: PageDeclaration[] = this.pages.map((page) => {
139+
const whenClauses: WhenClause[] = page.when
140+
? page.when.variants.map((v) =>
141+
createWhenClause(
142+
page.when!.discriminant,
143+
v.value,
144+
v.parameters?.map((p) => this.buildParameter(p)) ?? [],
145+
),
146+
)
147+
: [];
148+
return createPageDeclaration(
125149
page.name,
126150
page.path,
127151
page.parameters?.map((p) => this.buildParameter(p)),
128152
page.comment,
129-
),
130-
);
153+
whenClauses.length > 0 ? whenClauses : undefined,
154+
);
155+
});
131156

132157
return createURLSpecDocument({
133158
paramTypes,

packages/language/src/__generated__/ast.ts

Lines changed: 47 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)