Skip to content

Use declared class name for ClassDecoratorContext.name - #64844

Open
Adrián Iglesias Álvarez (adrianiglesiass) wants to merge 1 commit into
microsoft:mainfrom
adrianiglesiass:fix-decorator-context-name
Open

Adrián Iglesias Álvarez (adrianiglesiass) wants to merge 1 commit into
microsoft:mainfrom
adrianiglesiass:fix-decorator-context-name

Conversation

@adrianiglesiass

Copy link
Copy Markdown

Fixes #62870

Problem

For a decorated class with a declared name, the class decorator context was emitted as { kind: "class", name: _classThis.name, ... }. Reading _classThis.name invokes a user-defined static name getter or method, so context.name could be something other than the class name:

const dec = (_: unknown, ctx: ClassDecoratorContext) => console.log(ctx.name);

@dec
class A {
    static get name() { return 2434; }
}
// logs 2434, expected "A"

Implementation

In esdecorator.go, when the class has a declared (non-generated) name, the context name is now emitted as a string literal (name: "A"). Anonymous classes, including export default class {}, which get a generated identifier, keep _classThis.name. For those, __setFunctionName has already defined name as an own property, so it isn't shadowed by a static name getter.

Tests

  • New compiler test esDecoratorClassContextNameShadowedByStaticMember.ts covering a static name getter, a static name() method, and an anonymous class expression.
  • Existing baselines updated: every changed .js line is the kind: "class", name: ... context object (_classThis.name -> "<ClassName>"); the four sourceMap baselines change only because of the shifted columns.
  • Ran the full "Before submitting a pull request" list from CONTRIBUTING.md (generate, build, test, test:all, lint, format, check:format, package builds/tests, mod tidy -diff, go work sync); all passed with no extra diff.

AI disclosure

This PR was authored with Claude Code (Anthropic). I've reviewed and understand the change and will handle the review feedback myself.

🤖 Generated with Claude Code

Reading _classThis.name invokes a user-defined static `name` getter or
method, so ctx.name could be something other than the class name.
Emit the declared name as a string literal and keep _classThis.name only
for anonymous classes, whose name is set by __setFunctionName.

Fixes microsoft#62870
Copilot AI balanced review requested due to automatic review settings October 16, 2026 15:24
@github-project-automation github-project-automation Bot moved this to Not started in PR Backlog Oct 16, 2026
@typescript-automation typescript-automation Bot added For Backlog Bug PRs that fix a backlog bug labels Oct 16, 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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@adrianiglesiass

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

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

For Backlog Bug PRs that fix a backlog bug

Projects

Status: Not started

Development

Successfully merging this pull request may close these issues.

ES ClassDecoratorContext.name has incorrect value with static name

2 participants