Skip to content

Commit

Permalink
fix(tree-shaking): object spread transform should go before deconstru…
Browse files Browse the repository at this point in the history
…cting (#1598)

* fix: 🐛 deconstructing needs object spread transform first

* test: ✅ add guardian syntax
  • Loading branch information
stormslowly authored Sep 14, 2024
1 parent 75118d5 commit 9434d99
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
11 changes: 8 additions & 3 deletions crates/mako/src/plugins/tree_shaking/remove_useless_stmts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use swc_core::ecma::ast::{
Module, ModuleExportName,
};
use swc_core::ecma::transforms::compat::es2015::destructuring;
use swc_core::ecma::visit::{Fold, VisitMut, VisitMutWith, VisitWith};
use swc_core::ecma::transforms::compat::es2018::object_rest_spread;
use swc_core::ecma::visit::{VisitMut, VisitMutWith, VisitWith};

use super::collect_explicit_prop::IdExplicitPropAccessCollector;
use crate::plugins::tree_shaking::module::TreeShakeModule;
Expand Down Expand Up @@ -268,8 +269,12 @@ fn optimize_import_namespace(import_infos: &mut [ImportInfo], module: &mut Modul

if !ids.is_empty() {
let mut v = IdExplicitPropAccessCollector::new(ids);
let destucturing_module = destructuring(Default::default()).fold_module(module.clone());
destucturing_module.visit_with(&mut v);
let mut shadow = module.clone();

shadow.visit_mut_with(&mut object_rest_spread(Default::default()));
shadow.visit_mut_with(&mut destructuring(Default::default()));
shadow.visit_with(&mut v);

let explicit_prop_accessed_ids = v.explicit_accessed_props();

import_infos.iter_mut().for_each(|ii| {
Expand Down
14 changes: 14 additions & 0 deletions e2e/fixtures/tree-shaking.import_namespace/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,17 @@ const { shouldKeep2 } = mod
console.log(mod.shouldKeep1(42));
console.log(shouldKeep2(42))

// Guardian don't remove
let array= [1,2,3,]
console.log(array)
let [first, ...rest] = array
console.log(first,rest)
let copiedArray = [...array]
console.log(copiedArray)

let object = {a: 1,b: 2,c: 3}
console.log(object)
let {a, ...restObject} = object
console.log(a,restObject)
let copiedObject = {...object}
console.log(copiedObject)

0 comments on commit 9434d99

Please sign in to comment.