Add "decoratorsBeforeExport" option to @babel/generator (#7948)

* Add "decoratorsBeforeExport" option to @babel/generator

* Docs
This commit is contained in:
Nicolò Ribaudo 2018-05-27 08:50:26 +02:00 committed by GitHub
parent f699f1bbbf
commit d45ee5e025
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 105 additions and 5 deletions

View File

@ -2,8 +2,9 @@ import * as t from "@babel/types";
export function ClassDeclaration(node: Object, parent: Object) {
if (
!t.isExportDefaultDeclaration(parent) &&
!t.isExportNamedDeclaration(parent)
!this.format.decoratorsBeforeExport ||
(!t.isExportDefaultDeclaration(parent) &&
!t.isExportNamedDeclaration(parent))
) {
this.printJoin(node.decorators, node);
}

View File

@ -57,7 +57,10 @@ export function ExportAllDeclaration(node: Object) {
}
export function ExportNamedDeclaration(node: Object) {
if (t.isClassDeclaration(node.declaration)) {
if (
this.format.decoratorsBeforeExport &&
t.isClassDeclaration(node.declaration)
) {
this.printJoin(node.declaration.decorators, node);
}
@ -67,7 +70,10 @@ export function ExportNamedDeclaration(node: Object) {
}
export function ExportDefaultDeclaration(node: Object) {
if (t.isClassDeclaration(node.declaration)) {
if (
this.format.decoratorsBeforeExport &&
t.isClassDeclaration(node.declaration)
) {
this.printJoin(node.declaration.decorators, node);
}

View File

@ -52,6 +52,10 @@ function normalizeOptions(code, opts): Format {
style: " ",
base: 0,
},
decoratorsBeforeExport:
opts.decoratorsBeforeExport === undefined
? true
: opts.decoratorsBeforeExport,
};
if (format.minified) {

View File

@ -25,6 +25,7 @@ export type Format = {
style: string,
base: number,
},
decoratorsBeforeExport: boolean,
};
export default class Printer {

View File

@ -0,0 +1,3 @@
export default @dec class Foo {}
export @dec class Bar {}

View File

@ -0,0 +1,6 @@
{
"plugins": [
["decorators", { "decoratorsBeforeExport": false }]
],
"decoratorsBeforeExport": false
}

View File

@ -0,0 +1,4 @@
export default @dec
class Foo {}
export @dec
class Bar {}

View File

@ -0,0 +1,3 @@
export default @dec class Foo {}
export @dec class Bar {}

View File

@ -0,0 +1,6 @@
{
"plugins": [
["decorators", { "decoratorsBeforeExport": false }]
],
"decoratorsBeforeExport": true
}

View File

@ -0,0 +1,4 @@
@dec
export default class Foo {}
@dec
export class Bar {}

View File

@ -0,0 +1,5 @@
@dec
export default class Foo {}
@dec
export class Bar {}

View File

@ -0,0 +1,6 @@
{
"plugins": [
["decorators", { "decoratorsBeforeExport": true }]
],
"decoratorsBeforeExport": false
}

View File

@ -0,0 +1,4 @@
export default @dec
class Foo {}
export @dec
class Bar {}

View File

@ -0,0 +1,5 @@
@dec
export default class Foo {}
@dec
export class Bar {}

View File

@ -0,0 +1,6 @@
{
"plugins": [
["decorators", { "decoratorsBeforeExport": true }]
],
"decoratorsBeforeExport": true
}

View File

@ -0,0 +1,4 @@
@dec
export default class Foo {}
@dec
export class Bar {}

View File

@ -78,6 +78,23 @@ require("@babel/core").transform("code", {
## Options
### `decoratorsBeforeExport`
`boolean`, defaults to `true`.
```js
// decoratorsBeforeExport: true
@decorator
export class Foo {}
// decoratorsBeforeExport: false
export @decorator class Bar {}
```
This option was added to help tc39 collect feedback from the community by allowing experimentation with both possible syntaxes.
For more information, check out: [tc39/proposal-decorators#69](https://github.com/tc39/proposal-decorators/issues/69).
### `legacy`
`boolean`, defaults to `false`.

View File

@ -6,7 +6,7 @@ import legacyVisitor from "./transformer-legacy";
export default declare((api, options) => {
api.assertVersion(7);
const { legacy = false } = options;
const { legacy = false, decoratorsBeforeExport } = options;
if (typeof legacy !== "boolean") {
throw new Error("'legacy' must be a boolean.");
}
@ -19,9 +19,24 @@ export default declare((api, options) => {
);
}
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: syntaxDecorators,
manipulateOptions({ generatorOpts }) {
generatorOpts.decoratorsBeforeExport = decoratorsBeforeExport;
},
visitor: legacy ? legacyVisitor : visitor,
};
});