Skip to content

Commit

Permalink
[2021-12] Support class decorators in export declarations (#14396)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Mar 28, 2022
1 parent fe00618 commit 29dff68
Show file tree
Hide file tree
Showing 14 changed files with 92 additions and 12 deletions.
1 change: 1 addition & 0 deletions packages/babel-plugin-proposal-decorators/package.json
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
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
@@ -0,0 +1 @@
export default @dec class A {}
@@ -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 default };
@@ -0,0 +1 @@
export default @dec class {}
@@ -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 };
@@ -0,0 +1,3 @@
export class A {
@dec x;
}
@@ -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);
}
@@ -0,0 +1 @@
export @dec class A {}
@@ -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 };
@@ -0,0 +1,4 @@
export class A {}

class B {}
export { B };
@@ -0,0 +1,5 @@
export class A {}

class B {}

export { B };
@@ -0,0 +1,3 @@
{
"plugins": [["proposal-decorators", { "version": "2021-12" }]]
}
1 change: 1 addition & 0 deletions yarn.lock
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

0 comments on commit 29dff68

Please sign in to comment.