Skip to content

Commit 04b0f6d

Browse files
authoredJul 14, 2023
feat(es/minifier): Drop recursively used var declaration (#7649)
1 parent 7bf4ab3 commit 04b0f6d

File tree

20 files changed

+34
-64
lines changed

20 files changed

+34
-64
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
11
//// [assignmentCompatWithObjectMembers.ts]
22
var SimpleTypes, ObjectTypes;
33
import "@swc/helpers/_/_class_call_check";
4-
SimpleTypes || (SimpleTypes = {}), function(ObjectTypes) {
5-
var t2, b, a2 = {
6-
foo: a2
7-
}, b2 = {
8-
foo: b2
9-
};
10-
b2 = a2 = b2, a2 = b, a2 = t2;
11-
}(ObjectTypes || (ObjectTypes = {}));
4+
SimpleTypes || (SimpleTypes = {}), ObjectTypes || (ObjectTypes = {});

‎crates/swc/tests/tsc-references/destructuringArrayBindingPatternAndAssignment3.2.minified.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
//// [destructuringArrayBindingPatternAndAssignment3.ts]
22
import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
3-
var tmp2 = [
4-
1
5-
][2], e = void 0 === tmp2 ? e : tmp2;
63
!function(param) {
74
var _param = _sliced_to_array(param, 2);
85
_param[0], _param[1];
96
}([
107
1
118
]), function(param) {
12-
var _param = _sliced_to_array(param, 3), tmp1 = (_param[0], _param[1], _param[2]), e = void 0 === tmp1 ? e : tmp1;
9+
var _param = _sliced_to_array(param, 3);
10+
_param[0], _param[1], _param[2];
1311
}([
1412
1
1513
]), function(param) {
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
//// [destructuringObjectBindingPatternAndAssignment1ES5.ts]
22
(void 0).a1;
33
var tmp = {};
4-
(void 0 === tmp ? {
5-
b21: "string"
6-
} : tmp).b21;
7-
var b52 = b52;
84
function foo1() {
95
return {
106
prop1: 2
117
};
128
}
13-
foo1().prop1, foo1().prop2;
9+
(void 0 === tmp ? {
10+
b21: "string"
11+
} : tmp).b21, foo1().prop1, foo1().prop2;
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
//// [destructuringObjectBindingPatternAndAssignment4.ts]
2-
var _ref = {}, _ref_f = (_ref.a, _ref.b, _ref.c, _ref.d, _ref.e, _ref.f), f = void 0 === _ref_f ? f : _ref_f;
2+
var _ref = {};
3+
_ref.a, _ref.b, _ref.c, _ref.d, _ref.e, _ref.f;
Original file line numberDiff line numberDiff line change
@@ -1,9 +1 @@
11
//// [objectLiteralShorthandProperties.ts]
2-
var x3 = {
3-
a: 0,
4-
b: void 0,
5-
c: void 0,
6-
d: function() {},
7-
x3: x3,
8-
parent: x3
9-
};
Original file line numberDiff line numberDiff line change
@@ -1,9 +1 @@
11
//// [objectLiteralShorthandPropertiesES6.ts]
2-
var x3 = {
3-
a: 0,
4-
b: void 0,
5-
c: void 0,
6-
d () {},
7-
x3,
8-
parent: x3
9-
};
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
//// [recursiveInitializer.ts]
2-
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;
2+
var n1 = n1++;

‎crates/swc/tests/tsc-references/witness.2.minified.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
//// [witness.ts]
22
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
3-
var co2, cnd1, or1, or2, or3, and1, and3, propAcc1, M2, varInit = varInit;
43
!function fnReturn1() {
54
return fnReturn1();
65
}(), function fnReturn2() {
76
return fnReturn2;
8-
}();
9-
var co2 = co2, cnd1 = cnd1 ? 0 : 1, or1 = or1 || "", or2 = or2, or3 = or3 || or3, and1 = and1 && "", and3 = and3 && and3;
10-
!function fnCall() {
7+
}(), function fnCall() {
118
return fnCall();
129
}(), function fn5() {
1310
return new (void 0)(fn5);
1411
}();
15-
var propAcc1 = {
12+
var propAcc1, M2, propAcc1 = {
1613
m: propAcc1.m
1714
};
1815
(M2 || (M2 = {})).x = M2.x, new function C2() {

‎crates/swc_ecma_minifier/src/compress/optimize/inline.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,10 @@ impl Optimizer<'_> {
176176
self.mode.store(ident.to_id(), &*init);
177177
}
178178

179+
if usage.used_recursively {
180+
return;
181+
}
182+
179183
// Caution: for most case mutation of properties are ok, however if
180184
// new variant is added for multi inline, think carefully
181185
if is_inline_enabled
@@ -308,7 +312,7 @@ impl Optimizer<'_> {
308312
&& ref_count == 1
309313
{
310314
match init {
311-
Expr::Fn(FnExpr { function: f, .. }) | Expr::Fn(FnExpr { function: f, .. })
315+
Expr::Fn(FnExpr { function: f, .. })
312316
if matches!(
313317
&**f,
314318
Function { is_async: true, .. }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
let a = () => a

‎crates/swc_ecma_minifier/tests/fixture/issues/7641/1/output.js

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
[](function(){var n=function(){n[b]}});
1+
[](function(){});
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
[](function(){var n=function(){0(function(){return n})}});
1+
[](function(){});
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
[](function(){var n=function(){b(function(){b(n)})}});
1+
[](function(){});
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
[](function(){var n=function(){n.b}});
1+
[](function(){});
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
[](function(){var n;n=function(){b(n)}});
1+
[](function(){});
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
[](function(){var n=function(){b(n)}});
1+
[](function(){});
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,2 @@
1-
function f1() {
2-
var self = {
3-
inner: function() {
4-
return self;
5-
}
6-
};
7-
}
8-
function f2() {
9-
var self = {
10-
inner: self
11-
};
12-
}
1+
function f1() {}
2+
function f2() {}

‎crates/swc_ecma_usage_analyzer/src/analyzer/mod.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use swc_atoms::js_word;
22
use swc_common::{collections::AHashMap, Mark, SyntaxContext};
33
use swc_ecma_ast::*;
4-
use swc_ecma_utils::{find_pat_ids, ExprCtx, IsEmpty, StmtExt};
4+
use swc_ecma_utils::{find_pat_ids, ExprCtx, ExprExt, IsEmpty, StmtExt};
55
use swc_ecma_visit::{noop_visit_type, Visit, VisitWith};
66
use swc_timer::timer;
77

@@ -1261,14 +1261,18 @@ where
12611261
if self.marks.is_some() {
12621262
if let VarDeclarator {
12631263
name: Pat::Ident(id),
1264-
init: Some(..),
1264+
init: Some(init),
12651265
definite: false,
12661266
..
12671267
} = e
12681268
{
12691269
let id = id.to_id();
1270-
self.used_recursively
1271-
.insert(id.clone(), RecursiveUsage::Var { can_ignore: false });
1270+
self.used_recursively.insert(
1271+
id.clone(),
1272+
RecursiveUsage::Var {
1273+
can_ignore: !init.may_have_side_effects(&self.expr_ctx),
1274+
},
1275+
);
12721276
e.init.visit_with(&mut *self.with_ctx(ctx));
12731277
self.used_recursively.remove(&id);
12741278
return;

‎crates/swc_html_minifier/tests/fixture/element/script/output.min.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
alert('test')
5555
</script>
5656
</math>
57-
<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>
57+
<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>
5858
<div>test</div>
5959

6060
<div>topLevel - script</div>

0 commit comments

Comments
 (0)
Please sign in to comment.