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(es/transforms/compat): loose mode for class_properties #3722

Merged
merged 10 commits into from Feb 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion crates/swc/src/builder.rs
Expand Up @@ -184,7 +184,14 @@ impl<'a, 'b, P: swc_ecma_visit::Fold> PassBuilder<'a, 'b, P> {
} else {
Either::Right(chain!(
Optional::new(
compat::es2022::es2022(compat::es2022::Config { loose: self.loose }),
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
}
}),
should_enable(self.target, EsVersion::Es2022)
),
Optional::new(
Expand Down
15 changes: 1 addition & 14 deletions crates/swc/tests/fixture/issue-2011/no-minify/output/index.js
@@ -1,16 +1,3 @@
function _defineProperty(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
}
var ClassA = function ClassA() {
"use strict";
};
Expand All @@ -24,6 +11,6 @@ module.exports = (function() {
};
return ClassB;
}();
_defineProperty(ClassB, "MyA", ClassA);
ClassB.MyA = ClassA;
return ClassB;
})();
17 changes: 6 additions & 11 deletions crates/swc/tests/fixture/issue-2011/reduced/output/index.js
@@ -1,16 +1,11 @@
var a = function() {};
module.exports = (function() {
var b, c, d, e = function() {
var b = function() {
"use strict";
function e() {}
return e.prototype.it = function() {
this.bb = new e.MyA();
}, e;
function b() {}
return b.prototype.it = function() {
this.bb = new b.MyA();
}, b;
}();
return b = e, c = "MyA", d = a, c in b ? Object.defineProperty(b, c, {
value: d,
enumerable: !0,
configurable: !0,
writable: !0
}) : b[c] = d, e;
return b.MyA = a, b;
})();
Expand Up @@ -69,26 +69,26 @@ function _createSuper(Derived) {
return _possibleConstructorReturn(this, result);
};
}
var C = /*#__PURE__*/ function(_super) {
var C = /*#__PURE__*/ function(__class) {
"use strict";
_inherits(_class, _super);
var _super1 = _createSuper(_class);
_inherits(_class, __class);
var _super = _createSuper(_class);
function _class() {
_classCallCheck(this, _class);
var _this;
_this = _super1.apply(this, arguments);
_this = _super.apply(this, arguments);
_this.c = 3;
return _this;
}
return _class;
}(/*#__PURE__*/ function(_super) {
}(/*#__PURE__*/ function(__class) {
"use strict";
_inherits(_class, _super);
var _super2 = _createSuper(_class);
_inherits(_class, __class);
var _super = _createSuper(_class);
function _class() {
_classCallCheck(this, _class);
var _this;
_this = _super2.apply(this, arguments);
_this = _super.apply(this, arguments);
_this.b = 2;
return _this;
}
Expand Down
Expand Up @@ -44,22 +44,22 @@ function _createSuper(Derived) {
})(self);
};
}
var c = new (function(_super) {
var c = new (function(__class) {
"use strict";
_inherits(_class, _super);
var _super1 = _createSuper(_class);
_inherits(_class, __class);
var _super = _createSuper(_class);
function _class() {
var _this;
return _classCallCheck(this, _class), _this = _super1.apply(this, arguments), _this.c = 3, _this;
return _classCallCheck(this, _class), _this = _super.apply(this, arguments), _this.c = 3, _this;
}
return _class;
}(function(_super) {
}(function(__class) {
"use strict";
_inherits(_class, _super);
var _super2 = _createSuper(_class);
_inherits(_class, __class);
var _super = _createSuper(_class);
function _class() {
var _this;
return _classCallCheck(this, _class), _this = _super2.apply(this, arguments), _this.b = 2, _this;
return _classCallCheck(this, _class), _this = _super.apply(this, arguments), _this.b = 2, _this;
}
return _class;
}(function _class() {
Expand Down
Expand Up @@ -70,26 +70,26 @@ function _createSuper(Derived) {
};
}
// @target: es6
var C = /*#__PURE__*/ function(_super) {
var C = /*#__PURE__*/ function(__class) {
"use strict";
_inherits(_class, _super);
var _super1 = _createSuper(_class);
_inherits(_class, __class);
var _super = _createSuper(_class);
function _class() {
_classCallCheck(this, _class);
var _this;
_this = _super1.apply(this, arguments);
_this = _super.apply(this, arguments);
_this.c = 3;
return _this;
}
return _class;
}(/*#__PURE__*/ function(_super) {
}(/*#__PURE__*/ function(__class) {
"use strict";
_inherits(_class, _super);
var _super2 = _createSuper(_class);
_inherits(_class, __class);
var _super = _createSuper(_class);
function _class() {
_classCallCheck(this, _class);
var _this;
_this = _super2.apply(this, arguments);
_this = _super.apply(this, arguments);
_this.b = 2;
return _this;
}
Expand Down
Expand Up @@ -44,22 +44,22 @@ function _createSuper(Derived) {
})(self);
};
}
var c = new (function(_super) {
var c = new (function(__class) {
"use strict";
_inherits(_class, _super);
var _super1 = _createSuper(_class);
_inherits(_class, __class);
var _super = _createSuper(_class);
function _class() {
var _this;
return _classCallCheck(this, _class), _this = _super1.apply(this, arguments), _this.c = 3, _this;
return _classCallCheck(this, _class), _this = _super.apply(this, arguments), _this.c = 3, _this;
}
return _class;
}(function(_super) {
}(function(__class) {
"use strict";
_inherits(_class, _super);
var _super2 = _createSuper(_class);
_inherits(_class, __class);
var _super = _createSuper(_class);
function _class() {
var _this;
return _classCallCheck(this, _class), _this = _super2.apply(this, arguments), _this.b = 2, _this;
return _classCallCheck(this, _class), _this = _super.apply(this, arguments), _this.b = 2, _this;
}
return _class;
}(function _class() {
Expand Down
Expand Up @@ -41,7 +41,7 @@ class A {
var _foo = {
writable: true,
value: true
} // error (duplicate)
}// error (duplicate)
;
class B {
test(x) {
Expand Down
Expand Up @@ -59,7 +59,7 @@ var A = function A() {
var _foo = {
writable: true,
value: true
} // error (duplicate)
}// error (duplicate)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no actual idea why this is happening, but I guess it's fine.

;
var B = /*#__PURE__*/ function() {
"use strict";
Expand Down
7 changes: 6 additions & 1 deletion crates/swc_ecma_preset_env/src/lib.rs
Expand Up @@ -92,7 +92,12 @@ where
let pass = add!(
pass,
ClassProperties,
es2022::class_properties(es2022::class_properties::Config { loose })
es2022::class_properties(es2022::class_properties::Config {
private_as_properties: loose,
set_public_fields: loose,
constant_super: loose,
no_document_all: loose
})
);
let pass = add!(pass, PrivatePropertyInObject, es2022::private_in_object());

Expand Down
@@ -1 +1,5 @@
[1, 2, 3][flatMap];
[
1,
2,
3
][flatMap];
@@ -1,8 +1,9 @@
var __ = _classPrivateFieldLooseKey("__");
class A {
}
var __ = {
Object.defineProperty(A, __, {
writable: true,
value: (()=>{
A.abc = 123;
})()
};
});
9 changes: 5 additions & 4 deletions crates/swc_ecma_preset_env/tests/test.rs
Expand Up @@ -20,8 +20,8 @@ use swc_ecma_ast::*;
use swc_ecma_codegen::Emitter;
use swc_ecma_parser::{EsConfig, Parser, Syntax};
use swc_ecma_preset_env::{preset_env, Config, FeatureOrModule, Mode, Targets, Version};
use swc_ecma_transforms::fixer;
use swc_ecma_utils::drop_span;
use swc_ecma_transforms::{fixer, helpers};
use swc_ecma_utils::{drop_span, HANDLER};
use swc_ecma_visit::{as_folder, FoldWith, VisitMut};
use testing::{NormalizedOutput, Tester};

Expand Down Expand Up @@ -134,7 +134,6 @@ fn exec(c: PresetConfig, dir: PathBuf) -> Result<(), Error> {
v => unreachable!("invalid: {:?}", v),
},
skip: vec![],
// TODO
loose: true,
// TODO
dynamic_import: true,
Expand Down Expand Up @@ -194,7 +193,9 @@ fn exec(c: PresetConfig, dir: PathBuf) -> Result<(), Error> {
e.into_diagnostic(&handler).emit()
}

let actual = module.fold_with(&mut pass);
let actual = helpers::HELPERS.set(&Default::default(), || {
HANDLER.set(&handler, || module.fold_with(&mut pass))
});

// debug mode?
if dir.join("stdout.txt").exists() {
Expand Down