Skip to content

Conversation

@pranshugupta01
Copy link

@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
Author

@microsoft-github-policy-service agree

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.

__setFunctionName breaks custom static name on classes

2 participants