@babel/plugin-proposal-decorators
Advanced tools
Comparing version 7.0.0-beta.49 to 7.0.0-beta.50
@@ -37,3 +37,4 @@ "use strict"; | ||
const { | ||
legacy = false | ||
legacy = false, | ||
decoratorsBeforeExport | ||
} = options; | ||
@@ -49,4 +50,21 @@ | ||
if (decoratorsBeforeExport !== undefined) { | ||
if (legacy) { | ||
throw new Error("'decoratorsBeforeExport' can't be used with legacy decorators."); | ||
} | ||
if (typeof decoratorsBeforeExport !== "boolean") { | ||
throw new Error("'decoratorsBeforeExport' must be a boolean."); | ||
} | ||
} | ||
return { | ||
inherits: _pluginSyntaxDecorators().default, | ||
manipulateOptions({ | ||
generatorOpts | ||
}) { | ||
generatorOpts.decoratorsBeforeExport = decoratorsBeforeExport; | ||
}, | ||
visitor: legacy ? _transformerLegacy.default : _transformer.default | ||
@@ -53,0 +71,0 @@ }; |
@@ -41,8 +41,8 @@ "use strict"; | ||
const decorators = (path.isClass() ? [path].concat(path.get("body.body")) : path.get("properties")).reduce((acc, prop) => acc.concat(prop.node.decorators || []), []); | ||
const identDecorators = decorators.filter(decorator => !_core().types.isIdentifier(decorator.callee)); | ||
const identDecorators = decorators.filter(decorator => !_core().types.isIdentifier(decorator.expression)); | ||
if (identDecorators.length === 0) return; | ||
return _core().types.sequenceExpression(identDecorators.map(decorator => { | ||
const callee = decorator.callee; | ||
const id = decorator.callee = path.scope.generateDeclaredUidIdentifier("dec"); | ||
return _core().types.assignmentExpression("=", id, callee); | ||
const expression = decorator.expression; | ||
const id = decorator.expression = path.scope.generateDeclaredUidIdentifier("dec"); | ||
return _core().types.assignmentExpression("=", id, expression); | ||
}).concat([path.node])); | ||
@@ -56,3 +56,3 @@ } | ||
const name = classPath.scope.generateDeclaredUidIdentifier("class"); | ||
return decorators.map(dec => dec.callee).reverse().reduce(function (acc, decorator) { | ||
return decorators.map(dec => dec.expression).reverse().reduce(function (acc, decorator) { | ||
return buildClassDecorator({ | ||
@@ -107,5 +107,5 @@ CLASS_REF: _core().types.cloneNode(name), | ||
WARNING_CALLS.add(node.value); | ||
acc = acc.concat([_core().types.assignmentExpression("=", descriptor, _core().types.callExpression(state.addHelper("applyDecoratedDescriptor"), [_core().types.cloneNode(target), _core().types.cloneNode(property), _core().types.arrayExpression(decorators.map(dec => _core().types.cloneNode(dec.callee))), _core().types.objectExpression([_core().types.objectProperty(_core().types.identifier("enumerable"), _core().types.booleanLiteral(true)), _core().types.objectProperty(_core().types.identifier("initializer"), initializer)])]))]); | ||
acc = acc.concat([_core().types.assignmentExpression("=", descriptor, _core().types.callExpression(state.addHelper("applyDecoratedDescriptor"), [_core().types.cloneNode(target), _core().types.cloneNode(property), _core().types.arrayExpression(decorators.map(dec => _core().types.cloneNode(dec.expression))), _core().types.objectExpression([_core().types.objectProperty(_core().types.identifier("enumerable"), _core().types.booleanLiteral(true)), _core().types.objectProperty(_core().types.identifier("initializer"), initializer)])]))]); | ||
} else { | ||
acc = acc.concat(_core().types.callExpression(state.addHelper("applyDecoratedDescriptor"), [_core().types.cloneNode(target), _core().types.cloneNode(property), _core().types.arrayExpression(decorators.map(dec => _core().types.cloneNode(dec.callee))), _core().types.isObjectProperty(node) || _core().types.isClassProperty(node, { | ||
acc = acc.concat(_core().types.callExpression(state.addHelper("applyDecoratedDescriptor"), [_core().types.cloneNode(target), _core().types.cloneNode(property), _core().types.arrayExpression(decorators.map(dec => _core().types.cloneNode(dec.expression))), _core().types.isObjectProperty(node) || _core().types.isClassProperty(node, { | ||
static: true | ||
@@ -112,0 +112,0 @@ }) ? buildGetObjectInitializer({ |
{ | ||
"name": "@babel/plugin-proposal-decorators", | ||
"version": "7.0.0-beta.49", | ||
"version": "7.0.0-beta.50", | ||
"author": "Logan Smyth <loganfsmyth@gmail.com>", | ||
@@ -15,4 +15,4 @@ "license": "MIT", | ||
"dependencies": { | ||
"@babel/helper-plugin-utils": "7.0.0-beta.49", | ||
"@babel/plugin-syntax-decorators": "7.0.0-beta.49" | ||
"@babel/helper-plugin-utils": "7.0.0-beta.50", | ||
"@babel/plugin-syntax-decorators": "7.0.0-beta.50" | ||
}, | ||
@@ -23,5 +23,5 @@ "peerDependencies": { | ||
"devDependencies": { | ||
"@babel/core": "7.0.0-beta.49", | ||
"@babel/helper-plugin-test-runner": "7.0.0-beta.49" | ||
"@babel/core": "7.0.0-beta.50", | ||
"@babel/helper-plugin-test-runner": "7.0.0-beta.50" | ||
} | ||
} |
119
README.md
@@ -5,123 +5,16 @@ # @babel/plugin-proposal-decorators | ||
## Example | ||
See our website [@babel/plugin-proposal-decorators](https://new.babeljs.io/docs/en/next/babel-plugin-proposal-decorators.html) for more information. | ||
(examples are from proposal) | ||
## Install | ||
### Simple class decorator | ||
Using npm: | ||
```js | ||
@annotation | ||
class MyClass { } | ||
function annotation(target) { | ||
target.annotated = true; | ||
} | ||
``` | ||
### Class decorator | ||
```js | ||
@isTestable(true) | ||
class MyClass { } | ||
function isTestable(value) { | ||
return function decorator(target) { | ||
target.isTestable = value; | ||
} | ||
} | ||
``` | ||
### Class function decorator | ||
```js | ||
class C { | ||
@enumerable(false) | ||
method() { } | ||
} | ||
function enumerable(value) { | ||
return function (target, key, descriptor) { | ||
descriptor.enumerable = value; | ||
return descriptor; | ||
} | ||
} | ||
``` | ||
## Installation | ||
```sh | ||
npm install --save-dev @babel/plugin-proposal-decorators | ||
npm install --save @babel/plugin-proposal-decorators | ||
``` | ||
## Usage | ||
or using yarn: | ||
Add the following line to your .babelrc file: | ||
```json | ||
{ | ||
"plugins": ["@babel/plugin-proposal-decorators"] | ||
} | ||
``` | ||
### Via CLI | ||
```sh | ||
babel --plugins @babel/plugin-proposal-decorators script.js | ||
yarn add --save @babel/plugin-proposal-decorators | ||
``` | ||
### Via Node API | ||
```javascript | ||
require("@babel/core").transform("code", { | ||
plugins: ["@babel/plugin-proposal-decorators"] | ||
}); | ||
``` | ||
## Options | ||
### `legacy` | ||
`boolean`, defaults to `false`. | ||
Use the legacy (stage 1) decorators syntax and behavior. | ||
#### NOTE: Compatibility with `@babel/plugin-proposal-class-properties` | ||
If you are including your plugins manually and using `@babel/plugin-proposal-class-properties`, make sure that `@babel/plugin-proposal-decorators` comes *before* `@babel/plugin-proposal-class-properties`. | ||
When using the `legacy: true` mode, `@babel/plugin-proposal-class-properties` must be used in `loose` mode to support the `@babel/plugin-proposal-decorators`. | ||
Wrong: | ||
```json | ||
{ | ||
"plugins": [ | ||
"@babel/plugin-proposal-class-properties", | ||
"@babel/plugin-proposal-decorators" | ||
] | ||
} | ||
``` | ||
Right: | ||
```json | ||
{ | ||
"plugins": [ | ||
"@babel/plugin-proposal-decorators", | ||
"@babel/plugin-proposal-class-properties" | ||
] | ||
} | ||
``` | ||
```json | ||
{ | ||
"plugins": [ | ||
["@babel/plugin-proposal-decorators", { "legacy": true }], | ||
["@babel/plugin-proposal-class-properties", { "loose" : true }] | ||
] | ||
} | ||
``` | ||
## References | ||
* [Proposal: JavaScript Decorators](https://github.com/wycats/javascript-decorators/blob/master/README.md) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
206
10363
5
20
+ Added@babel/helper-plugin-utils@7.0.0-beta.50(transitive)
+ Added@babel/plugin-syntax-decorators@7.0.0-beta.50(transitive)
- Removed@babel/helper-plugin-utils@7.0.0-beta.49(transitive)
- Removed@babel/plugin-syntax-decorators@7.0.0-beta.49(transitive)