From 680d6ef1dae5f582298584ecf1c76e8dd3bf116d Mon Sep 17 00:00:00 2001 From: mutefish0 <648262030@qq.com> Date: Tue, 6 Aug 2024 13:51:24 +0800 Subject: [PATCH 1/2] enhance dynamic import with template argument --- src/dep.rs | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 58 insertions(+), 2 deletions(-) diff --git a/src/dep.rs b/src/dep.rs index e4cc8ab..2e47763 100644 --- a/src/dep.rs +++ b/src/dep.rs @@ -6,6 +6,7 @@ use ast::BinExpr; use ast::Module; use serde::Deserialize; use serde::Serialize; +use swc_ecma_ast::{Ident, IdentName, MemberExpr, MemberProp}; use crate::swc::ast; use crate::swc::ast::Callee; @@ -161,6 +162,8 @@ pub enum DynamicArgument { #[derive(Clone, Debug, Eq, PartialEq)] pub enum DynamicTemplatePart { String(Atom), + /// An member expression that may be analyzed. + MemberExpr(Atom, Atom, Atom), /// An expression that could not be analyzed. Expr, } @@ -304,8 +307,39 @@ impl<'a> Visit for DependencyCollector<'a> { if cooked.len() > 0 { parts.push(DynamicTemplatePart::String(cooked.clone())); } - if tpl.exprs.get(i).is_some() { - parts.push(DynamicTemplatePart::Expr); + + let mut is_member_expr = false; + + if let Some(expr) = tpl.exprs.get(i) { + match **expr { + Expr::Member(MemberExpr { + prop: MemberProp::Ident(IdentName { sym: ref sym2, .. }), + ref obj, + .. + }) => match **obj { + Expr::Member(MemberExpr { + prop: MemberProp::Ident(IdentName { sym: ref sym1, .. }), + obj: ref obj1, + .. + }) => match **obj1 { + Expr::Ident(Ident { ref sym, .. }) => { + parts.push(DynamicTemplatePart::MemberExpr( + sym.clone(), + sym1.clone(), + sym2.clone(), + )); + is_member_expr = true; + } + _ => {} + }, + _ => {} + }, + _ => {} + } + + if !is_member_expr { + parts.push(DynamicTemplatePart::Expr); + } } } DynamicArgument::Template(parts) @@ -881,6 +915,7 @@ const d9 = await import("./foo/" + value + ".ts"); const d10 = await import(value + ".ts"); const d11 = await import("./foo/" - value); const d12 = await import(expr); +const d13 = await import(`${Deno.build.target}/test`); "#; let (start_pos, dependencies) = helper("file:///test.ts", source); assert_eq!( @@ -1008,6 +1043,27 @@ const d12 = await import(expr); import_attributes: ImportAttributes::None, } .into(), + DynamicDependencyDescriptor { + leading_comments: Vec::new(), + range: SourceRange { + start: start_pos + 543, + end: start_pos + 578, + }, + argument: DynamicArgument::Template(vec![ + DynamicTemplatePart::MemberExpr( + JsWord::from("Deno"), + JsWord::from("build"), + JsWord::from("target") + ), + DynamicTemplatePart::String(JsWord::from("/test")), + ]), + argument_range: SourceRange { + start: start_pos + 550, + end: start_pos + 577, + }, + import_attributes: ImportAttributes::None, + } + .into(), ] ); } From fccbf7153d76cecfa62d8ea4bc5beed34f8c774b Mon Sep 17 00:00:00 2001 From: mutefish0 <648262030@qq.com> Date: Tue, 6 Aug 2024 21:23:37 +0800 Subject: [PATCH 2/2] fix lint --- src/dep.rs | 43 ++++++++++++++++++++----------------------- 1 file changed, 20 insertions(+), 23 deletions(-) diff --git a/src/dep.rs b/src/dep.rs index 2e47763..c31bdb3 100644 --- a/src/dep.rs +++ b/src/dep.rs @@ -311,30 +311,27 @@ impl<'a> Visit for DependencyCollector<'a> { let mut is_member_expr = false; if let Some(expr) = tpl.exprs.get(i) { - match **expr { - Expr::Member(MemberExpr { - prop: MemberProp::Ident(IdentName { sym: ref sym2, .. }), - ref obj, + if let Expr::Member(MemberExpr { + prop: MemberProp::Ident(IdentName { sym: ref sym2, .. }), + ref obj, + .. + }) = **expr + { + if let Expr::Member(MemberExpr { + prop: MemberProp::Ident(IdentName { sym: ref sym1, .. }), + obj: ref obj1, .. - }) => match **obj { - Expr::Member(MemberExpr { - prop: MemberProp::Ident(IdentName { sym: ref sym1, .. }), - obj: ref obj1, - .. - }) => match **obj1 { - Expr::Ident(Ident { ref sym, .. }) => { - parts.push(DynamicTemplatePart::MemberExpr( - sym.clone(), - sym1.clone(), - sym2.clone(), - )); - is_member_expr = true; - } - _ => {} - }, - _ => {} - }, - _ => {} + }) = **obj + { + if let Expr::Ident(Ident { ref sym, .. }) = **obj1 { + parts.push(DynamicTemplatePart::MemberExpr( + sym.clone(), + sym1.clone(), + sym2.clone(), + )); + is_member_expr = true; + } + } } if !is_member_expr {