diff --git a/crates/mako/src/build/transform.rs b/crates/mako/src/build/transform.rs index a406f7c99..404d4d944 100644 --- a/crates/mako/src/build/transform.rs +++ b/crates/mako/src/build/transform.rs @@ -37,7 +37,6 @@ use crate::visitors::css_px2rem::Px2Rem; use crate::visitors::default_export_namer::DefaultExportNamer; use crate::visitors::dynamic_import_to_require::DynamicImportToRequire; use crate::visitors::env_replacer::{build_env_map, EnvReplacer}; -use crate::visitors::fix_helper_inject_position::FixHelperInjectPosition; use crate::visitors::fix_symbol_conflict::FixSymbolConflict; use crate::visitors::import_meta_env_replacer::ImportMetaEnvReplacer; use crate::visitors::import_template_to_string_literal::ImportTemplateToStringLiteral; @@ -101,10 +100,6 @@ impl Transform { top_level_mark, is_ts || is_tsx, )), - // fix helper inject position - // should be removed after upgrade to latest swc - // ref: https://github.com/umijs/mako/issues/1193 - Box::new(FixHelperInjectPosition::new()), Box::new(FixSymbolConflict::new(top_level_mark)), Box::new(NewUrlAssets { context: context.clone(), diff --git a/crates/mako/src/visitors.rs b/crates/mako/src/visitors.rs index 0d40cb6a4..fd31b158b 100644 --- a/crates/mako/src/visitors.rs +++ b/crates/mako/src/visitors.rs @@ -13,7 +13,6 @@ pub(crate) mod dep_replacer; pub(crate) mod dynamic_import; pub(crate) mod dynamic_import_to_require; pub(crate) mod env_replacer; -pub(crate) mod fix_helper_inject_position; pub(crate) mod fix_symbol_conflict; pub(crate) mod import_meta_env_replacer; pub(crate) mod import_template_to_string_literal; diff --git a/crates/mako/src/visitors/fix_helper_inject_position.rs b/crates/mako/src/visitors/fix_helper_inject_position.rs deleted file mode 100644 index c93ba2b0d..000000000 --- a/crates/mako/src/visitors/fix_helper_inject_position.rs +++ /dev/null @@ -1,200 +0,0 @@ -use swc_core::common::DUMMY_SP; -use swc_core::ecma::ast::{ - ArrowExpr, Decl, ExportDecl, ExportNamedSpecifier, ExportSpecifier, Expr, FnDecl, Function, - Ident, ModuleDecl, ModuleItem, NamedExport, Pat, Stmt, VarDecl, -}; -use swc_core::ecma::visit::{VisitMut, VisitMutWith}; - -pub struct FixHelperInjectPosition { - pub exports: Vec, -} - -impl FixHelperInjectPosition { - pub fn new() -> Self { - Self { exports: vec![] } - } -} - -impl VisitMut for FixHelperInjectPosition { - fn visit_mut_module_item(&mut self, n: &mut ModuleItem) { - if let ModuleItem::ModuleDecl(ModuleDecl::ExportDecl(ExportDecl { decl, .. })) = n { - let mut has_array_param = false; - let mut export_names = vec![]; - // export function ident ([a]){} - if let Decl::Fn(FnDecl { - ident, - function: box Function { params, .. }, - .. - }) = decl - { - has_array_param = params - .iter() - .any(|param| matches!(param.pat, Pat::Array(_))); - if has_array_param { - export_names.push(ident.clone()); - } - } - if let Decl::Var(box VarDecl { decls, .. }) = &decl { - decls.iter().for_each(|decl| { - let name = match &decl.name { - Pat::Ident(ident) => Some(ident.id.clone()), - _ => None, - }; - // why don't handle Expr::Fn(FnExpr {}) ? - // export const x = function ([ a, b ]) { return a + b; }; works correctly - // we don't need to fix it - if let Some(box Expr::Arrow(ArrowExpr { params, .. })) = &decl.init { - has_array_param = params.iter().any(|param| matches!(param, Pat::Array(_))); - if has_array_param && let Some(name) = name { - export_names.push(name); - } - } - }); - } - if has_array_param { - *n = ModuleItem::Stmt(Stmt::Decl(decl.clone())); - self.exports.extend(export_names); - } - } - n.visit_mut_children_with(self); - } - - fn visit_mut_module_items(&mut self, n: &mut Vec) { - n.visit_mut_children_with(self); - - if !self.exports.is_empty() { - let mut new_items = vec![]; - for export in self.exports.iter() { - new_items.push(ModuleItem::ModuleDecl(ModuleDecl::ExportNamed( - NamedExport { - span: DUMMY_SP, - specifiers: vec![ExportSpecifier::Named(ExportNamedSpecifier { - span: DUMMY_SP, - orig: export.clone().into(), - exported: None, - is_type_only: false, - })], - src: None, - type_only: false, - with: None, - }, - ))); - } - n.extend(new_items); - } - } -} - -#[cfg(test)] -mod tests { - use std::collections::HashMap; - - use swc_core::common::GLOBALS; - use swc_core::ecma::preset_env::{self as swc_preset_env}; - use swc_core::ecma::transforms::base::feature::FeatureFlag; - use swc_core::ecma::transforms::base::helpers::{Helpers, HELPERS}; - use swc_core::ecma::transforms::base::Assumptions; - use swc_core::ecma::transforms::proposal::decorators; - use swc_core::ecma::visit::{Fold, VisitMut, VisitMutWith}; - - use super::FixHelperInjectPosition; - use crate::ast::tests::TestUtils; - use crate::build::targets::swc_preset_env_targets_from_map; - - #[test] - fn test_normal() { - assert_eq!( - run(r#" -export function foo([ a, b ]) { - return a + b; -} - "#), - r#" -function foo(ref) { - let _ref = _sliced_to_array(ref, 2), a = _ref[0], b = _ref[1]; - return a + b; -} -export { foo }; - "# - .trim() - ); - assert_eq!( - run(r#" -export function foo(c, [ a, b ]) { - return a + b + c; -} - "#), - r#" -function foo(c, ref) { - let _ref = _sliced_to_array(ref, 2), a = _ref[0], b = _ref[1]; - return a + b + c; -} -export { foo }; - "# - .trim() - ); - } - - #[test] - fn test_export_const_arrow() { - assert_eq!( - run(r#" -export const foo = (x, [ a, b ]) => a + b + x; - "#), - r#" -const foo = (x, ref)=>{ - let _ref = _sliced_to_array(ref, 2), a = _ref[0], b = _ref[1]; - return a + b + x; -}; -export { foo }; - "# - .trim() - ); - } - - fn run(js_code: &str) -> String { - let mut test_utils = TestUtils::gen_js_ast(js_code); - let ast = test_utils.ast.js_mut(); - let unresolved_mark = ast.unresolved_mark; - GLOBALS.set(&test_utils.context.meta.script.globals, || { - HELPERS.set(&Helpers::new(true), || { - let mut v = FixHelperInjectPosition { exports: vec![] }; - ast.ast.visit_mut_with(&mut v); - - // preset_env - let mut folders: Vec> = vec![]; - folders.push(Box::new(decorators(decorators::Config { - legacy: true, - emit_metadata: false, - ..Default::default() - }))); - let origin_comments = test_utils - .context - .meta - .script - .origin_comments - .read() - .unwrap(); - let comments = origin_comments.get_swc_comments().clone(); - let mut targets = HashMap::new(); - targets.insert("chrome".to_string(), 50.0); - folders.push(Box::new(swc_preset_env::preset_env( - unresolved_mark, - Some(comments), - swc_preset_env::Config { - mode: Some(swc_preset_env::Mode::Entry), - targets: Some(swc_preset_env_targets_from_map(targets)), - ..Default::default() - }, - Assumptions::default(), - &mut FeatureFlag::default(), - ))); - let mut visitors: Vec> = vec![]; - let context = test_utils.context.clone(); - ast.transform(&mut visitors, &mut folders, false, context) - .unwrap(); - }); - }); - test_utils.js_ast_to_code() - } -}