feat(es): Expose stage 3 decorator (#7220)

This commit is contained in:
Donny/강동윤 2023-04-06 23:45:21 +09:00 committed by GitHub
parent 4b9cfbb4dd
commit a7a53c6208
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 93 additions and 5 deletions

View File

@ -703,11 +703,20 @@ impl Options {
})), })),
// Decorators may use type information // Decorators may use type information
Optional::new( Optional::new(
decorators(decorators::Config { match transform.decorator_version.unwrap_or_default() {
legacy: transform.legacy_decorator.into_bool(), DecoratorVersion::V202112 => {
emit_metadata: transform.decorator_metadata.into_bool(), Either::Left(decorators(decorators::Config {
use_define_for_class_fields: !assumptions.set_public_class_fields legacy: transform.legacy_decorator.into_bool(),
}), emit_metadata: transform.decorator_metadata.into_bool(),
use_define_for_class_fields: !assumptions.set_public_class_fields,
}))
}
DecoratorVersion::V202203 => {
Either::Right(
swc_ecma_transforms::proposals::decorator_2022_03::decorator_2022_03(),
)
}
},
syntax.decorators() syntax.decorators()
), ),
// The transform strips import assertions, so it's only enabled if // The transform strips import assertions, so it's only enabled if
@ -1552,6 +1561,20 @@ pub struct TransformConfig {
#[serde(default)] #[serde(default)]
pub use_define_for_class_fields: BoolConfig<true>, pub use_define_for_class_fields: BoolConfig<true>,
#[serde(default)]
pub decorator_version: Option<DecoratorVersion>,
}
#[derive(Debug, Default, Clone, Copy, Serialize, Deserialize)]
#[serde(deny_unknown_fields, rename_all = "camelCase")]
pub enum DecoratorVersion {
#[default]
#[serde(rename = "2021-12")]
V202112,
#[serde(rename = "2022-03")]
V202203,
} }
#[derive(Debug, Default, Clone, Serialize, Deserialize, Merge)] #[derive(Debug, Default, Clone, Serialize, Deserialize, Merge)]

View File

@ -0,0 +1,11 @@
{
"jsc": {
"parser": {
"syntax": "ecmascript",
"decorators": true
},
"transform": {
"decoratorVersion": "2022-03"
}
}
}

View File

@ -0,0 +1,14 @@
const _BOUND_FUNCTIONS = {};
export class BackgroundJob {
}
class TestClass {
calls = [];
@BackgroundJob.bind
success(s, n) {
this.calls.push(`success(${s}, ${n})`);
return Promise.resolve();
}
}

View File

@ -0,0 +1,40 @@
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
import { _ as _create_class } from "@swc/helpers/_/_create_class";
import { _ as _define_property } from "@swc/helpers/_/_define_property";
import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
import { _ as _apply_decs_2203_r } from "@swc/helpers/_/_apply_decs_2203_r";
var _dec, _initProto;
var _BOUND_FUNCTIONS = {};
export var BackgroundJob = function BackgroundJob() {
"use strict";
_class_call_check(this, BackgroundJob);
};
_dec = BackgroundJob.bind;
var TestClass = /*#__PURE__*/ function() {
"use strict";
function TestClass() {
_class_call_check(this, TestClass);
_define_property(this, "calls", []);
_initProto(this);
}
_create_class(TestClass, [
{
key: "success",
value: function success(s, n) {
this.calls.push("success(".concat(s, ", ").concat(n, ")"));
return Promise.resolve();
}
}
]);
return TestClass;
}();
(function() {
var ref, ref1;
ref = _apply_decs_2203_r(TestClass, [
[
_dec,
2,
"success"
]
], []), ref1 = _sliced_to_array(ref.e, 1), _initProto = ref1[0], ref1, ref;
})();