microsoft / microsoft/TypeScript
`this` is incorrect for static fields and static accessors within decorators
@rbuckton がすでに取り組んでいます。
2023年11月22日 から。
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.3k
- 平均マージ
- 1日 19時間
- マージ済み PR(30日)
- 117
説明
🔎 Search Terms
javascript ecmascript standard decorator this static field accessor
🕗 Version & Regression Information
- This is an error in every version I tried (from 5.0 upward)
⏯ Playground Link
💻 Code
The following code incorrectly crashes when it's transformed by TypeScript and then run:
const dec: Function = () => { }
const ctx = { dec }
function wrapper(this: typeof ctx) {
@(this.dec) class Foo {
@(this.dec) method() { }
@(this.dec) static method() { }
@(this.dec) field: undefined
@(this.dec) static field: undefined
@(this.dec) get getter(): undefined { return }
@(this.dec) static get getter(): undefined { return }
@(this.dec) set setter(x: undefined) { }
@(this.dec) static set setter(x: undefined) { }
@(this.dec) accessor accessor: undefined
@(this.dec) static accessor accessor: undefined
}
}
wrapper.call(ctx)
console.log('success')
🙁 Actual behavior
The value of this in decorators of static fields and static accessors is inconsistent with the value of this in all other kinds of decorators (run this code vs. this code).
🙂 Expected behavior
I expected the value of this to be the same for all kinds of decorators. The value of this is consistent when this code is compiled with Babel.
Additional information about the issue
For context, I'm starting to look at implementing JavaScript decorators in esbuild. I know this feature hasn't been standardized yet and is still being iterated on, but many of my users are asking for esbuild to implement it. My first step was to write a lot of tests: https://github.com/evanw/decorator-tests. That led to me discovering this bug in TypeScript.
Other bugs I noticed:
-
TypeScript thinks the
@in() => @dec class {}is a syntax error (demo here). My reading of the specification is that this should be valid asPrimaryExpressioncontainsClassExpressionwhich starts withDecoratorList opt. Babel accepts this code. -
TypeScript generates code containing syntax errors if
awaitis used in a decorator. This is allowed by the specification as far as I can tell. Here is an example: (link). Babel printstruefor this code while TypeScript emits invalid code. One simple hack to fix this could be for TypeScript to useawait (async () => { await ... })()instead of(() => { await ... })()in this case. -
Referencing the class name within a decorator seems to return
undefinedinstead of throwing aReferenceError. Both TypeScript and Babel do this so I'm not sure if this is a bug with both, or if it's not a bug and the specification is outdated, or if I'm misreading the specification. Assuming this is a bug with both, it may be somewhat important to fix as people will surely try to do that (e.g. like this) and aReferenceErrorseems more clear than a value ofundefined.
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
評価
この issue はまだ評価されていません。