-
Notifications
You must be signed in to change notification settings - Fork 388
feat(compiler): allow declarations to be used as expressions #11019
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
Open
Timothee Guerin (timotheeguerin)
wants to merge
41
commits into
microsoft:main
Choose a base branch
from
timotheeguerin:decl-expr
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.
Open
Changes from all commits
Commits
Show all changes
41 commits
Select commit
Hold shift + click to select a range
638a365
feat(compiler): allow declarations to be used as expressions
timotheeguerin 053d3ff
fix(html-program-viewer): configure rendering for new expression prop…
timotheeguerin 0b7d1ea
test(compiler): use expect and flatten top-level describe in decl-exp…
timotheeguerin 0ceaad6
docs: add html-program-viewer changelog and clarify compiler changelo…
timotheeguerin f8ebdb7
fix(compiler): don't flatten keyword-form unions used as `|` operands
timotheeguerin f18990d
test(compiler): expand coverage for declarations as expressions
timotheeguerin 44e5e41
fix(compiler): correct type names for declaration expressions
timotheeguerin 9c2aaf0
Add syntax highlighting for declarations in expression position
timotheeguerin 8262855
Update language spec grammar for declarations in expression position
timotheeguerin 75ef483
fix: handle declaration expressions in emitters and versioning
timotheeguerin e3ecdd7
Merge remote-tracking branch 'upstream/main' into decl-expr
timotheeguerin 843db66
refactor(compiler): use dedicated AST nodes for declaration expressions
timotheeguerin 03f837d
feat(compiler): support inline and augment decorators on declaration …
timotheeguerin 0c57447
feat(compiler): declaration expressions in decorator arguments, doc c…
timotheeguerin d640056
Refine declaration expressions: parser recovery, typekit, printer, docs
timotheeguerin dbbf0de
Gate declaration expressions behind declaration-expressions feature flag
timotheeguerin 8495a48
Fix model is issue
timotheeguerin f7822f3
feat(compiler): break and indent decorators/doc comments on wide decl…
timotheeguerin a34aaee
Merge remote-tracking branch 'upstream/main' into decl-expr
timotheeguerin 0bfae19
Format
timotheeguerin 7ff1415
Format
timotheeguerin 62f14a8
fix test
timotheeguerin 5d4825e
test: add features option to Tester for enabling compiler features
timotheeguerin 6dd3521
Merge remote-tracking branch 'upstream/main' into decl-expr
timotheeguerin b0c8f36
Merge branch 'main' into decl-expr
timotheeguerin 8a8682a
fix(graphql): set required expression field when creating replacement…
timotheeguerin 537dd0b
test(compiler): fix indentation in declaration expression formatter t…
timotheeguerin 9fbccfd
docs(graphql): add changelog entry for scalar expression field fix
timotheeguerin 41ad6b8
Merge branch 'main' into decl-expr
timotheeguerin 3d09a5b
Merge upstream/main into decl-expr
timotheeguerin 02ca05c
Merge branch 'main' into decl-expr
timotheeguerin 79c30c8
Potential fix for pull request finding
timotheeguerin 7053830
fix: gate declaration expressions behind feature flag error
timotheeguerin a947f0b
Merge remote-tracking branch 'upstream/main' into decl-expr
timotheeguerin 8407316
Merge remote-tracking branch 'origin/decl-expr' into decl-expr
timotheeguerin 68a4502
Update ModelDeclarationExpression syntax for heritage
timotheeguerin f55b400
Merge remote-tracking branch 'upstream/main' into decl-expr
timotheeguerin 35e2e7b
Merge remote-tracking branch 'origin/decl-expr' into decl-expr
timotheeguerin c331435
fix: address review feedback on declaration expressions
timotheeguerin df03591
fix: reject anonymous declarations in typekit create
timotheeguerin 9076246
Merge remote-tracking branch 'upstream/main' into decl-expr
timotheeguerin 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
29 changes: 29 additions & 0 deletions
29
.chronus/changes/decl-expr-declaration-expressions-2026-4-18-0-0-0.md
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,29 @@ | ||
| --- | ||
| changeKind: feature | ||
| packages: | ||
| - "@typespec/compiler" | ||
| --- | ||
|
|
||
| Allow `model`, `enum`, `union`, and `scalar` declarations to be used as expressions. A declaration used in expression position has its corresponding type marked with `expression: true` and is not registered in the enclosing namespace. It may be named or anonymous (in which case it has no name). | ||
|
|
||
| This is an experimental feature that must be opted into by adding `declaration-expressions` to the `features` list in `tspconfig.yaml`; using a declaration expression without it reports a `declaration-expression-disabled` error. | ||
|
|
||
| They can be used anywhere an expression is expected, including aliases, model properties, decorator arguments, template arguments, function/call arguments, and tuples. | ||
|
|
||
| `model`, `scalar`, and `union` declaration expressions support the same `extends` clause as their statement form (union `extends` additionally requires the `union-extends` feature). | ||
|
|
||
| ```tsp | ||
| alias Foo = enum { | ||
| a, | ||
| b, | ||
| }; | ||
|
|
||
| model Bar { | ||
| status: enum { active, inactive }; | ||
| unit: scalar extends string; | ||
| inner: model Inner { x: string }; | ||
| } | ||
|
|
||
| @Versioning.versioned(enum Versions { v1, v2 }) | ||
| namespace MyService; | ||
| ``` |
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,16 @@ | ||
| --- | ||
| changeKind: feature | ||
| packages: | ||
| - "@typespec/compiler" | ||
| --- | ||
|
|
||
| Allow decorators to be applied to `model`, `enum`, `union`, and `scalar` declarations used in expression position. Inline decorators can be applied directly, and augment decorators (`@@`) can target them through a navigation reference (such as `::type`). | ||
|
|
||
| ```tsp | ||
| model Foo { | ||
| status: @doc("the current status") enum { active, inactive }; | ||
| inner: @doc("nested model") model Inner { x: string }; | ||
| } | ||
|
|
||
| @@doc(Foo.status::type, "the current status"); | ||
| ``` |
13 changes: 13 additions & 0 deletions
13
.chronus/changes/decl-expr-doc-comments-2026-4-18-0-0-3.md
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,13 @@ | ||
| --- | ||
| changeKind: feature | ||
| packages: | ||
| - "@typespec/compiler" | ||
| --- | ||
|
|
||
| Allow a doc comment to be applied inline to a `model`, `enum`, `union`, or `scalar` declaration expression, just like an inline `@doc` decorator. | ||
|
|
||
| ```tsp | ||
| model Foo { | ||
| status: /** the current status */ enum { active, inactive }; | ||
| } | ||
| ``` |
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,19 @@ | ||
| --- | ||
| changeKind: feature | ||
| packages: | ||
| - "@typespec/compiler" | ||
| --- | ||
|
|
||
| Improve formatting of declaration expressions (`model`, `enum`, `union`, and `scalar` used in expression position) that carry doc comments and/or decorators. When the inline form would exceed the print width, the doc comments and decorators are now each placed on their own line and the whole block is indented one level instead of overflowing. | ||
|
|
||
| ```tsp | ||
| model Foo { | ||
| status: | ||
| @summary("a fairly long summary text here") | ||
| @example("some-default-example-value") | ||
| enum { | ||
| active, | ||
| inactive, | ||
| }; | ||
| } | ||
| ``` |
15 changes: 15 additions & 0 deletions
15
.chronus/changes/decl-expr-json-schema-inline-2026-4-18-0-0-1.md
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,15 @@ | ||
| --- | ||
| changeKind: feature | ||
| packages: | ||
| - "@typespec/json-schema" | ||
| --- | ||
|
|
||
| Support `model`, `enum`, `union`, and `scalar` declarations used in expression position. Anonymous declaration expressions are inlined, while named ones are hoisted into their own schema. | ||
|
|
||
| ```tsp | ||
| model Foo { | ||
| status: enum { active, inactive }; // inlined | ||
| unit: scalar extends string; // inlined | ||
| inner: model Inner { x: string }; // hoisted as `Inner.json` | ||
| } | ||
| ``` |
16 changes: 16 additions & 0 deletions
16
.chronus/changes/decl-expr-openapi-inline-2026-4-18-0-0-1.md
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,16 @@ | ||
| --- | ||
| changeKind: feature | ||
| packages: | ||
| - "@typespec/openapi" | ||
| - "@typespec/openapi3" | ||
| --- | ||
|
|
||
| Support `model`, `enum`, `union`, and `scalar` declarations used in expression position. Anonymous declaration expressions are inlined, while named ones are hoisted into a referenced component. | ||
|
|
||
| ```tsp | ||
| model Foo { | ||
| status: enum { active, inactive }; // inlined | ||
| unit: scalar extends string; // inlined | ||
| inner: model Inner { x: string }; // hoisted as component `Inner` | ||
| } | ||
| ``` | ||
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,7 @@ | ||
| --- | ||
| changeKind: feature | ||
| packages: | ||
| - "@typespec/compiler" | ||
| --- | ||
|
|
||
| `$.enum.create` now produces an enum expression (`expression: true`) when given an empty `name`, mirroring `$.model.create`. |
7 changes: 7 additions & 0 deletions
7
.chronus/changes/decl-expr-versioning-validation-2026-4-18-0-0-1.md
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,7 @@ | ||
| --- | ||
| changeKind: feature | ||
| packages: | ||
| - "@typespec/versioning" | ||
| --- | ||
|
|
||
| Validate the variants of a keyword-form union expression (`union { ... }`) used in expression position like the variants of a named union, so versioning incompatibilities on decorated variants are reported. |
7 changes: 7 additions & 0 deletions
7
.chronus/changes/graphql-scalar-expression-field-2026-7-30-10-49-0.md
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,7 @@ | ||
| --- | ||
| changeKind: fix | ||
| packages: | ||
| - "@typespec/graphql" | ||
| --- | ||
|
|
||
| Set the required `expression` field to `false` when creating a replacement scalar so scalars produced by the model mutation engine are valid declaration types. |
7 changes: 7 additions & 0 deletions
7
.chronus/changes/html-program-viewer-expression-2026-4-18-0-0-0.md
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,7 @@ | ||
| --- | ||
| changeKind: feature | ||
| packages: | ||
| - "@typespec/html-program-viewer" | ||
| --- | ||
|
|
||
| Display the new `expression` property on `Model`, `Enum`, and `Scalar` types in the program viewer. |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.