Skip to content

feat(compiler): allow inline decorators on declaration expressions - #31

Closed
timotheeguerin wants to merge 2 commits into
decl-exprfrom
decl-expr-inline-decorators
Closed

timotheeguerin wants to merge 2 commits into
decl-exprfrom
decl-expr-inline-decorators

Conversation

@timotheeguerin

Copy link
Copy Markdown
Owner

Stacked on top of the declarations-as-expressions work (microsoft#11019, branch decl-expr).

Enables applying decorators inline to model, enum, union, and scalar declarations used in expression position.

model Foo {
  status: @doc("the current status") enum { active, inactive };
  inner: @doc("nested model") model Inner { x: string };
}

Changes

  • Parser (parser.ts): the @ case in parsePrimaryExpression now parses a decorator list and, when followed by a model/enum/union/scalar keyword, dispatches to the corresponding parse*Statement (with pos captured at @ so the node span includes the decorators). Decorators before any other expression still report invalid-decorator-location ("Cannot decorate expression.").
  • Grammar spec (spec.emu.html): added the optional DecoratorList? prefix to the four *DeclarationExpression productions.
  • Formatter (printer.ts): the four print*Statement printers now pass tryInline: isInExpressionPosition(path), keeping decorators on the same line in expression position while statement-position decorators still break to their own line. Output is idempotent.

Tests

  • 5 checker tests: @doc applies to anonymous enum, named model, keyword union, and scalar declaration expressions (verified via getDoc), plus a negative case (decorator before a string reference still rejected).
  • 6 formatter tests covering inline decorators on each kind.
  • Full compiler suite: 4034 passed / 6 skipped.

Discussion

  • No checker changes were required — parse*Statement already plumbs decorators onto the AST node and the checker already applies node.decorators. The only blocker was the parser discarding decorators in expression position.

@timotheeguerin
timotheeguerin force-pushed the decl-expr-inline-decorators branch from 5894e7b to 24d23ff Compare June 22, 2026 16:49
@timotheeguerin

Copy link
Copy Markdown
Owner Author

Closing in favor of bundling this work directly into the base declaration-expressions PR microsoft#11019. Following a design-meeting decision, inline decorators (and augment decorators) on declaration expressions are now part of that single feature rather than a separate stacked change. The changes have been moved onto the decl-expr branch (commit 03f837d).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant