Skip to content

Commit

Permalink
Preserve decorators position when printing
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Feb 7, 2023
1 parent 37849e1 commit 1fb3036
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 29 deletions.
17 changes: 9 additions & 8 deletions packages/babel-generator/src/generators/classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@ export function ClassDeclaration(
node: t.ClassDeclaration,
parent: t.Node,
) {
if (process.env.BABEL_8_BREAKING) {
const inExport =
isExportDefaultDeclaration(parent) || isExportNamedDeclaration(parent);

if (
!inExport ||
!this._shouldPrintDecoratorsBeforeExport(
parent as t.ExportDeclaration & { declaration: t.ClassDeclaration },
)
) {
this.printJoin(node.decorators, node);
} else {
if (
!this.format.decoratorsBeforeExport ||
(!isExportDefaultDeclaration(parent) && !isExportNamedDeclaration(parent))
) {
this.printJoin(node.decorators, node);
}
}

if (node.declare) {
Expand Down
12 changes: 12 additions & 0 deletions packages/babel-generator/src/generators/expressions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,18 @@ function shouldParenthesizeDecoratorExpression(
);
}

export function _shouldPrintDecoratorsBeforeExport(
this: Printer,
node: t.ExportDeclaration & { declaration: t.ClassDeclaration },
) {
if (typeof this.format.decoratorsBeforeExport === "boolean") {
return this.format.decoratorsBeforeExport;
}
return (
typeof node.start === "number" && node.start === node.declaration.start
);
}

export function Decorator(this: Printer, node: t.Decorator) {
this.token("@");
const { expression } = node;
Expand Down
32 changes: 16 additions & 16 deletions packages/babel-generator/src/generators/modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,18 +106,25 @@ export function ExportAllDeclaration(
this.semicolon();
}

function maybePrintDecoratorsBeforeExport(
printer: Printer,
node: t.ExportNamedDeclaration | t.ExportDefaultDeclaration,
) {
if (
isClassDeclaration(node.declaration) &&
printer._shouldPrintDecoratorsBeforeExport(
node as t.ExportNamedDeclaration & { declaration: t.ClassDeclaration },
)
) {
printer.printJoin(node.declaration.decorators, node);
}
}

export function ExportNamedDeclaration(
this: Printer,
node: t.ExportNamedDeclaration,
) {
if (!process.env.BABEL_8_BREAKING) {
if (
this.format.decoratorsBeforeExport &&
isClassDeclaration(node.declaration)
) {
this.printJoin(node.declaration.decorators, node);
}
}
maybePrintDecoratorsBeforeExport(this, node);

this.word("export");
this.space();
Expand Down Expand Up @@ -183,14 +190,7 @@ export function ExportDefaultDeclaration(
this: Printer,
node: t.ExportDefaultDeclaration,
) {
if (!process.env.BABEL_8_BREAKING) {
if (
this.format.decoratorsBeforeExport &&
isClassDeclaration(node.declaration)
) {
this.printJoin(node.declaration.decorators, node);
}
}
maybePrintDecoratorsBeforeExport(this, node);

this.word("export");
this.noIndentInnerCommentsHere();
Expand Down
7 changes: 4 additions & 3 deletions packages/babel-generator/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ function normalizeOptions(
};

if (!process.env.BABEL_8_BREAKING) {
format.decoratorsBeforeExport = !!opts.decoratorsBeforeExport;
format.decoratorsBeforeExport = opts.decoratorsBeforeExport;
format.jsonCompatibleStrings = opts.jsonCompatibleStrings;
}

Expand Down Expand Up @@ -200,8 +200,9 @@ export interface GeneratorOptions {
jsonCompatibleStrings?: boolean;

/**
* Set to true to enable support for experimental decorators syntax before module exports.
* Defaults to `false`.
* Set to true to enable support for experimental decorators syntax before
* module exports. If not specified, decorators will be printed in the same
* position as they were in the input source code.
* @deprecated Removed in Babel 8
*/
decoratorsBeforeExport?: boolean;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@dec export class A {}
@dec export default class {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@dec
export class A {}
@dec
export default class {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export @dec class A {}
export default @dec class {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export @dec
class A {}
export default @dec
class {}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
{
"plugins": [["decorators", { "decoratorsBeforeExport": false }]],
"decoratorsBeforeExport": true
"plugins": ["decorators"]
}

0 comments on commit 1fb3036

Please sign in to comment.