Skip to content

Report an error for class declarations in statement position - #65005

Merged
Ryan Cavanaugh (RyanCavanaugh) merged 1 commit into
microsoft:mainfrom
TimonGaertner:class-declaration-in-statement-position
Oct 29, 2026
Merged

Ryan Cavanaugh (RyanCavanaugh) merged 1 commit into
microsoft:mainfrom
TimonGaertner:class-declaration-in-statement-position

Conversation

@TimonGaertner

Copy link
Copy Markdown
Contributor

Fixes #22682

A class declaration used directly as the body of an if, else, loop, or labeled statement (for example if (cond) class C {}) is rejected by JavaScript engines with a SyntaxError, but tsc currently accepts it without any error, in both .ts and plain .js files. interface and type declarations in the same position already report TS1156 ("... declarations can only be declared inside a block").

This adds the same check to checkClassDeclaration, reusing containerAllowsBlockScopedVariable, so the class case reports:

error TS1156: 'class' declarations can only be declared inside a block.

Class expressions (return class C {}, x = class C {}) and class declarations in blocks, case clauses, namespaces, function bodies and at the top level are unaffected; the test covers these. TS1156 is already on the plain-JS diagnostic allowlist, so .js files get the error too.

Note: I limited this to class, following the clarified repro in the issue. enum in the same position is valid TypeScript (just useless, as noted in the issue), so I left it alone; happy to add it if wanted.

Test: new compiler test classDeclarationInStatementPosition.ts; no existing baselines change.

AI disclosure: I used Claude Code to help investigate the issue and write this change. I have reviewed and tested the result myself, understand it, and will handle the review discussion.

A class declaration used directly as the body of an if, else, loop or
labeled statement is a syntax error at runtime, but was accepted without
error. Report TS1156, as is already done for interface and type alias
declarations.

Fixes microsoft#22682
Copilot AI balanced review requested due to automatic review settings October 29, 2026 01:51
@github-project-automation github-project-automation Bot moved this to Not started in PR Backlog Oct 29, 2026
@typescript-automation typescript-automation Bot added the For Backlog Bug PRs that fix a backlog bug label Oct 29, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are reported for the change that diagnoses class declarations used directly as statement bodies.

No open issues 🎉

What changed in this PR

Addresses #22682 by making the native TypeScript checker report TS1156 for class declarations used directly as statement bodies in TypeScript and JavaScript files.

Changes:

  • Reuses the existing block-scope container check for class declarations.
  • Adds regression coverage for invalid statement positions, valid declaration locations, and class expressions.
  • Adds diagnostic, JavaScript emit, type, and symbol baselines.
File Description
tsc/​testdata/​tests/​cases/​compiler/​classDeclarationInStatementPosition.ts Tests invalid statement positions and unaffected valid cases.
tsc/​testdata/​baselines/​reference/​compiler/​classDeclarationInStatementPosition.types Records expected type information.
tsc/​testdata/​baselines/​reference/​compiler/​classDeclarationInStatementPosition.symbols Records expected symbol information.
tsc/​testdata/​baselines/​reference/​compiler/​classDeclarationInStatementPosition.js Records JavaScript emit for the regression test.
tsc/​testdata/​baselines/​reference/​compiler/​classDeclarationInStatementPosition.errors.txt Records expected diagnostics, including TS1156 in plain JavaScript.
tsc/​internal/​checker/​checker.go Rejects class declarations in disallowed statement positions.

Review effort: Balanced

@TimonGaertner

Copy link
Copy Markdown
Contributor Author

@microsoft-github-policy-service agree

@RyanCavanaugh

Copy link
Copy Markdown
Member

Timon Gärtner (@TimonGaertner) this is an unusually good test, what model and reasoning effort were you using?

@github-project-automation github-project-automation Bot moved this from Not started to Needs merge in PR Backlog Oct 29, 2026
Merged via the queue into microsoft:main with commit e5ae01b Oct 29, 2026
29 checks passed
@github-project-automation github-project-automation Bot moved this from Needs merge to Done in PR Backlog Oct 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

For Backlog Bug PRs that fix a backlog bug

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Missing error if ClassDeclaration is used in Statement position

3 participants