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): Add single-property optimization to destructuring #2511

Merged
merged 15 commits into from
Oct 22, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ecmascript/transforms/compat/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2018"
license = "Apache-2.0/MIT"
name = "swc_ecma_transforms_compat"
repository = "https://github.com/swc-project/swc.git"
version = "0.45.1"
version = "0.45.2"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[features]
Expand Down
31 changes: 31 additions & 0 deletions ecmascript/transforms/compat/src/es2015/destructuring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,21 @@ impl AssignFolder {
"destructuring pattern binding requires initializer"
);

if props.len() == 1 {
match &props[0] {
ObjectPatProp::Assign(p @ AssignPatProp { value: None, .. }) => {
decls.push(VarDeclarator {
span: decl.span,
name: Pat::Ident(p.key.clone().into()),
init: Some(Box::new(decl.init.unwrap().make_member(p.key.clone()))),
definite: false,
});
return;
}
_ => {}
}
}

let can_be_null = can_be_null(decl.init.as_ref().unwrap());
let ref_ident = make_ref_ident(self.c, decls, decl.init);

Expand Down Expand Up @@ -713,6 +728,22 @@ impl Fold for AssignFolder {
})
}
Pat::Object(ObjectPat { span, props, .. }) => {
if props.len() == 1 {
match &props[0] {
ObjectPatProp::Assign(p @ AssignPatProp { value: None, .. }) => {
return Expr::Assign(AssignExpr {
span,
op: op!("="),
left: PatOrExpr::Pat(Box::new(Pat::Ident(
p.key.clone().into(),
))),
right: Box::new(right.make_member(p.key.clone())),
});
}
_ => {}
}
}

let ref_ident = make_ref_ident(self.c, &mut self.vars, None);

let mut exprs = vec![];
Expand Down
30 changes: 27 additions & 3 deletions ecmascript/transforms/compat/tests/es2015_destructuring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ test!(
var { x: { y } = {} } = z;"#,
r#"var z = {
};
var tmp = z.x, ref = tmp === void 0 ? {} : tmp, y = ref.y;"#
var tmp = z.x, y = (tmp === void 0 ? {} : tmp).y;"#
);

test!(
Expand Down Expand Up @@ -527,7 +527,7 @@ const bar = {
qux: 'baz'
}
};
const _Foo = bar[Foo], qux = _Foo.qux;"
const qux = bar[Foo].qux;"
);

test!(
Expand Down Expand Up @@ -1225,7 +1225,7 @@ var { x: { y } = {} } = z;
"#,
r#"
var z = {};
var tmp = z.x, ref = tmp === void 0 ? {} : tmp, y = ref.y;
var tmp = z.x, y = (tmp === void 0 ? {} : tmp).y;
"#
);

Expand Down Expand Up @@ -1607,3 +1607,27 @@ test!(
ref = b, ref1 = ref[0], a = ref1 === void 0 ? 1 : ref1, ref;
"
);

test!(
syntax(),
|_| tr(),
next_001,
"
const { NODE_ENV }= process.env;
",
"
const NODE_ENV = process.env.NODE_ENV;
"
);

