Skip to content

Keep decorator expressions in the AST when no class expression follows - #65796

Draft
Ryan Cavanaugh (RyanCavanaugh) wants to merge 3 commits into
mainfrom
copilot/fix-assertion-failure-document-highlights
Draft

Ryan Cavanaugh (RyanCavanaugh) wants to merge 3 commits into
mainfrom
copilot/fix-assertion-failure-document-highlights

Conversation

@RyanCavanaugh

@RyanCavanaugh Ryan Cavanaugh (RyanCavanaugh) commented Dec 4, 2026 •

Copy link
Copy Markdown
Member

This is an automated PR authored by GitHub Copilot using Claude Opus with default reasoning level.

Motivation & Guidance

Motivation: Copilot Coding Agent was assigned the issue.

Guidance: None beyond the issue. A rubber-duck review recommended wrapping the multi-decorator recovery in parentheses.

Analysis

declare function dec(x: any): any;
const x = @dec 1;

Document highlights panics here with did not expect KindVariableDeclaration to have KindIdentifier in its trivia.

Since #65496, parseDecoratedExpression returns a missing identifier when no class follows. That drops the parsed decorators, so dec is left in the VariableDeclaration's trivia, which astnav asserts against. Going back to returning a MissingDeclaration would contradict #65496, since a declaration isn't an expression.

Fix

The "Expression expected" diagnostic is unchanged; only the returned node differs. It is built from the decorators' own expressions, with no lookahead:

Source Recovered expression
x = @dec 1 x = dec
x = @a @b 1 x = (a, b)
x = @a @b[0] x = (a, b)[0]
  • Multiple decorators are joined with zero-width synthetic commas (as the JSX "must have one parent element" recovery does), then wrapped in a synthetic ParenthesizedExpression. Without the wrapper, a bare BinaryExpression would be returned where a primary expression is expected.
  • Non-decorator modifiers (e.g. @a public @b) are dropped. Keyword tokens in trivia are fine for astnav.

Baseline changes worth reviewing

  • decoratorOnArrowFunction returns to its pre-Return a missing identifier from parseDecoratedExpression when no class follows #65496 parse and errors, and decoratorOnFunctionExpression gets its dec symbol back.
  • Decorator expressions are now checked as ordinary expressions. Expect TS2304 for undeclared names, and TS2554 for @dec() when dec requires an argument.
  • With multiple decorators, the synthetic comma also reports TS2695 ("Left side of comma operator is unused"). The checker suppresses this only for the JSX recovery comma. Extending that suppression to zero-width commas would be a small follow-up; it is left out here to keep the diff focused.

Tests

  • New fourslash test TestDocumentHighlightsDecoratorOnNonClassExpression covers the issue's two markers plus a two-decorator case.
  • decoratorMissingClassExpression.ts gains x = @a @b 1; and x = @a @b[0];.

…ass expression

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
@RyanCavanaugh Ryan Cavanaugh (RyanCavanaugh) changed the title [WIP] Fix assertion failure for document highlights Keep decorator expressions in the AST when no class expression follows Dec 4, 2026

This branch has not been deployed

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

Labels

Author: Team For Uncommitted Bug PR for untriaged, rejected, closed or missing bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Assertion failure for document highlights on decorator preceding non-class expression

2 participants