Skip to content

Commit

Permalink
fix(es/minifier): Don't skip shorthand properties from sequential inl…
Browse files Browse the repository at this point in the history
…iner (#6918)

**Related issue:**

 - Closes #6914.
  • Loading branch information
kdy1 committed Feb 9, 2023
1 parent 932d5a5 commit 725d3fb
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 6 deletions.
@@ -1,8 +1,8 @@
//// [objectLiteralShorthandPropertiesES6.ts]
var b, c, x3 = {
var x3 = {
a: 0,
b,
c,
b: void 0,
c: void 0,
d () {},
x3,
parent: x3
Expand Down
22 changes: 20 additions & 2 deletions crates/swc_ecma_minifier/src/compress/optimize/sequences.rs
@@ -1,7 +1,7 @@
use std::mem::take;

use swc_atoms::js_word;
use swc_common::{util::take::Take, Spanned, DUMMY_SP};
use swc_common::{util::take::Take, Spanned, SyntaxContext, DUMMY_SP};
use swc_ecma_ast::*;
use swc_ecma_usage_analyzer::{
alias::{collect_infects_from, AccessKind, AliasConfig},
Expand Down Expand Up @@ -1847,7 +1847,25 @@ where
PropOrSpread::Prop(prop) => {
// Inline into key
let key = match &mut **prop {
Prop::Shorthand(_) => continue,
Prop::Shorthand(shorthand) => {
// We can't ignore shorthand properties
//
// https://github.com/swc-project/swc/issues/6914

let mut new_b = Box::new(Expr::Ident(shorthand.clone()));
if self.merge_sequential_expr(a, &mut new_b)? {
*prop = Box::new(Prop::KeyValue(KeyValueProp {
key: Ident::new(
shorthand.sym.clone(),
shorthand.span.with_ctxt(SyntaxContext::empty()),
)
.into(),
value: new_b,
}));
}

continue;
}
Prop::KeyValue(prop) => Some(&mut prop.key),
Prop::Assign(_) => None,
Prop::Getter(prop) => Some(&mut prop.key),
Expand Down
1 change: 0 additions & 1 deletion crates/swc_ecma_minifier/tests/TODO.txt
Expand Up @@ -70,7 +70,6 @@ collapse_vars/switch_case_1/input.js
collapse_vars/toplevel_single_reference/input.js
collapse_vars/undeclared/input.js
collapse_vars/unused_orig/input.js
conditionals/equality_conditionals_false/input.js
conditionals/ifs_5/input.js
conditionals/ifs_6/input.js
conditionals/ifs_same_consequent/input.js
Expand Down
110 changes: 110 additions & 0 deletions crates/swc_ecma_minifier/tests/exec.rs
Expand Up @@ -10821,3 +10821,113 @@ fn issue_6903_3() {
false,
);
}

#[test]
fn issue_6914_1() {
run_default_exec_test(
r###"
console.log(doSomething())
function doSomething() {
return fabricateEvent()
}
function fabricateEvent() {
let def = createEventDef()
return {
def,
ui: compileEventUi(def),
}
}
function compileEventUi(def) {
let uis = []
uis.push(def.ui)
return uis
}
function createEventDef() {
return {
id: 'fakeId',
ui: 'something'
}
}
"###,
);
}

#[test]
fn issue_6914_2() {
run_exec_test(
r###"
console.log(doSomething())
function doSomething() {
return fabricateEvent()
}
function fabricateEvent() {
let def = createEventDef()
return {
def,
ui: compileEventUi(def),
}
}
function compileEventUi(def) {
let uis = []
uis.push(def.ui)
return uis
}
function createEventDef() {
return {
id: 'fakeId',
ui: 'something'
}
}
"###,
r###"
{
"inline": true,
"toplevel": true
}
"###,
false,
);
}

#[test]
fn issue_6914_3() {
run_exec_test(
r###"
console.log(function() {
return function() {
let def = function() {
return {
id: 'fakeId',
ui: 'something'
};
}();
return {
def,
ui: function(def) {
let uis = [];
uis.push(def.ui);
return uis;
}(def)
};
}();
}());
"###,
r###"
{
"inline": true,
"toplevel": true
}
"###,
false,
);
}
1 change: 1 addition & 0 deletions crates/swc_ecma_minifier/tests/passing.txt
Expand Up @@ -288,6 +288,7 @@ conditionals/cond_9/input.js
conditionals/condition_symbol_matches_consequent/input.js
conditionals/delete_conditional_1/input.js
conditionals/delete_conditional_2/input.js
conditionals/equality_conditionals_false/input.js
conditionals/equality_conditionals_true/input.js
conditionals/ifs_1/input.js
conditionals/ifs_2/input.js
Expand Down

0 comments on commit 725d3fb

Please sign in to comment.