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

fix(es/minifier): Do not inline into a template literal with sequential inliner #7971

Merged
merged 11 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from 10 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
72 changes: 72 additions & 0 deletions crates/swc/tests/fixture/issues-7xxx/7969/input/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
{
"jsc": {
"parser": {
"syntax": "ecmascript",
"jsx": false
},
"target": "es2015",
"loose": false,
"minify": {
"compress": {
"arguments": false,
"arrows": true,
"booleans": true,
"booleans_as_integers": false,
"collapse_vars": true,
"comparisons": true,
"computed_props": true,
"conditionals": true,
"dead_code": true,
"directives": true,
"drop_console": false,
"drop_debugger": true,
"evaluate": true,
"expression": false,
"hoist_funs": false,
"hoist_props": true,
"hoist_vars": false,
"if_return": true,
"join_vars": true,
"keep_classnames": false,
"keep_fargs": true,
"keep_fnames": false,
"keep_infinity": false,
"loops": true,
"negate_iife": true,
"properties": true,
"reduce_funcs": false,
"reduce_vars": false,
"side_effects": true,
"switches": true,
"typeofs": true,
"unsafe": false,
"unsafe_arrows": false,
"unsafe_comps": false,
"unsafe_Function": false,
"unsafe_math": false,
"unsafe_symbols": false,
"unsafe_methods": false,
"unsafe_proto": false,
"unsafe_regexp": false,
"unsafe_undefined": false,
"unused": true,
"const_to_let": true,
"pristine_globals": true,
"passes": 1
},
"mangle": {
"toplevel": false,
"keep_classnames": false,
"keep_fnames": false,
"keep_private_props": false,
"ie8": false,
"safari10": false
}
}
},
"module": {
"type": "es6"
},
"minify": true,
"isModule": true
}
14 changes: 14 additions & 0 deletions crates/swc/tests/fixture/issues-7xxx/7969/input/1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
let a = 0;
function f(arr) {
let b = a;
return `${arr.map((child) => {
a = b + "str";
})}`;
}

