Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: unify class properties transform in ts and js #3766

Merged
merged 17 commits into from
Mar 5, 2022
Merged
  •  
  •  
  •  
35 changes: 19 additions & 16 deletions crates/swc/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,16 +183,18 @@ impl<'a, 'b, P: swc_ecma_visit::Fold> PassBuilder<'a, 'b, P> {
self.top_level_mark,
comments,
env,
self.assumptions,
))
} else {
let assumptions = self.assumptions;
Either::Right(chain!(
Optional::new(
compat::es2022::es2022(compat::es2022::Config {
class_properties: compat::es2022::class_properties::Config {
private_as_properties: self.loose,
constant_super: self.loose,
set_public_fields: self.loose,
no_document_all: self.loose
private_as_properties: assumptions.private_fields_as_properties,
constant_super: assumptions.constant_super,
set_public_fields: assumptions.set_public_class_fields,
no_document_all: assumptions.no_document_all
}
}),
should_enable(self.target, EsVersion::Es2022)
Expand All @@ -204,11 +206,11 @@ impl<'a, 'b, P: swc_ecma_visit::Fold> PassBuilder<'a, 'b, P> {
Optional::new(
compat::es2020::es2020(compat::es2020::Config {
nullish_coalescing: compat::es2020::nullish_coalescing::Config {
no_document_all: self.loose
no_document_all: assumptions.no_document_all
},
optional_chaining: compat::es2020::opt_chaining::Config {
no_document_all: self.loose,
pure_getter: self.loose
no_document_all: assumptions.no_document_all,
pure_getter: assumptions.pure_getters
}
}),
should_enable(self.target, EsVersion::Es2020)
Expand All @@ -220,8 +222,8 @@ impl<'a, 'b, P: swc_ecma_visit::Fold> PassBuilder<'a, 'b, P> {
Optional::new(
compat::es2018(compat::es2018::Config {
object_rest_spread: compat::es2018::object_rest_spread::Config {
no_symbol: self.loose,
set_property: self.loose
no_symbol: assumptions.object_rest_no_symbols,
set_property: assumptions.set_spread_properties
}
}),
should_enable(self.target, EsVersion::Es2018)
Expand All @@ -240,10 +242,11 @@ impl<'a, 'b, P: swc_ecma_visit::Fold> PassBuilder<'a, 'b, P> {
comments,
compat::es2015::Config {
classes: compat::es2015::classes::Config {
constant_super: self.loose,
no_class_calls: self.loose,
set_class_methods: self.loose,
super_is_callable_constructor: self.loose
constant_super: assumptions.constant_super,
no_class_calls: assumptions.no_class_calls,
set_class_methods: assumptions.set_class_methods,
super_is_callable_constructor: assumptions
.super_is_callable_constructor
},
computed_props: compat::es2015::computed_props::Config {
loose: self.loose
Expand All @@ -257,11 +260,11 @@ impl<'a, 'b, P: swc_ecma_visit::Fold> PassBuilder<'a, 'b, P> {
},
regenerator: self.regenerator,
template_literal: compat::es2015::template_literal::Config {
ignore_to_primitive: self.loose,
mutable_template: self.loose
ignore_to_primitive: assumptions.ignore_to_primitive_hint,
mutable_template: assumptions.mutable_template_object
},
parameters: compat::es2015::parameters::Config {
ignore_function_length: self.loose,
ignore_function_length: assumptions.ignore_function_length,
},
typescript: syntax.typescript()
}
Expand Down
13 changes: 10 additions & 3 deletions crates/swc/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ impl Options {
..
} = config.jsc;

let assumptions = assumptions.unwrap_or_else(|| {
let mut assumptions = assumptions.unwrap_or_else(|| {
if loose {
Assumptions::all()
} else {
Expand All @@ -317,18 +317,21 @@ impl Options {

let mut program = parse(syntax, es_version, is_module)?;

let mut transform = transform.unwrap_or_default();

// Do a resolver pass before everything.
//
// We do this before creating custom passses, so custom passses can use the
// variable management system based on the syntax contexts.
if syntax.typescript() {
// assumptions.set_class_methods = !transform.use_define_for_class_fields;
assumptions.set_public_class_fields = !transform.use_define_for_class_fields;

program.visit_mut_with(&mut ts_resolver(top_level_mark));
} else {
program.visit_mut_with(&mut resolver_with_mark(top_level_mark));
}

let mut transform = transform.unwrap_or_default();

if program.is_module() {
js_minify = js_minify.map(|c| {
let compress = c
Expand Down Expand Up @@ -477,6 +480,7 @@ impl Options {
treat_const_enum_as_enum: transform.treat_const_enum_as_enum,
ts_enum_is_readonly: assumptions.ts_enum_is_readonly,
},
use_define_for_class_fields: !assumptions.set_public_class_fields,
..Default::default()
},
comments,
Expand Down Expand Up @@ -1186,6 +1190,9 @@ pub struct TransformConfig {

#[serde(default)]
pub treat_const_enum_as_enum: bool,

#[serde(default)]
pub use_define_for_class_fields: bool,
}

#[derive(Debug, Default, Clone, Serialize, Deserialize)]
Expand Down
5 changes: 2 additions & 3 deletions crates/swc/tests/fixture/issue-1505/case2/output/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ var MyClass = function MyClass() {
_classCallCheck(this, MyClass);
};
export var fn = function() {
var _class = /*#__PURE__*/ function(MyClass) {
return /*#__PURE__*/ (function(MyClass) {
"use strict";
_inherits(_class, MyClass);
var _super = _createSuper(_class);
Expand All @@ -83,6 +83,5 @@ export var fn = function() {
return _super.apply(this, arguments);
}
return _class;
}(MyClass);
return _class;
})(MyClass);
};
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ var MyClass = function MyClass() {
_classCallCheck(this, MyClass);
};
export var fn = function() {
var _class = /*#__PURE__*/ function(MyClass) {
return /*#__PURE__*/ (function(MyClass) {
"use strict";
_inherits(_class, MyClass);
var _super = _createSuper(_class);
Expand All @@ -83,6 +83,5 @@ export var fn = function() {
return _super.apply(this, arguments);
}
return _class;
}(MyClass);
return _class;
})(MyClass);
};
3 changes: 1 addition & 2 deletions crates/swc/tests/fixture/issue-1729/case1/output/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,9 @@ function createConstructor(callback) {
};
}
var constructor = createConstructor(function() {
var _class = function _class() {
return function _class() {
"use strict";
_classCallCheck(this, _class);
};
return _class;
});
console.log(constructor());
6 changes: 2 additions & 4 deletions crates/swc/tests/fixture/issue-1729/case4/output/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ function createConstructor(callback) {
return new klass(...args);
};
}
const constructor = createConstructor(()=>{
class _class {
const constructor = createConstructor(()=>class {
}
return _class;
});
);
console.log(constructor());
10 changes: 5 additions & 5 deletions crates/swc/tests/fixture/issue-1869/decorator/output/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ function _classCallCheck(instance, Constructor) {
throw new TypeError("Cannot call a class as a function");
}
}
var _TestClass;
var _class;
var _class1;
var TestClass = _class1 = someClassDecorator((_class1 = (_class = function TestClass() {
var TestClass = _class = someClassDecorator((_class = (_TestClass = function TestClass() {
"use strict";
_classCallCheck(this, TestClass);
}, _class.Something = 'hello', _class.SomeProperties = {
firstProp: _class.Something
}, _class)) || _class1) || _class1;
}, _TestClass.Something = 'hello', _TestClass.SomeProperties = {
firstProp: _TestClass.Something
}, _TestClass)) || _class) || _class;
function someClassDecorator(c) {
return c;
}
2 changes: 1 addition & 1 deletion crates/swc/tests/fixture/issue-2086/output/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
class a{#a;constructor(a){this.#a=a} #b(){setTimeout(()=>{this.#a.textContent="TESTED"},1e3)}run(){this.#b()}}export{a as default}
export default class a{#a;constructor(a){this.#a=a} #b(){setTimeout(()=>{this.#a.textContent="TESTED"},1e3)}run(){this.#b()}}
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ var A;
this.y = y;
}
}
A1.Point = Point2;
Point2.Origin = {
x: 0,
y: 0
};
A1.Point = Point2;
(function(Point3) {
var Origin = Point3.Origin = "";
})(Point2 = A1.Point || (A1.Point = {}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ Point.Origin = {
this.x = x, this.y = y;
}
}
A1.Point = Point1, Point1.Origin = {
Point1.Origin = {
x: 0,
y: 0
}, (Point1 = A1.Point || (A1.Point = {})).Origin = "";
}, A1.Point = Point1, (Point1 = A1.Point || (A1.Point = {})).Origin = "";
})(A || (A = {}));
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ var A;
this.x = x;
this.y = y;
};
A1.Point = Point;
Point.Origin = {
x: 0,
y: 0
};
A1.Point = Point;
(function(Point) {
var Origin = Point.Origin = "";
})(Point = A1.Point || (A1.Point = {}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ Point.Origin = {
"use strict";
_classCallCheck(this, Point1), this.x = x, this.y = y;
};
A1.Point = Point1, Point1.Origin = {
Point1.Origin = {
x: 0,
y: 0
}, (Point1 = A1.Point || (A1.Point = {})).Origin = "";
}, A1.Point = Point1, (Point1 = A1.Point || (A1.Point = {})).Origin = "";
})(A || (A = {}));
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ var A;
this.y = y;
}
}
A1.Point = Point1;
Point1.Origin = {
x: 0,
y: 0
};
A1.Point = Point1;
(function(Point) {
var Origin = ""; // not an error since not exported
})(Point1 = A1.Point || (A1.Point = {}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ var A;
this.x = x, this.y = y;
}
}
A1.Point = Point, Point.Origin = {
Point.Origin = {
x: 0,
y: 0
};
}, A1.Point = Point;
})(A || (A = {}));
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ var A;
this.x = x;
this.y = y;
};
A1.Point = Point;
Point.Origin = {
x: 0,
y: 0
};
A1.Point = Point;
(function(Point) {
var Origin = ""; // not an error since not exported
})(Point = A1.Point || (A1.Point = {}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ Point.Origin = {
"use strict";
_classCallCheck(this, Point1), this.x = x, this.y = y;
};
A1.Point = Point1, Point1.Origin = {
Point1.Origin = {
x: 0,
y: 0
};
}, A1.Point = Point1;
})(A || (A = {}));
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,6 @@ function _createSuper(Derived1) {
var Base = function Base() {
"use strict";
_classCallCheck(this, Base);
// @target: esnext
// @useDefineForClassFields: true
this.x = 1;
};
var Derived = /*#__PURE__*/ function(Base) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,6 @@ function _createSuper(Derived) {
var A = function A() {
"use strict";
_classCallCheck(this, A);
// @target: esnext
// @useDefineForClassFields: false
this.p = 'yep';
};
var B = /*#__PURE__*/ function(A) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,6 @@ function _createSuper(Derived) {
var A = function A() {
"use strict";
_classCallCheck(this, A);
// @target: esnext
// @useDefineForClassFields: true
this.p = 'yep';
};
var B = /*#__PURE__*/ function(A) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
class _class {
}
// @target: ES6
// @module: amd
// @filename: a.ts
export { _class as default };
export default class {
};
// @filename: b.ts
export default function() {};
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
class _class {
}
export default class {
};
export default function() {};
export { _class as default };
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@ function _classCallCheck(instance, Constructor) {
throw new TypeError("Cannot call a class as a function");
}
}
var _class = function _class() {
var _default = function _default() {
"use strict";
_classCallCheck(this, _class);
_classCallCheck(this, _default);
};
// @target: ES6
// @module: amd
// @filename: a.ts
export { _class as default };
export { _default as default };
// @filename: b.ts
export default function() {};
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
var _class = function() {
var _default = function() {
"use strict";
!function(instance, Constructor) {
if (!(instance instanceof Constructor)) throw new TypeError("Cannot call a class as a function");
}(this, _class);
}(this, _default);
};
export default function() {};
export { _class as default };
export { _default as default };
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
class _class {
}
// @target: ES6
// @module: commonjs
// @filename: a.ts
export { _class as default };
export default class {
};
// @filename: b.ts
export default function() {};
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
class _class {
}
export default class {
};
export default function() {};
export { _class as default };
Loading