test!(
syntax(),
|_| tr(),
next_002,
"
({ NODE_ENV }= process.env);
",
"
NODE_ENV = process.env.NODE_ENV;
"
);
2 changes: 1 addition & 1 deletion ecmascript/transforms/module/tests/common_js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4282,7 +4282,7 @@ let SomeClass = function() {
_createClass(SomeClass, [{
key: 'call',
value: function call() {
var _props = this.props, myFunction = _props.myFunction;
var myFunction = this.props.myFunction;
if (myFunction) {
myFunction();
} else {
Expand Down
4 changes: 2 additions & 2 deletions tests/fixture/issue-2232/case1/output/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@ var Foo = /*#__PURE__*/ function() {
var ref;
while(1)switch(_ctx.prev = _ctx.next){
case 0:
var ref1, ref2;
var ref1;
ref1 = _param, ref = ref1.input, toNumber = ref.toNumber, messageBody = ref.messageBody, ref, ref1, all = _objectWithoutProperties(_param.input, [
"toNumber",
"messageBody"
]), ref2 = _param1, dataSources = ref2.dataSources, ref2;
]), dataSources = _param1.dataSources;
case 1:
case "end":
return _ctx.stop();
Expand Down
4 changes: 2 additions & 2 deletions tests/fixture/issue-2232/case2/output/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ var resolver = {
var ref;
while(1)switch(_ctx.prev = _ctx.next){
case 0:
var ref1, ref2;
var ref1;
ref1 = _param, ref = ref1.input, toNumber = ref.toNumber, messageBody = ref.messageBody, ref, ref1, all = _objectWithoutProperties(_param.input, [
"toNumber",
"messageBody"
]), ref2 = _param1, dataSources = ref2.dataSources, ref2;
]), dataSources = _param1.dataSources;
case 1:
case "end":
return _ctx.stop();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,7 @@ function _fn5() {
return regeneratorRuntime.wrap(function _callee$(_ctx) {
while(1)switch(_ctx.prev = _ctx.next){
case 0:
var ref;
ref = y, x = ref.x, ref;
x = y.x;
case 1:
case "end":
return _ctx.stop();
Expand Down Expand Up @@ -225,11 +224,10 @@ function _fn9() {
_fn9 = _asyncToGenerator(regeneratorRuntime.mark(function _callee(x) {
var x;
return regeneratorRuntime.wrap(function _callee$(_ctx) {
var ref;
while(1)switch(_ctx.prev = _ctx.next){
case 0:
var ref2;
ref2 = y, ref = ref2.z, x = ref.x, ref, ref2;
var ref;
ref = y, x = ref.z.x, ref;
case 1:
case "end":
return _ctx.stop();
Expand All @@ -248,9 +246,8 @@ function _fn10() {
var ref;
while(1)switch(_ctx.prev = _ctx.next){
case 0:
var ref3;
var ref4;
ref3 = y, ref = y, ref4 = ref, x = ref4.x, ref4, ref3;
var ref2;
ref2 = y, ref = y, x = ref.x, ref2;
case 1:
case "end":
return _ctx.stop();
Expand Down Expand Up @@ -306,8 +303,8 @@ function _fn13() {
return regeneratorRuntime.wrap(function _callee$(_ctx) {
while(1)switch(_ctx.prev = _ctx.next){
case 0:
var ref, ref5;
ref = y, ref5 = ref[0], x = ref5 === void 0 ? y : ref5, ref;
var ref, ref3;
ref = y, ref3 = ref[0], x = ref3 === void 0 ? y : ref3, ref;
case 1:
case "end":
return _ctx.stop();
Expand Down Expand Up @@ -363,8 +360,8 @@ function _fn16() {
return regeneratorRuntime.wrap(function _callee$(_ctx) {
while(1)switch(_ctx.prev = _ctx.next){
case 0:
var ref, ref6;
ref = y, ref6 = ref[0], x = ref6[0], ref6, ref;
var ref, ref4;
ref = y, ref4 = ref[0], x = ref4[0], ref4, ref;
case 1:
case "end":
return _ctx.stop();
Expand All @@ -382,8 +379,8 @@ function _fn17() {
return regeneratorRuntime.wrap(function _callee$(_ctx) {
while(1)switch(_ctx.prev = _ctx.next){
case 0:
var ref, ref7, ref8;
ref = y, ref7 = ref[0], ref8 = ref7 === void 0 ? y : ref7, x = ref8[0], ref8, ref;
var ref, ref5, ref6;
ref = y, ref5 = ref[0], ref6 = ref5 === void 0 ? y : ref5, x = ref6[0], ref6, ref;
case 1:
case "end":
return _ctx.stop();
Expand Down Expand Up @@ -665,8 +662,7 @@ function _fn31() {
return regeneratorRuntime.wrap(function _callee$(_ctx) {
while(1)switch(_ctx.prev = _ctx.next){
case 0:
var ref;
for(ref = y, x = ref.x, ref;;){
for(x = y.x;;){
}
case 1:
case "end":
Expand Down Expand Up @@ -806,8 +802,7 @@ function _fn36() {
_iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
_ctx.prev = 1;
for(_iterator = y[Symbol.iterator](); !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
var ref;
ref = _step.value, x = ref.x, ref;
x = _step.value.x;
}
_ctx.next = 9;
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ var p6 = function(param) {
var a = param.a;
};
var p7 = function(param) {
var _a = param.a, b = _a.b;
var b = param.a.b;
};
var p8 = function(param) {
var _a = param.a, a = _a === void 0 ? 1 : _a;
Expand Down
Loading