Skip to content

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

@dead-claudia

Description

@dead-claudia

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions