Skip to content

Default return value for decorators #77

@nicolo-ribaudo

Description

@nicolo-ribaudo

While playing with decorators I found a little usability problem. I initially wrote this decorator:

function add(value) {
  return function decorator(element) {
    const { initializer } = element;
    element.initializer = function() {
      return initializer.call(this) + value;
    };
  };
}

class Foo {
  @add(2) num = 3;
}

When run, it throws Cannot read property "kind" of undefined.. It was my fault (I forgot to write return element; at the end of the decorator function), but it has been quite hard to spot.

I propose two ways of improving this:

  1. If the decorator function returns undefined, throw an error. (After step 3.d of https://tc39.github.io/proposal-decorators/#sec-decorate-element)
  2. If the decorator function returns undefined, default to elementObject. This make it possible to just mutate the descriptor without returning it.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions