Skip to content

Commit 725d3fb

Browse files
authoredFeb 9, 2023
fix(es/minifier): Don't skip shorthand properties from sequential inliner (#6918)
**Related issue:** - Closes #6914.
1 parent 932d5a5 commit 725d3fb

File tree

5 files changed

+134
-6
lines changed

5 files changed

+134
-6
lines changed
 

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//// [objectLiteralShorthandPropertiesES6.ts]
2-
var b, c, x3 = {
2+
var x3 = {
33
a: 0,
4-
b,
5-
c,
4+
b: void 0,
5+
c: void 0,
66
d () {},
77
x3,
88
parent: x3

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

+20-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::mem::take;
22

33
use swc_atoms::js_word;
4-
use swc_common::{util::take::Take, Spanned, DUMMY_SP};
4+
use swc_common::{util::take::Take, Spanned, SyntaxContext, DUMMY_SP};
55
use swc_ecma_ast::*;
66
use swc_ecma_usage_analyzer::{
77
alias::{collect_infects_from, AccessKind, AliasConfig},
@@ -1847,7 +1847,25 @@ where
18471847
PropOrSpread::Prop(prop) => {
18481848
// Inline into key
18491849
let key = match &mut **prop {
1850-
Prop::Shorthand(_) => continue,
1850+
Prop::Shorthand(shorthand) => {
1851+
// We can't ignore shorthand properties
1852+
//
1853+
// https://github.com/swc-project/swc/issues/6914
1854+
1855+
let mut new_b = Box::new(Expr::Ident(shorthand.clone()));
1856+
if self.merge_sequential_expr(a, &mut new_b)? {
1857+
*prop = Box::new(Prop::KeyValue(KeyValueProp {
1858+
key: Ident::new(
1859+
shorthand.sym.clone(),
1860+
shorthand.span.with_ctxt(SyntaxContext::empty()),
1861+
)
1862+
.into(),
1863+
value: new_b,
1864+
}));
1865+
}
1866+
1867+
continue;
1868+
}
18511869
Prop::KeyValue(prop) => Some(&mut prop.key),
18521870
Prop::Assign(_) => None,
18531871
Prop::Getter(prop) => Some(&mut prop.key),

‎crates/swc_ecma_minifier/tests/TODO.txt

-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ collapse_vars/switch_case_1/input.js
7070
collapse_vars/toplevel_single_reference/input.js
7171
collapse_vars/undeclared/input.js
7272
collapse_vars/unused_orig/input.js
73-
conditionals/equality_conditionals_false/input.js
7473
conditionals/ifs_5/input.js
7574
conditionals/ifs_6/input.js
7675
conditionals/ifs_same_consequent/input.js

‎crates/swc_ecma_minifier/tests/exec.rs

+110
Original file line numberDiff line numberDiff line change
@@ -10821,3 +10821,113 @@ fn issue_6903_3() {
1082110821
false,
1082210822
);
1082310823
}
10824+
10825+
#[test]
10826+
fn issue_6914_1() {
10827+
run_default_exec_test(
10828+
r###"
10829+
console.log(doSomething())
10830+
10831+
function doSomething() {
10832+
return fabricateEvent()
10833+
}
10834+
10835+
function fabricateEvent() {
10836+
let def = createEventDef()
10837+
10838+
return {
10839+
def,
10840+
ui: compileEventUi(def),
10841+
}
10842+
}
10843+
10844+
function compileEventUi(def) {
10845+
let uis = []
10846+
uis.push(def.ui)
10847+
return uis
10848+
}
10849+
10850+
function createEventDef() {
10851+
return {
10852+
id: 'fakeId',
10853+
ui: 'something'
10854+
}
10855+
}
10856+
"###,
10857+
);
10858+
}
10859+
10860+
#[test]
10861+
fn issue_6914_2() {
10862+
run_exec_test(
10863+
r###"
10864+
console.log(doSomething())
10865+
10866+
function doSomething() {
10867+
return fabricateEvent()
10868+
}
10869+
10870+
function fabricateEvent() {
10871+
let def = createEventDef()
10872+
10873+
return {
10874+
def,
10875+
ui: compileEventUi(def),
10876+
}
10877+
}
10878+
10879+
function compileEventUi(def) {
10880+
let uis = []
10881+
uis.push(def.ui)
10882+
return uis
10883+
}
10884+
10885+
function createEventDef() {
10886+
return {
10887+
id: 'fakeId',
10888+
ui: 'something'
10889+
}
10890+
}
10891+
"###,
10892+
r###"
10893+
{
10894+
"inline": true,
10895+
"toplevel": true
10896+
}
10897+
"###,
10898+
false,
10899+
);
10900+
}
10901+
10902+
#[test]
10903+
fn issue_6914_3() {
10904+
run_exec_test(
10905+
r###"
10906+
console.log(function() {
10907+
return function() {
10908+
let def = function() {
10909+
return {
10910+
id: 'fakeId',
10911+
ui: 'something'
10912+
};
10913+
}();
10914+
return {
10915+
def,
10916+
ui: function(def) {
10917+
let uis = [];
10918+
uis.push(def.ui);
10919+
return uis;
10920+
}(def)
10921+
};
10922+
}();
10923+
}());
10924+
"###,
10925+
r###"
10926+
{
10927+
"inline": true,
10928+
"toplevel": true
10929+
}
10930+
"###,
10931+
false,
10932+
);
10933+
}

‎crates/swc_ecma_minifier/tests/passing.txt

+1
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ conditionals/cond_9/input.js
288288
conditionals/condition_symbol_matches_consequent/input.js
289289
conditionals/delete_conditional_1/input.js
290290
conditionals/delete_conditional_2/input.js
291+
conditionals/equality_conditionals_false/input.js
291292
conditionals/equality_conditionals_true/input.js
292293
conditionals/ifs_1/input.js
293294
conditionals/ifs_2/input.js

0 commit comments

Comments
 (0)
Please sign in to comment.