Skip to content

Decorators broken with private fields, generated code has syntax errorΒ #48515

Open

Description

Bug Report

πŸ”Ž Search Terms

decorator private field syntax error

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

function decorator(cb: () => any): any {
  return () => console.log(cb())
}

class Foo {
  static #bar = 123
  @decorator(() => Foo.#bar) foo() {}
}

new Foo

πŸ™ Actual behavior

I expect this code to either be a compile error or to compile successfully and print 123 when run.

πŸ™‚ Expected behavior

The code compiles without any errors but the generated code contains a syntax error:

"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var _a, _Foo_bar;
function decorator(cb) {
    return () => console.log(cb());
}
class Foo {
    foo() { }
}
_a = Foo;
_Foo_bar = { value: 123 };
__decorate([
    decorator(() => Foo.)
], Foo.prototype, "foo", null);
new Foo;

Context: I discovered this issue while investigating how TypeScript handles various decorator edge cases, which I'm doing because I'm trying to replicate TypeScript's behavior into esbuild's TypeScript transpiler for this issue: evanw/esbuild#2147.

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

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions