diff --git a/crates/swc/src/config/mod.rs b/crates/swc/src/config/mod.rs index 6dcc839795f..f944bcf52ed 100644 --- a/crates/swc/src/config/mod.rs +++ b/crates/swc/src/config/mod.rs @@ -703,11 +703,20 @@ impl Options { })), // Decorators may use type information Optional::new( - decorators(decorators::Config { - legacy: transform.legacy_decorator.into_bool(), - emit_metadata: transform.decorator_metadata.into_bool(), - use_define_for_class_fields: !assumptions.set_public_class_fields - }), + match transform.decorator_version.unwrap_or_default() { + DecoratorVersion::V202112 => { + Either::Left(decorators(decorators::Config { + 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() ), // The transform strips import assertions, so it's only enabled if @@ -1552,6 +1561,20 @@ pub struct TransformConfig { #[serde(default)] pub use_define_for_class_fields: BoolConfig, + + #[serde(default)] + pub decorator_version: Option, +} + +#[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)] diff --git a/crates/swc/tests/fixture/issues-7xxx/7220/1/input/.swcrc b/crates/swc/tests/fixture/issues-7xxx/7220/1/input/.swcrc new file mode 100644 index 00000000000..51a244a87a9 --- /dev/null +++ b/crates/swc/tests/fixture/issues-7xxx/7220/1/input/.swcrc @@ -0,0 +1,11 @@ +{ + "jsc": { + "parser": { + "syntax": "ecmascript", + "decorators": true + }, + "transform": { + "decoratorVersion": "2022-03" + } + } +} \ No newline at end of file diff --git a/crates/swc/tests/fixture/issues-7xxx/7220/1/input/index.js b/crates/swc/tests/fixture/issues-7xxx/7220/1/input/index.js new file mode 100644 index 00000000000..fc17931f8b4 --- /dev/null +++ b/crates/swc/tests/fixture/issues-7xxx/7220/1/input/index.js @@ -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(); + } +} \ No newline at end of file diff --git a/crates/swc/tests/fixture/issues-7xxx/7220/1/output/index.js b/crates/swc/tests/fixture/issues-7xxx/7220/1/output/index.js new file mode 100644 index 00000000000..d240a2f14d9 --- /dev/null +++ b/crates/swc/tests/fixture/issues-7xxx/7220/1/output/index.js @@ -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; +})();