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

Reuse original decorator only if not Type(result) is Object for methods, etc. #68

Open
dead-claudia opened this issue Mar 26, 2016 · 0 comments

Comments

@dead-claudia
Copy link

The current specification relies on truthiness to determine whether to keep the decorator, but I think it might be a little cleaner to spec out checking the result of calling the decorator, instead of relying on truthiness. TypeScript already follows this logic itself, where it requires TypedPropertyDescriptor<T> | void for the return type. This change would also put it more in line with the rest of the spec.

A few minimal examples to demonstrate:

(Note: %variable refers to an internal variable here.)

  • Check typeof result === "function" for class decorators

    // Original
    @decorator
    class C {}
    
    // Current
    class C {}
    C = decorator(C) || C
    
    // Proposed
    class C {}
    let %temp = decorator(C)
    if (typeof %temp === "function") C = %temp
  • Check typeof result === "object" for method and accessor decorators.

    class C {
      @decorator
      method() {}
    }
    
    // Current
    class C {
      method() {}
    }
    let %desc = Object.getOwnPropertyDescriptor(C.prototype, "method")
    %desc = decorator(C.prototype, "method", %desc) || %desc
    Object.defineProperty(C.prototype, "method", %desc)
    
    // Proposed
    class C {
      method() {}
    }
    let %desc = Object.getOwnPropertyDescriptor(C.prototype, "method")
    let %result = decorator(C.prototype, "method", %desc)
    %desc = typeof %result === "object" ? %result : %desc
    Object.defineProperty(C.prototype, "method", %desc)
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

1 participant