Skip to content

Commit

Permalink
feat(es/minifier): Drop recursively used var declaration (#7649)
Browse files Browse the repository at this point in the history
  • Loading branch information
Austaras committed Jul 14, 2023
1 parent 7bf4ab3 commit 04b0f6d
Show file tree
Hide file tree
Showing 20 changed files with 34 additions and 64 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
//// [assignmentCompatWithObjectMembers.ts]
var SimpleTypes, ObjectTypes;
import "@swc/helpers/_/_class_call_check";
SimpleTypes || (SimpleTypes = {}), function(ObjectTypes) {
var t2, b, a2 = {
foo: a2
}, b2 = {
foo: b2
};
b2 = a2 = b2, a2 = b, a2 = t2;
}(ObjectTypes || (ObjectTypes = {}));
SimpleTypes || (SimpleTypes = {}), ObjectTypes || (ObjectTypes = {});
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
//// [destructuringArrayBindingPatternAndAssignment3.ts]
import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
var tmp2 = [
1
][2], e = void 0 === tmp2 ? e : tmp2;
!function(param) {
var _param = _sliced_to_array(param, 2);
_param[0], _param[1];
}([
1
]), function(param) {
var _param = _sliced_to_array(param, 3), tmp1 = (_param[0], _param[1], _param[2]), e = void 0 === tmp1 ? e : tmp1;
var _param = _sliced_to_array(param, 3);
_param[0], _param[1], _param[2];
}([
1
]), function(param) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
//// [destructuringObjectBindingPatternAndAssignment1ES5.ts]
(void 0).a1;
var tmp = {};
(void 0 === tmp ? {
b21: "string"
} : tmp).b21;
var b52 = b52;
function foo1() {
return {
prop1: 2
};
}
foo1().prop1, foo1().prop2;
(void 0 === tmp ? {
b21: "string"
} : tmp).b21, foo1().prop1, foo1().prop2;
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
//// [destructuringObjectBindingPatternAndAssignment4.ts]
var _ref = {}, _ref_f = (_ref.a, _ref.b, _ref.c, _ref.d, _ref.e, _ref.f), f = void 0 === _ref_f ? f : _ref_f;
var _ref = {};
_ref.a, _ref.b, _ref.c, _ref.d, _ref.e, _ref.f;
Original file line number Diff line number Diff line change
@@ -1,9 +1 @@
//// [objectLiteralShorthandProperties.ts]
var x3 = {
a: 0,
b: void 0,
c: void 0,
d: function() {},
x3: x3,
parent: x3
};
Original file line number Diff line number Diff line change
@@ -1,9 +1 @@
//// [objectLiteralShorthandPropertiesES6.ts]
var x3 = {
a: 0,
b: void 0,
c: void 0,
d () {},
x3,
parent: x3
};
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
//// [recursiveInitializer.ts]
var n1 = n1++, n2 = n2 + n2, n3 = n3 + n3, s1 = s1 + "", s2 = s2 + s2, s3 = s3 + s3, s4 = "" + s4, b1 = !b1, b2 = !!b2, b3 = !b3 || b3, b4 = !b4 && b4;
var n1 = n1++;
7 changes: 2 additions & 5 deletions crates/swc/tests/tsc-references/witness.2.minified.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
//// [witness.ts]
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
var co2, cnd1, or1, or2, or3, and1, and3, propAcc1, M2, varInit = varInit;
!function fnReturn1() {
return fnReturn1();
}(), function fnReturn2() {
return fnReturn2;
}();
var co2 = co2, cnd1 = cnd1 ? 0 : 1, or1 = or1 || "", or2 = or2, or3 = or3 || or3, and1 = and1 && "", and3 = and3 && and3;
!function fnCall() {
}(), function fnCall() {
return fnCall();
}(), function fn5() {
return new (void 0)(fn5);
}();
var propAcc1 = {
var propAcc1, M2, propAcc1 = {
m: propAcc1.m
};
(M2 || (M2 = {})).x = M2.x, new function C2() {
Expand Down
6 changes: 5 additions & 1 deletion crates/swc_ecma_minifier/src/compress/optimize/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ impl Optimizer<'_> {
self.mode.store(ident.to_id(), &*init);
}

if usage.used_recursively {
return;
}

// Caution: for most case mutation of properties are ok, however if
// new variant is added for multi inline, think carefully
if is_inline_enabled
Expand Down Expand Up @@ -308,7 +312,7 @@ impl Optimizer<'_> {
&& ref_count == 1
{
match init {
Expr::Fn(FnExpr { function: f, .. }) | Expr::Fn(FnExpr { function: f, .. })
Expr::Fn(FnExpr { function: f, .. })
if matches!(
&**f,
Function { is_async: true, .. }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let a = () => a
Empty file.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[](function(){var n=function(){n[b]}});
[](function(){});
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[](function(){var n=function(){0(function(){return n})}});
[](function(){});
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[](function(){var n=function(){b(function(){b(n)})}});
[](function(){});
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[](function(){var n=function(){n.b}});
[](function(){});
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[](function(){var n;n=function(){b(n)}});
[](function(){});
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[](function(){var n=function(){b(n)}});
[](function(){});
Original file line number Diff line number Diff line change
@@ -1,12 +1,2 @@
function f1() {
var self = {
inner: function() {
return self;
}
};
}
function f2() {
var self = {
inner: self
};
}
function f1() {}
function f2() {}
12 changes: 8 additions & 4 deletions crates/swc_ecma_usage_analyzer/src/analyzer/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use swc_atoms::js_word;
use swc_common::{collections::AHashMap, Mark, SyntaxContext};
use swc_ecma_ast::*;
use swc_ecma_utils::{find_pat_ids, ExprCtx, IsEmpty, StmtExt};
use swc_ecma_utils::{find_pat_ids, ExprCtx, ExprExt, IsEmpty, StmtExt};
use swc_ecma_visit::{noop_visit_type, Visit, VisitWith};
use swc_timer::timer;

Expand Down Expand Up @@ -1261,14 +1261,18 @@ where
if self.marks.is_some() {
if let VarDeclarator {
name: Pat::Ident(id),
init: Some(..),
init: Some(init),
definite: false,
..
} = e
{
let id = id.to_id();
self.used_recursively
.insert(id.clone(), RecursiveUsage::Var { can_ignore: false });
self.used_recursively.insert(
id.clone(),
RecursiveUsage::Var {
can_ignore: !init.may_have_side_effects(&self.expr_ctx),
},
);
e.init.visit_with(&mut *self.with_ctx(ctx));
self.used_recursively.remove(&id);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
alert('test')
</script>
</math>
<script blocking="a render">console.log("block")</script><script>!function(t){var t="test"+Math.random()+t;alert("1 "+(2+Math.random())),console.log(Math.random())}("test"),window.jQuery||document.write('<script src="jquery.js"><\/script>')</script>
<script blocking="a render">console.log("block")</script><script>!function(r){var r=Math.random();alert("1 "+(2+Math.random())),console.log(Math.random())}(0),window.jQuery||document.write('<script src="jquery.js"><\/script>')</script>
<div>test</div>

<div>topLevel - script</div>
Expand Down

0 comments on commit 04b0f6d

Please sign in to comment.