tc39 / proposal-decorators-previous

Decorators for ECMAScript
183 stars 16 forks source link

Could mis-placed decorators be made to throw? #42

Closed ljharb closed 6 years ago

ljharb commented 7 years ago

Let's say I return an object from a decorator that has an initializer property, but the placement is not "field" - I'd like it to throw.

Similarly, if I return "private": true on a public property, I'd like it to throw.

In other words, the overarching desire is for a decorator intended for one placement, to throw, whenever possible, when it's used in the "wrong" placement.

ljharb commented 7 years ago

It would be even nicer if expected fields were type-checked; iow, if private is not undefined or a boolean, throw; if initializer is not undefined or a function, throw; etc.

littledan commented 7 years ago

Thanks for filing this bug; I will add something to make misplaced initializers throw. I'm not sure what you're referring to with "private"--that's not used right now. It's just part of the key.

ljharb commented 7 years ago

I'm definitely not aware of the entire API surface of what can be returned; that's part of why I want it to throw when I do something wrong :-)

littledan commented 7 years ago

One point about evolution in the future here: In theory, it could cause compatibility issues if we add more fields, to check for those and throw on them. We might just be able to do this the first time. Or, we might risk that compat possibility anyway.

ljharb commented 7 years ago

Changing things from "throws" to "does something" has generally been considered a web-compatible change; but you're right that someone could potentially rely on the exception.

littledan commented 7 years ago

I was saying, for example, we could make a field do something for class decorators, and then we'd want to make it throw an error if you use it on field decorators, but throwing there could cause a compatibility issue.

littledan commented 6 years ago

See https://github.com/tc39/proposal-decorators/pull/30 for that strictness.

littledan commented 6 years ago

Fixed by https://github.com/tc39/proposal-decorators/pull/30 .