Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2021-12] Support class decorators in export declarations #14396

Merged
merged 1 commit into from
Mar 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/babel-plugin-proposal-decorators/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"@babel/helper-create-class-features-plugin": "workspace:^",
"@babel/helper-plugin-utils": "workspace:^",
"@babel/helper-replace-supers": "workspace:^",
"@babel/helper-split-export-declaration": "workspace:^",
"@babel/plugin-syntax-decorators": "workspace:^",
"charcodes": "^0.2.0"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { NodePath, Scope } from "@babel/traverse";
import { types as t, template } from "@babel/core";
import syntaxDecorators from "@babel/plugin-syntax-decorators";
import ReplaceSupers from "@babel/helper-replace-supers";
import splitExportDeclaration from "@babel/helper-split-export-declaration";
import * as charCodes from "charcodes";

type ClassDecoratableElement =
Expand Down Expand Up @@ -1009,24 +1010,23 @@ export default function ({ assertVersion, assumption }, { loose }) {
inherits: syntaxDecorators,

visitor: {
ClassDeclaration(path: NodePath<t.ClassDeclaration>, state: any) {
if (VISITED.has(path)) return;

const newPath = transformClass(path, state, constantSuper);

if (newPath) {
VISITED.add(newPath);
"ExportNamedDeclaration|ExportDefaultDeclaration"(path) {
const { declaration } = path.node;
if (
declaration?.type === "ClassDeclaration" &&
// When compiling class decorators we need to replace the class
// binding, so we must split it in two separate declarations.
declaration.decorators?.length > 0
) {
splitExportDeclaration(path);
}
},

ClassExpression(path: NodePath<t.ClassExpression>, state: any) {
Class(path: NodePath<t.Class>, state: any) {
if (VISITED.has(path)) return;

const newPath = transformClass(path, state, constantSuper);

if (newPath) {
VISITED.add(newPath);
}
if (newPath) VISITED.add(newPath);
},
},
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default @dec class A {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
var _initClass, _dec;

let _A;

_dec = dec
Copy link
Member Author

Choose a reason for hiding this comment

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

Here there is a missing ExpressionStatement, I'll open a new PR.


class A {
static {
[_A, _initClass] = babelHelpers.applyDecs(this, [], [_dec]);
}
static {
_initClass();

}
}

export { _A as default };
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default @dec class {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
var _initClass, _dec;

let _default2;

_dec = dec

class _default {
static {
[_default2, _initClass] = babelHelpers.applyDecs(this, [], [_dec]);
}
static {
_initClass();

}
}

export { _default2 as default };
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export class A {
@dec x;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var _dec, _init_x;

_dec = dec
export class A {
static {
[_init_x] = babelHelpers.applyDecs(this, [[_dec, 0, "x"]], []);
}
x = _init_x(this);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export @dec class A {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
var _initClass, _dec;

let _A;

_dec = dec

class A {
static {
[_A, _initClass] = babelHelpers.applyDecs(this, [], [_dec]);
}
static {
_initClass();

}
}

export { _A as A };
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export class A {}

class B {}
export { B };
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export class A {}

class B {}

export { B };
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"plugins": [["proposal-decorators", { "version": "2021-12" }]]
}
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1309,6 +1309,7 @@ __metadata:
"@babel/helper-plugin-test-runner": "workspace:^"
"@babel/helper-plugin-utils": "workspace:^"
"@babel/helper-replace-supers": "workspace:^"
"@babel/helper-split-export-declaration": "workspace:^"
"@babel/plugin-syntax-decorators": "workspace:^"
"@babel/traverse": "workspace:^"
babel-plugin-polyfill-es-shims: ^0.6.0
Expand Down