evanw / evanw/esbuild

Reference to self within decorated class refers to undecorated class

Open
#3,968 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
40.1k
Forks
1.3k
PR merge metrics
No merged PRs in 30d

Description

TL;DR: Should a decorated class referencing itself in a method refer to the decorated class or undecorated? Currently it does the latter - which feels broken, but admittedly that may just be due to quirky code patterns.

Example code:

```ts
const bar = ...

@bar
class Foo {
baz() {
console.log(Foo);
}
}
```

To be honest, despite having done my best to read the [proposal](https://github.com/tc39/proposal-decorators?tab=readme-ov-file) and [spec](https://arai-a.github.io/ecma262-compare/?pr=2417), I cannot be certain whether esbuild is behaving incorrectly in this matter. However, I could not be certain that esbuild is deliberate in its current behavior since I could not spot a relevant case in the tests (https://github.com/evanw/decorator-tests/blob/main/decorator-tests.ts). The only case(s) that seemed similar also seemed distinct enough to not quite be relevant (i.e. the complexities involved with things like `accessor` might mean it's irrelevant to my simpler situation).

[esbuild.github.io/try link](https://esbuild.github.io/try/#YgAwLjI0LjAALS1idW5kbGUgLS10YXJnZXQ9ZXMyMDIyAGUAZW50cnkuanMAY29uc3QgYmFyID0geCA9PiBjbGFzcyBCYXIgewogIGJheigpIHsKICAgIG5ldyB4KCkuYmF6KCkKICB9Cn0KCkBiYXIKY2xhc3MgRm9vIHsKICBiYXooKSB7CiAgICBjb25zb2xlLmxvZyhGb28pOwogIH0KfQoKY29uc29sZS5sb2coRm9vKQpuZXcgRm9vKCkuYmF6KCk7Cg)

Running the built code via node (20.12.1 in case it matters) results in

```bash
[class Bar] { [Symbol(Symbol.metadata)]: [Object: null prototype] {} }
[class Foo]
```

Notably, if I modify the built code and change one line near the bottom to `var _Foo = class _FooInner {` (so as to not reuse the `_Foo` identifier for the class expression itself), then I get the result I had expected:

```bash
[class Bar] { [Symbol(Symbol.metadata)]: [Object: null prototype] {} }
[class Bar] { [Symbol(Symbol.metadata)]: [Object: null prototype] {} }
```

Contributor guide

No contributing guide indexed for this repository

Research direction

Reproduce the behavior using the provided esbuild.github.io/try link and compare it with the decorator-tests.ts cases and linked decorators proposal/spec. Determine whether self-references in decorated classes should resolve to the decorated class, then add a focused regression test and confirm the generated code and runtime result match the chosen semantics.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.