Add "decoratorsBeforeExport" option to @babel/generator (#7948)
* Add "decoratorsBeforeExport" option to @babel/generator * Docs
This commit is contained in:
parent
f699f1bbbf
commit
d45ee5e025
@ -2,8 +2,9 @@ import * as t from "@babel/types";
|
|||||||
|
|
||||||
export function ClassDeclaration(node: Object, parent: Object) {
|
export function ClassDeclaration(node: Object, parent: Object) {
|
||||||
if (
|
if (
|
||||||
!t.isExportDefaultDeclaration(parent) &&
|
!this.format.decoratorsBeforeExport ||
|
||||||
!t.isExportNamedDeclaration(parent)
|
(!t.isExportDefaultDeclaration(parent) &&
|
||||||
|
!t.isExportNamedDeclaration(parent))
|
||||||
) {
|
) {
|
||||||
this.printJoin(node.decorators, node);
|
this.printJoin(node.decorators, node);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -57,7 +57,10 @@ export function ExportAllDeclaration(node: Object) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function ExportNamedDeclaration(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);
|
this.printJoin(node.declaration.decorators, node);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -67,7 +70,10 @@ export function ExportNamedDeclaration(node: Object) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function ExportDefaultDeclaration(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);
|
this.printJoin(node.declaration.decorators, node);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -52,6 +52,10 @@ function normalizeOptions(code, opts): Format {
|
|||||||
style: " ",
|
style: " ",
|
||||||
base: 0,
|
base: 0,
|
||||||
},
|
},
|
||||||
|
decoratorsBeforeExport:
|
||||||
|
opts.decoratorsBeforeExport === undefined
|
||||||
|
? true
|
||||||
|
: opts.decoratorsBeforeExport,
|
||||||
};
|
};
|
||||||
|
|
||||||
if (format.minified) {
|
if (format.minified) {
|
||||||
|
|||||||
@ -25,6 +25,7 @@ export type Format = {
|
|||||||
style: string,
|
style: string,
|
||||||
base: number,
|
base: number,
|
||||||
},
|
},
|
||||||
|
decoratorsBeforeExport: boolean,
|
||||||
};
|
};
|
||||||
|
|
||||||
export default class Printer {
|
export default class Printer {
|
||||||
|
|||||||
3
packages/babel-generator/test/fixtures/decoratorsBeforeExport/false-to-false/input.js
vendored
Normal file
3
packages/babel-generator/test/fixtures/decoratorsBeforeExport/false-to-false/input.js
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
export default @dec class Foo {}
|
||||||
|
|
||||||
|
export @dec class Bar {}
|
||||||
6
packages/babel-generator/test/fixtures/decoratorsBeforeExport/false-to-false/options.json
vendored
Normal file
6
packages/babel-generator/test/fixtures/decoratorsBeforeExport/false-to-false/options.json
vendored
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"plugins": [
|
||||||
|
["decorators", { "decoratorsBeforeExport": false }]
|
||||||
|
],
|
||||||
|
"decoratorsBeforeExport": false
|
||||||
|
}
|
||||||
4
packages/babel-generator/test/fixtures/decoratorsBeforeExport/false-to-false/output.js
vendored
Normal file
4
packages/babel-generator/test/fixtures/decoratorsBeforeExport/false-to-false/output.js
vendored
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
export default @dec
|
||||||
|
class Foo {}
|
||||||
|
export @dec
|
||||||
|
class Bar {}
|
||||||
3
packages/babel-generator/test/fixtures/decoratorsBeforeExport/false-to-true/input.js
vendored
Normal file
3
packages/babel-generator/test/fixtures/decoratorsBeforeExport/false-to-true/input.js
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
export default @dec class Foo {}
|
||||||
|
|
||||||
|
export @dec class Bar {}
|
||||||
6
packages/babel-generator/test/fixtures/decoratorsBeforeExport/false-to-true/options.json
vendored
Normal file
6
packages/babel-generator/test/fixtures/decoratorsBeforeExport/false-to-true/options.json
vendored
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"plugins": [
|
||||||
|
["decorators", { "decoratorsBeforeExport": false }]
|
||||||
|
],
|
||||||
|
"decoratorsBeforeExport": true
|
||||||
|
}
|
||||||
4
packages/babel-generator/test/fixtures/decoratorsBeforeExport/false-to-true/output.js
vendored
Normal file
4
packages/babel-generator/test/fixtures/decoratorsBeforeExport/false-to-true/output.js
vendored
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
@dec
|
||||||
|
export default class Foo {}
|
||||||
|
@dec
|
||||||
|
export class Bar {}
|
||||||
5
packages/babel-generator/test/fixtures/decoratorsBeforeExport/true-to-false/input.js
vendored
Normal file
5
packages/babel-generator/test/fixtures/decoratorsBeforeExport/true-to-false/input.js
vendored
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
@dec
|
||||||
|
export default class Foo {}
|
||||||
|
|
||||||
|
@dec
|
||||||
|
export class Bar {}
|
||||||
6
packages/babel-generator/test/fixtures/decoratorsBeforeExport/true-to-false/options.json
vendored
Normal file
6
packages/babel-generator/test/fixtures/decoratorsBeforeExport/true-to-false/options.json
vendored
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"plugins": [
|
||||||
|
["decorators", { "decoratorsBeforeExport": true }]
|
||||||
|
],
|
||||||
|
"decoratorsBeforeExport": false
|
||||||
|
}
|
||||||
4
packages/babel-generator/test/fixtures/decoratorsBeforeExport/true-to-false/output.js
vendored
Normal file
4
packages/babel-generator/test/fixtures/decoratorsBeforeExport/true-to-false/output.js
vendored
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
export default @dec
|
||||||
|
class Foo {}
|
||||||
|
export @dec
|
||||||
|
class Bar {}
|
||||||
5
packages/babel-generator/test/fixtures/decoratorsBeforeExport/true-to-true/input.js
vendored
Normal file
5
packages/babel-generator/test/fixtures/decoratorsBeforeExport/true-to-true/input.js
vendored
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
@dec
|
||||||
|
export default class Foo {}
|
||||||
|
|
||||||
|
@dec
|
||||||
|
export class Bar {}
|
||||||
6
packages/babel-generator/test/fixtures/decoratorsBeforeExport/true-to-true/options.json
vendored
Normal file
6
packages/babel-generator/test/fixtures/decoratorsBeforeExport/true-to-true/options.json
vendored
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"plugins": [
|
||||||
|
["decorators", { "decoratorsBeforeExport": true }]
|
||||||
|
],
|
||||||
|
"decoratorsBeforeExport": true
|
||||||
|
}
|
||||||
4
packages/babel-generator/test/fixtures/decoratorsBeforeExport/true-to-true/output.js
vendored
Normal file
4
packages/babel-generator/test/fixtures/decoratorsBeforeExport/true-to-true/output.js
vendored
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
@dec
|
||||||
|
export default class Foo {}
|
||||||
|
@dec
|
||||||
|
export class Bar {}
|
||||||
@ -78,6 +78,23 @@ require("@babel/core").transform("code", {
|
|||||||
|
|
||||||
## Options
|
## 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`
|
### `legacy`
|
||||||
|
|
||||||
`boolean`, defaults to `false`.
|
`boolean`, defaults to `false`.
|
||||||
|
|||||||
@ -6,7 +6,7 @@ import legacyVisitor from "./transformer-legacy";
|
|||||||
export default declare((api, options) => {
|
export default declare((api, options) => {
|
||||||
api.assertVersion(7);
|
api.assertVersion(7);
|
||||||
|
|
||||||
const { legacy = false } = options;
|
const { legacy = false, decoratorsBeforeExport } = options;
|
||||||
if (typeof legacy !== "boolean") {
|
if (typeof legacy !== "boolean") {
|
||||||
throw new Error("'legacy' must be a 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 {
|
return {
|
||||||
inherits: syntaxDecorators,
|
inherits: syntaxDecorators,
|
||||||
|
|
||||||
|
manipulateOptions({ generatorOpts }) {
|
||||||
|
generatorOpts.decoratorsBeforeExport = decoratorsBeforeExport;
|
||||||
|
},
|
||||||
|
|
||||||
visitor: legacy ? legacyVisitor : visitor,
|
visitor: legacy ? legacyVisitor : visitor,
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user