Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decorator context.kind "object-getter" or "getter" #2

Open
Jack-Works opened this issue Jan 26, 2024 · 2 comments
Open

Decorator context.kind "object-getter" or "getter" #2

Jack-Works opened this issue Jan 26, 2024 · 2 comments

Comments

@Jack-Works
Copy link
Member

type ObjectLiteralGetterDecoratorContext = {
  kind: "object-getter"; // or just "getter"?
  ... // other properties from ObjectLiteralMethodDecoratorContext
}

type ObjectLiteralSetterDecoratorContext = {
  kind: "object-setter"; // or just "setter"?
  ... // other properties from ObjectLiteralMethodDecoratorContext
}

I can think of a new option: add a new property in the context.

  kind: 'field'
  container: 'class' | 'object' // | 'enum' | 'protocol'

this allows us to extend in the future, for example, if we have enum or protocol as the syntatic container.

@Jamesernator
Copy link

Yes this is a lot simpler as it indicates what kind of thing metadata will installed on.

I would suggest having this even for root containers, so that the type in this proposal would be:

container: "class" | "object" | "function"

@rbuckton
Copy link
Collaborator

This was something I also explored in tc39/proposal-decorators#466 and it's something I'd like to revisit.

Ideally, container would be more flexible than just a string:

type DecoratorContext = {
  ...
  container?: {
    kind: "class" | "struct" | "object" | "enum" | "protocol";
    name: string | undefined;
    addInitializer(initializer: () => void): void; // adds a static initializer
  };
};

I'd thought about having context.metadata only be available if the decorated element itself could have metadata (i.e., class, function), while you would use context.container.metadata if you were a child element (i.e., methods, getters, setters, fields, etc.). That would have allowed methods have access to both own metadata and container metadata, so a function decorator could be used on a method more readily.

However, I don't think we should go about changing context.metadata at stage 3, which is part of why the explainer includes a suggestion to use the name context.functionMetadata.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants