microsoft / microsoft/TypeScript

[Decorators] Missing error when calling `addInitializer` after that the decorator is done

Open
#57,300 3 comments 0 reactions 1 assignee View on GitHub

@rbuckton is already working on this.

Since Feb 5, 2024.

Needs Investigation
Dominant language
Go
Stars
111k
Forks
14.4k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

🔎 Search Terms

decorators addInitializer error

🕗 Version & Regression Information
  • This is the behavior in every version I tried since Stage 3 decorators where implemented
⏯ Playground Link

https://www.typescriptlang.org/play?#code/DYUwLgBAhgJjCSA7AlmZVjIF4gE4DFcB7AWwEYAuCAERAGMjcoxGBhIxMEADzAG0ARLAQo0GbHgEBdANwBYAFCKAZgFdEdNBwgx6ZABQB9KlEQBPADQRN3KrQZMWudpx5gAlBADeiiNDhIqOiYOATE5BAAvNZg3AB0woFiIXjyCgC+iirqmsjaunQATEYm5laGhaVmnj4KfomiwRJhpAb6npEAfN7p7r7WHADORKBxwEQA5voCAILU1ACi1AJ9GVkKdMBQg4MQM979AAIFhUcFZP3KRERREGRpmUoKQA

💻 Code
let addInitializerFrom1: DecoratorContext["addInitializer"];

function dec1(_: any, ctx: DecoratorContext) {
  addInitializerFrom1 = ctx.addInitializer;
}

function dec2(_: any, _2: any) {
  addInitializerFrom1(() => {})
  console.log("ADDED")
}

class A {
  @dec2
  @dec1
  foo = 1;
}
🙁 Actual behavior

The addInitializerFrom1 call should throw

🙂 Expected behavior

addInitializerFrom1 doesn't throw

Additional information about the issue

Spec: https://ci.tc39.es/preview/tc39/ecma262/sha/59003ed603d09485b957a0f579b46697f49a96a6/#sec-applydecoratorstoelementdefinition

After that dec1 is called, decorationState.[[Finished]] for the decorationState relative to dec1 is set to false and thus calling addInitializer should throw.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.