function g(arr) {
return f(arr);
}
globalThis.g = g;
g([1, 2, 3])
console.log(a);
1 change: 1 addition & 0 deletions crates/swc/tests/fixture/issues-7xxx/7969/output/1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let a=0;function g(l){var o;let t;return o=l,t=a,`${o.map(l=>{a=t+"str"})}`}globalThis.g=g,g([1,2,3]),console.log(a);
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//// [taggedTemplateStringsTypeArgumentInferenceES6.ts]
// Generic tag with one parameter
var anyVar;
// Generic tag with argument of function type whose parameter is not of type parameter type but body/return type uses type parameter
function someGenerics3(strs, producer) {}
// 2 parameter generic tag with argument 1 of type parameter type and argument 2 of function type whose parameter is of type parameter type
Expand All @@ -11,14 +10,16 @@ function someGenerics5(strs, n, f) {}
function someGenerics6(strs, a, b, c) {}
// Generic tag with multiple arguments of function types that each have parameters of different generic type
function someGenerics7(strs, a, b, c) {}
someGenerics3`${()=>''}`, someGenerics3`${()=>void 0}`, someGenerics3`${()=>3}`, someGenerics4`${4}${()=>null}`, someGenerics4`${''}${()=>3}`, someGenerics4`${null}${null}`, someGenerics5`${4} ${()=>null}`, someGenerics5`${''}${()=>3}`, someGenerics5`${null}${null}`, someGenerics6`${(n)=>n}${(n)=>n}${(n)=>n}`, someGenerics6`${(n)=>n}${(n)=>n}${(n)=>n}`, someGenerics6`${(n)=>n}${(n)=>n}${(n)=>n}`, someGenerics7`${(n)=>n}${(n)=>n}${(n)=>n}`, someGenerics7`${(n)=>n}${(n)=>n}${(n)=>n}`, someGenerics7`${(n)=>n}${(n)=>n}${(n)=>n}`;
var anyVar, x = // Generic tag with argument of generic function type
(function(strs, n) {
return n;
})`${someGenerics7}`;
// Generic tag with multiple parameters of generic type passed arguments with no best common type
function someGenerics9(strs, a, b, c) {
return null;
}
someGenerics3`${()=>''}`, someGenerics3`${()=>void 0}`, someGenerics3`${()=>3}`, someGenerics4`${4}${()=>null}`, someGenerics4`${''}${()=>3}`, someGenerics4`${null}${null}`, someGenerics5`${4} ${()=>null}`, someGenerics5`${''}${()=>3}`, someGenerics5`${null}${null}`, someGenerics6`${(n)=>n}${(n)=>n}${(n)=>n}`, someGenerics6`${(n)=>n}${(n)=>n}${(n)=>n}`, someGenerics6`${(n)=>n}${(n)=>n}${(n)=>n}`, someGenerics7`${(n)=>n}${(n)=>n}${(n)=>n}`, someGenerics7`${(n)=>n}${(n)=>n}${(n)=>n}`, someGenerics7`${(n)=>n}${(n)=>n}${(n)=>n}`, // Generic tag with argument of generic function type
(function(strs, n) {
return n;
})`${someGenerics7}``${null}${null}${null}`, someGenerics9`${''}${0}${[]}`, someGenerics9`${void 0}${{
x`${null}${null}${null}`, someGenerics9`${''}${0}${[]}`, someGenerics9`${void 0}${{
x: 6,
z: new Date()
}}${{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
//// [taggedTemplateStringsWithManyCallAndMemberExpressionsES6.ts]
new new new (void 0)`abc${0}def`.member("hello")(42);
var f;
new new new f`abc${0}def`.member("hello")(42);
15 changes: 11 additions & 4 deletions crates/swc_ecma_minifier/src/compress/optimize/sequences.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1207,6 +1207,13 @@ impl Optimizer<'_> {

Expr::Yield(..) | Expr::Await(..) => false,

Expr::Tpl(t) => t.exprs.iter().all(|e| self.is_skippable_for_seq(a, e)),

Expr::TaggedTpl(t) => {
self.is_skippable_for_seq(a, &t.tag)
&& t.tpl.exprs.iter().all(|e| self.is_skippable_for_seq(a, e))
}

Expr::Unary(UnaryExpr {
op: op!("!") | op!("void") | op!("typeof") | op!(unary, "-") | op!(unary, "+"),
arg,
Expand Down Expand Up @@ -1378,14 +1385,12 @@ impl Optimizer<'_> {
exprs.iter().all(|e| self.is_skippable_for_seq(a, e))
}

Expr::TaggedTpl(..) | Expr::New(..) => {
Expr::New(..) => {
// TODO(kdy1): We can optimize some known calls.

false
}

Expr::Tpl(Tpl { exprs, .. }) => exprs.iter().all(|e| self.is_skippable_for_seq(a, e)),

// Expressions without any effects
Expr::This(_)
| Expr::Fn(_)
Expand Down Expand Up @@ -1523,7 +1528,9 @@ impl Optimizer<'_> {
| Expr::Class(..)
| Expr::Lit(..)
| Expr::Await(..)
| Expr::Yield(..) => true,
| Expr::Yield(..)
| Expr::Tpl(..)
| Expr::TaggedTpl(..) => true,
Expr::Unary(UnaryExpr {
op: op!("delete"), ..
}) => true,
Expand Down
46 changes: 46 additions & 0 deletions crates/swc_ecma_minifier/tests/fixture/issues/7969/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"arguments": false,
"arrows": true,
"booleans": true,
"booleans_as_integers": false,
"collapse_vars": true,
"comparisons": true,
"computed_props": true,
"conditionals": true,
"dead_code": true,
"directives": true,
"drop_console": false,
"drop_debugger": true,
"evaluate": true,
"expression": false,
"hoist_funs": false,
"hoist_props": true,
"hoist_vars": false,
"if_return": true,
"join_vars": true,
"keep_classnames": false,
"keep_fargs": true,
"keep_fnames": false,
"keep_infinity": false,
"loops": true,
"negate_iife": true,
"properties": true,
"reduce_funcs": false,
"reduce_vars": false,
"side_effects": true,
"switches": true,
"typeofs": true,
"unsafe": false,
"unsafe_arrows": false,
"unsafe_comps": false,
"unsafe_Function": false,
"unsafe_math": false,
"unsafe_symbols": false,
"unsafe_methods": false,
"unsafe_proto": false,
"unsafe_regexp": false,
"unsafe_undefined": false,
"unused": true,
"const_to_let": true,
"pristine_globals": true
}
14 changes: 14 additions & 0 deletions crates/swc_ecma_minifier/tests/fixture/issues/7969/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
let a = 0;
function f(arr) {
let b = a;
return `${arr.map((child) => {
a = b + "str";
})}`;
}

function g(arr) {
return f(arr);
}
globalThis.g = g;
g([1, 2, 3])
console.log(a);
17 changes: 17 additions & 0 deletions crates/swc_ecma_minifier/tests/fixture/issues/7969/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
let a = 0;
function f(arr) {
let b = a;
return `${arr.map((child)=>{
a = b + "str";
})}`;
}
function g(arr) {
return f(arr);
}
globalThis.g = g;
g([
1,
2,
3
]);
console.log(a);
Original file line number Diff line number Diff line change
Expand Up @@ -19083,8 +19083,8 @@
if (Array.isArray(value1)) {
if (0 === value1.length) return '[]';
if (maximumDepth1 < stack1.length + 1) return '"[Array]"';
stack1.push(value1);
let res1 = `\n${indentation1 += spacer1}`;
stack1.push(value1), indentation1 += spacer1;
let res1 = `\n${indentation1}`;
const join1 = `,\n${indentation1}`, maximumValuesToStringify1 = Math.min(value1.length, maximumBreadth1);
let i2 = 0;
for(; i2 < maximumValuesToStringify1 - 1; i2++){
Expand Down