Skip to content

Commit

Permalink
[corejs3] Inject esnext.symbol.metadata for decorated classes (#179)
Browse files Browse the repository at this point in the history
Co-authored-by: liuxingbaoyu <[email protected]>
  • Loading branch information
nicolo-ribaudo and liuxingbaoyu authored Sep 23, 2023
1 parent 8999c65 commit 3416609
Show file tree
Hide file tree
Showing 7 changed files with 169 additions and 102 deletions.
2 changes: 2 additions & 0 deletions packages/babel-plugin-polyfill-corejs3/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@
"devDependencies": {
"@babel/core": "^7.22.6",
"@babel/helper-plugin-test-runner": "^7.22.5",
"@babel/plugin-proposal-decorators": "^7.22.15",
"@babel/plugin-transform-class-properties": "^7.22.5",
"@babel/plugin-transform-classes": "^7.22.6",
"@babel/plugin-transform-for-of": "^7.22.5",
"@babel/plugin-transform-modules-commonjs": "^7.22.5",
"@babel/plugin-transform-runtime": "^7.22.15",
"@babel/plugin-transform-spread": "^7.22.5",
"core-js": "^3.32.0",
"core-js-pure": "^3.32.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,11 @@ const IteratorDependencies = [
"es.object.to-string",
];

export const DecoratorMetadataDependencies = [
"esnext.symbol.metadata",
"esnext.function.metadata",
];

const TypedArrayStaticMethods = {
from: define(null, ["es.typed-array.from"]),
fromAsync: define(null, [
Expand Down Expand Up @@ -640,10 +645,7 @@ export const StaticProperties: ObjectMap2<CoreJSPolyfillDescriptor> = {
"es.symbol.match-all",
"es.string.match-all",
]),
metadata: define("symbol/metadata", [
"esnext.symbol.metadata",
"esnext.function.metadata",
]),
metadata: define("symbol/metadata", DecoratorMetadataDependencies),
metadataKey: define("symbol/metadata-key", ["esnext.symbol.metadata-key"]),
observable: define("symbol/observable", ["esnext.symbol.observable"]),
patternMatch: define("symbol/pattern-match", [
Expand Down
13 changes: 13 additions & 0 deletions packages/babel-plugin-polyfill-corejs3/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
PromiseDependenciesWithIterators,
StaticProperties,
InstanceProperties,
DecoratorMetadataDependencies,
type CoreJSPolyfillDescriptor,
} from "./built-in-definitions";
import canSkipPolyfill from "./usage-filters";
Expand Down Expand Up @@ -375,6 +376,18 @@ export default defineProvider<Options>(function (
maybeInjectGlobal(CommonIterators, getUtils(path));
}
},

// Decorators metadata
Class(path: NodePath<t.Class>) {
const hasDecorators =
path.node.decorators?.length ||
path.node.body.body.some(
el => (el as t.ClassMethod).decorators?.length,
);
if (hasDecorators) {
maybeInjectGlobal(DecoratorMetadataDependencies, getUtils(path));
}
},
},
};
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class A {
@dec foo = 2;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"plugins": [
["@@/polyfill-corejs3", { "method": "usage-global", "version": "3.31.0", "proposals": true }],
["@babel/plugin-proposal-decorators", { "version": "2023-05" }],
["@babel/plugin-transform-runtime", { "version": "7.100.0" }]
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
var _applyDecs = require("@babel/runtime-corejs3/helpers/applyDecs2305").default;
var _dec, _init_foo;
require("core-js/modules/esnext.function.metadata.js");
require("core-js/modules/esnext.symbol.metadata.js");
require("core-js/modules/es.string.iterator.js");
require("core-js/modules/es.array.iterator.js");
require("core-js/modules/web.dom-collections.iterator.js");
_dec = dec;
class A {
static {
[_init_foo] = _applyDecs(this, [[_dec, 0, "foo"]], []).e;
}
foo = _init_foo(this, 2);
}
Loading

0 comments on commit 3416609

Please sign in to comment.