Skip to content

Commit

Permalink
feat(es/compat): Implement loose mode for class_properties (#3722)
Browse files Browse the repository at this point in the history
  • Loading branch information
Austaras committed Feb 26, 2022
1 parent d23e19a commit 14155eb
Show file tree
Hide file tree
Showing 40 changed files with 1,388 additions and 642 deletions.
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;
})();
16 changes: 8 additions & 8 deletions crates/swc/tests/tsc-references/classExpression3_es5.1.normal.js
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)
;
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

1 comment on commit 14155eb

@github-actions
Copy link

Choose a reason for hiding this comment

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

Benchmark

Benchmark suite Current: 14155eb Previous: d23e19a Ratio
full_es2015 161630320 ns/iter (± 31582901) 218390906 ns/iter (± 22835954) 0.74
full_es2016 178726641 ns/iter (± 19231431) 225927439 ns/iter (± 19268688) 0.79
full_es2017 178834214 ns/iter (± 40347115) 226197335 ns/iter (± 18194498) 0.79
full_es2018 176653869 ns/iter (± 22181386) 226739859 ns/iter (± 17648367) 0.78
full_es2019 178140924 ns/iter (± 31564308) 229200980 ns/iter (± 29608015) 0.78
full_es2020 160296432 ns/iter (± 16471539) 197670899 ns/iter (± 33786199) 0.81
full_es3 219455448 ns/iter (± 15704702) 289749546 ns/iter (± 22671119) 0.76
full_es5 225429420 ns/iter (± 23526362) 287814546 ns/iter (± 19535184) 0.78
parser 678361 ns/iter (± 17909) 903496 ns/iter (± 111197) 0.75

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.