Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 42 additions & 12 deletions docs/plugin-proposal-decorators.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,26 @@ sidebar_label: proposal-decorators

## Example

(examples are from proposal)

### Simple class decorator

```js
@annotation
class MyClass { }

function annotation(target) {
target.annotated = true;
function annotation(classDescriptor) {
return {
...classDescriptor,
elements: [
...classDescriptor.elements,
{
kind: 'field',
key: 'annotated',
placement: 'static',
descriptor: {},
initializer: () => true
}
]
};
}
```

Expand All @@ -26,9 +36,21 @@ function annotation(target) {
class MyClass { }

function isTestable(value) {
return function decorator(target) {
target.isTestable = value;
}
return function(classDescriptor) {
return {
...classDescriptor,
elements: [
...classDescriptor.elements,
{
kind: 'field',
key: 'isTestable',
placement: 'static',
descriptor: {},
initializer: () => value
}
]
};
};
}
```

Expand All @@ -41,13 +63,20 @@ class C {
}

function enumerable(value) {
return function (target, key, descriptor) {
descriptor.enumerable = value;
return descriptor;
}
return function(elementDescriptor) {
return {
...elementDescriptor,
descriptor: {
...elementDescriptor.descriptor,
enumerable: value
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All this function can be simplified to

return function(elementDescriptor) {
  elementDescriptor.descriptor.enumerable = value;
};

@mountainmoon mountainmoon Dec 13, 2018

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just noticed the examples of the latest proposal were wrote in immutable style. But it looks like not necessary.
So the others can also be simplified to

function annotation(classDescriptor) {
  classDescriptor.elements.push({
    kind: 'field',
    key: 'annotated',
    placement: 'static',
    descriptor: {},
    initializer: () => true,
  });
}

function isTestable(value) {
  return function(classDescriptor) {
    classDescriptor.elements.push({
      kind: 'field',
      key: 'isTestable',
      placement: 'static',
      descriptor: {},
      initializer: () => value,
    });
  };
}

It works in Babel 7.1.0. However, the proposal said a decorator function returns a decorator. So should I add a return ?

return function(elementDescriptor) {
  elementDescriptor.descriptor.enumerable = value;
  return elementDescriptor
};

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If a decorator returns nothing, it returns the original descriptor (probably that introduction needs to be updated, in you want to open a PR).

I'd like to have examples in both styles, to show different ways of using decorators.

};
};
}
```

[For further examples.][proposal]

## Installation

```sh
Expand Down Expand Up @@ -142,5 +171,6 @@ Right:

## References

* [Proposal: JavaScript Decorators](https://github.com/wycats/javascript-decorators/blob/master/README.md)
* [Proposal: JavaScript Decorators][proposal]

[proposal]: https://github.com/tc39/proposal-decorators/blob/master/README.md