Skip to content

Commit bb6dde7

Browse files
authored
fix(es/compat): Handle useDefineForClassFields: false (#7055)
**BREAKING CHANGE:** IMPORTANT NOTE: Users of decorators are recommended to configure `"useDefineForClassFields": false` to ensure that your code is properly transpiled. **Related issue:** - Closes #6985.
1 parent caaf29d commit bb6dde7

File tree

339 files changed

+5546
-2960
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

339 files changed

+5546
-2960
lines changed

crates/swc/src/builder.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,10 @@ impl<'a, 'b, P: swc_ecma_visit::Fold> PassBuilder<'a, 'b, P> {
213213
feature_flag = enable_available_feature_from_es_version(self.target);
214214

215215
Either::Right(chain!(
216+
Optional::new(
217+
compat::class_fields_use_set::class_fields_use_set(assumptions.pure_getters),
218+
assumptions.set_public_class_fields,
219+
),
216220
Optional::new(
217221
compat::es2022::es2022(
218222
comments,

crates/swc/src/config/mod.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -363,10 +363,10 @@ impl Options {
363363
// variable management system based on the syntax contexts.
364364
if syntax.typescript() {
365365
assumptions.set_class_methods = !transform.use_define_for_class_fields.into_bool();
366-
assumptions.set_public_class_fields =
367-
!transform.use_define_for_class_fields.into_bool();
368366
}
369367

368+
assumptions.set_public_class_fields = !transform.use_define_for_class_fields.into_bool();
369+
370370
program.visit_mut_with(&mut resolver(
371371
unresolved_mark,
372372
top_level_mark,
@@ -669,7 +669,6 @@ impl Options {
669669
.into_bool(),
670670
ts_enum_is_readonly: assumptions.ts_enum_is_readonly,
671671
},
672-
use_define_for_class_fields: !assumptions.set_public_class_fields,
673672
import_export_assign_config,
674673
..Default::default()
675674
},
@@ -1478,7 +1477,7 @@ pub struct TransformConfig {
14781477
pub treat_const_enum_as_enum: BoolConfig<false>,
14791478

14801479
#[serde(default)]
1481-
pub use_define_for_class_fields: BoolConfig<false>,
1480+
pub use_define_for_class_fields: BoolConfig<true>,
14821481
}
14831482

14841483
#[derive(Debug, Default, Clone, Serialize, Deserialize, Merge)]

crates/swc/tests/exec.rs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ use std::{
88
use anyhow::{Context, Error};
99
use once_cell::sync::Lazy;
1010
use swc::{
11-
config::{Config, JsMinifyOptions, JscConfig, ModuleConfig, Options, SourceMapsConfig},
11+
config::{
12+
Config, JsMinifyOptions, JscConfig, ModuleConfig, Options, SourceMapsConfig,
13+
TransformConfig,
14+
},
1215
try_with_handler, BoolOrDataConfig, Compiler, HandlerOpts,
1316
};
1417
use swc_common::{errors::ColorConfig, SourceMap, GLOBALS};
@@ -113,10 +116,7 @@ fn init_helpers() -> Arc<PathBuf> {
113116
}
114117

115118
fn create_matrix(entry: &Path) -> Vec<Options> {
116-
// use_define_for_class_fields: false
117-
// force to use [[Set]] instead of [[Define]]
118-
// EsVersion should be lower than EsVersion::Es2022
119-
let force_set_class_field = entry
119+
let use_define_for_class_fields = entry
120120
.parent()
121121
.map(|parent| parent.join(".swcrc"))
122122
.and_then(|path| fs::read_to_string(path).ok())
@@ -133,8 +133,8 @@ fn create_matrix(entry: &Path) -> Vec<Options> {
133133
})
134134
.and_then(|content| serde_json::from_value::<Config>(content).ok())
135135
.and_then(|config| config.jsc.transform.into_inner())
136-
.map(|c| c.use_define_for_class_fields == false.into())
137-
.unwrap_or(false);
136+
.map(|c| c.use_define_for_class_fields)
137+
.unwrap_or_default();
138138

139139
[
140140
EsVersion::Es2022,
@@ -148,7 +148,6 @@ fn create_matrix(entry: &Path) -> Vec<Options> {
148148
EsVersion::Es5,
149149
]
150150
.into_iter()
151-
.filter(|e| !force_set_class_field || e < &EsVersion::Es2022)
152151
.matrix(|| {
153152
let default_es = Syntax::Es(EsConfig {
154153
..Default::default()
@@ -177,7 +176,11 @@ fn create_matrix(entry: &Path) -> Vec<Options> {
177176
config: Config {
178177
jsc: JscConfig {
179178
syntax: Some(syntax),
180-
transform: None.into(),
179+
transform: Some(TransformConfig {
180+
use_define_for_class_fields,
181+
..Default::default()
182+
})
183+
.into(),
181184
// true, false
182185
external_helpers: (!external_helpers).into(),
183186
target: Some(target),
@@ -307,11 +310,19 @@ fn get_expected_stdout(input: &Path) -> Result<String, Error> {
307310
&Options {
308311
config: Config {
309312
jsc: JscConfig {
310-
target: Some(EsVersion::Es2021),
313+
target: Some(EsVersion::Es2022),
311314
syntax: Some(Syntax::Typescript(TsConfig {
312315
decorators: true,
313316
..Default::default()
314317
})),
318+
transform: Some(TransformConfig {
319+
use_define_for_class_fields: (!input
320+
.to_string_lossy()
321+
.contains("set_public_class_fields"))
322+
.into(),
323+
..Default::default()
324+
})
325+
.into(),
315326
..Default::default()
316327
},
317328
module: match input.extension() {

crates/swc/tests/exec/issues-1xxx/1592/.swcrc renamed to crates/swc/tests/exec/issues-1xxx/1592/set_public_class_fields/.swcrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
},
1313
"transform": {
1414
"legacyDecorator": true,
15-
"decoratorMetadata": true
15+
"decoratorMetadata": true,
16+
"useDefineForClassFields": false
1617
}
1718
},
1819
"module": {

crates/swc/tests/fixture/issues-1xxx/1160/output/entry.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Object.defineProperty(exports, "__esModule", {
33
value: true
44
});
5+
const _defineProperty = require("@swc/helpers/lib/_define_property.js").default;
56
const _tsDecorate = require("@swc/helpers/lib/_ts_decorate.js").default;
67
const _tsMetadata = require("@swc/helpers/lib/_ts_metadata.js").default;
78
var MyEnum;
@@ -10,6 +11,9 @@ var MyEnum;
1011
MyEnum["y"] = "yyy";
1112
})(MyEnum || (MyEnum = {}));
1213
class Xpto {
14+
constructor(){
15+
_defineProperty(this, "value", void 0);
16+
}
1317
}
1418
_tsDecorate([
1519
Decorator(),
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
import _define_property from "@swc/helpers/src/_define_property.mjs";
12
class Foo {
23
constructor(){
3-
this.static = 5;
4-
this.declare = 5;
4+
_define_property(this, "static", 5);
5+
_define_property(this, "declare", 5);
56
}
67
}

crates/swc/tests/fixture/issues-1xxx/1278/output/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Object.defineProperty(exports, "__esModule", {
33
value: true
44
});
5+
const _defineProperty = require("@swc/helpers/lib/_define_property.js").default;
56
const _tsDecorate = require("@swc/helpers/lib/_ts_decorate.js").default;
67
const _tsMetadata = require("@swc/helpers/lib/_ts_metadata.js").default;
78
function MyDecorator(klass) {
@@ -11,6 +12,9 @@ function MyDecorator(klass) {
1112
};
1213
}
1314
class MyClass {
15+
constructor(){
16+
_defineProperty(this, "prop", void 0);
17+
}
1418
}
1519
_tsDecorate([
1620
MyDecorator(MyClass),

0 commit comments

Comments
 (0)