Skip to content

Commit 29dff68

Browse files
[2021-12] Support class decorators in export declarations (#14396)
1 parent fe00618 commit 29dff68

14 files changed

Lines changed: 92 additions & 12 deletions

File tree

packages/babel-plugin-proposal-decorators/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"@babel/helper-create-class-features-plugin": "workspace:^",
2424
"@babel/helper-plugin-utils": "workspace:^",
2525
"@babel/helper-replace-supers": "workspace:^",
26+
"@babel/helper-split-export-declaration": "workspace:^",
2627
"@babel/plugin-syntax-decorators": "workspace:^",
2728
"charcodes": "^0.2.0"
2829
},

packages/babel-plugin-proposal-decorators/src/transformer-2021-12.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { NodePath, Scope } from "@babel/traverse";
22
import { types as t, template } from "@babel/core";
33
import syntaxDecorators from "@babel/plugin-syntax-decorators";
44
import ReplaceSupers from "@babel/helper-replace-supers";
5+
import splitExportDeclaration from "@babel/helper-split-export-declaration";
56
import * as charCodes from "charcodes";
67

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

10111012
visitor: {
1012-
ClassDeclaration(path: NodePath<t.ClassDeclaration>, state: any) {
1013-
if (VISITED.has(path)) return;
1014-
1015-
const newPath = transformClass(path, state, constantSuper);
1016-
1017-
if (newPath) {
1018-
VISITED.add(newPath);
1013+
"ExportNamedDeclaration|ExportDefaultDeclaration"(path) {
1014+
const { declaration } = path.node;
1015+
if (
1016+
declaration?.type === "ClassDeclaration" &&
1017+
// When compiling class decorators we need to replace the class
1018+
// binding, so we must split it in two separate declarations.
1019+
declaration.decorators?.length > 0
1020+
) {
1021+
splitExportDeclaration(path);
10191022
}
10201023
},
10211024

1022-
ClassExpression(path: NodePath<t.ClassExpression>, state: any) {
1025+
Class(path: NodePath<t.Class>, state: any) {
10231026
if (VISITED.has(path)) return;
10241027

10251028
const newPath = transformClass(path, state, constantSuper);
1026-
1027-
if (newPath) {
1028-
VISITED.add(newPath);
1029-
}
1029+
if (newPath) VISITED.add(newPath);
10301030
},
10311031
},
10321032
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default @dec class A {}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
var _initClass, _dec;
2+
3+
let _A;
4+
5+
_dec = dec
6+
7+
class A {
8+
static {
9+
[_A, _initClass] = babelHelpers.applyDecs(this, [], [_dec]);
10+
}
11+
static {
12+
_initClass();
13+
14+
}
15+
}
16+
17+
export { _A as default };
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default @dec class {}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
var _initClass, _dec;
2+
3+
let _default2;
4+
5+
_dec = dec
6+
7+
class _default {
8+
static {
9+
[_default2, _initClass] = babelHelpers.applyDecs(this, [], [_dec]);
10+
}
11+
static {
12+
_initClass();
13+
14+
}
15+
}
16+
17+
export { _default2 as default };
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export class A {
2+
@dec x;
3+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
var _dec, _init_x;
2+
3+
_dec = dec
4+
export class A {
5+
static {
6+
[_init_x] = babelHelpers.applyDecs(this, [[_dec, 0, "x"]], []);
7+
}
8+
x = _init_x(this);
9+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export @dec class A {}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
var _initClass, _dec;
2+
3+
let _A;
4+
5+
_dec = dec
6+
7+
class A {
8+
static {
9+
[_A, _initClass] = babelHelpers.applyDecs(this, [], [_dec]);
10+
}
11+
static {
12+
_initClass();
13+
14+
}
15+
}
16+
17+
export { _A as A };

0 commit comments

Comments
 (0)