tc39/proposal-decorators

How to exclude methods from class decorator

Closed this issue · 2 comments

With the new decorators proposal, if I want to create a class decorator which by default affects all the methods in the class, how do I make it possible for another (or the same) decorator to be applied to specific methods in such a way that those methods will be excluded from the class decorator?

For example:

@myDecorator
class MyClass {
    // This method will be affected by `@myDecorator`:
    foo() { /* ... */ }
    
    // This method should be exempted:
    @myDecorator({ skip: true })
    bar() { /* ... */ }
}

The problem I'm facing is that I'm not sure how to make @myDecorator at the class level aware of the decorators that are being invoked at the method level. Any suggestions?

It is very simple.

You can use the metadata for exclude the method and check this metadata in the class decorator.

Ah right, so I guess I need to set up a side channel to track which methods are exempted, and look those up when the class decorator is invoked. Think I got it, thanks!