Skip to content

Fix __setFunctionName overriding custom static name property#62902

Closed
pranshugupta01 wants to merge 5 commits intomicrosoft:mainfrom
pranshugupta01:fix/setFunctionName-static-name
Closed

Fix __setFunctionName overriding custom static name property#62902
pranshugupta01 wants to merge 5 commits intomicrosoft:mainfrom
pranshugupta01:fix/setFunctionName-static-name

Conversation

@pranshugupta01
Copy link
Copy Markdown

@pranshugupta01 pranshugupta01 commented Dec 15, 2025

This PR fixes #62854 where the __setFunctionName helper unconditionally overwrites custom static name properties (getters, methods, accessors) on decorated classes.

The Problem

const dec = (c: any) => c;

@dec class MyClass {
    static get name() { return 2434; }
}

console.log(MyClass.name);
// Expected: 2434
// Actual: "MyClass" ❌ — custom getter was overwritten!

The error occurred because __setFunctionName used Object.defineProperty to unconditionally set the name property, ignoring any custom static name getter, method, or accessor defined by the user.

The Fix:
Modified the __setFunctionName helper in src/compiler/factory/emitHelpers.ts to check the existing property descriptor before overwriting:

var __setFunctionName = function (f, name, prefix) {
    if (typeof name === "symbol") name = name.description ? "[".concat(name.description, "]") : "";
    var d = Object.getOwnPropertyDescriptor(f, "name");
    if (d && d.writable !== false) return f;  // Don't override custom definitions
    return Object.defineProperty(f, "name", { configurable: true, value: prefix ? "".concat(prefix, " ", name) : name });
};

Testing

  • Added test case esDecorators-classDeclaration-staticName.ts covering:
  • Decorated class with static get name()
  • Decorated class with static name() method
  • Decorated anonymous class with custom name getter
  • Decorated class without custom name (automatic name still works)
  • Decorated class with static name field

Baseline Changes

New baselines:

  • esDecorators-classDeclaration-staticName(target=es2022).js
  • esDecorators-classDeclaration-staticName(target=esnext).js
  • Updated baselines (helper text changed with 2 new lines):
  • esDecorators-classDeclaration-sourceMap (es2015, es2022)
  • esDecoratorsClassFieldsCrash.js
  • esDecoratorsMetadata1-4 (es2015)

All 99,167 tests pass.

@github-project-automation github-project-automation Bot moved this to Not started in PR Backlog Dec 15, 2025
@typescript-bot typescript-bot added the For Backlog Bug PRs that fix a backlog bug label Dec 15, 2025
@pranshugupta01
Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

@pranshugupta01
Copy link
Copy Markdown
Author

Hi @RyanCavanaugh , please review this PR , thanks

@typescript-bot
Copy link
Copy Markdown
Collaborator

With 6.0 out as the final release vehicle for this codebase, we're closing all PRs that don't fit the merge criteria for post-6.0 patches. If you think this was a mistake and this PR fits the post-6.0 patch criteria, please post to the 6.0 iteration issue with details (specifically, which PR and which patch criteria it satisfies).

Next steps for PRs:

  • For crash bugfixes or language service improvements, PRs are currently accepted at the typescript-go repo
  • Changes to type system behavior should wait until after 7.0, at which point mainline TypeScript development will resume in this repository with the Go codebase
  • Library file updates (lib.d.ts etc) continue to live in this repo or the DOM Generator repo as appropriate

@github-project-automation github-project-automation Bot moved this from Not started to Done in PR Backlog Mar 24, 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.

__setFunctionName breaks custom static name on classes

2 participants