From 5232e3aba23efbf2f9603dafe0a3cd75c40a685c Mon Sep 17 00:00:00 2001 From: HeYunfei Date: Sun, 23 Apr 2023 12:53:54 +0800 Subject: [PATCH] refactor: align `starts_with` with the same semantics (#2872) * refactor: align `starts_with` with the same semantics * Fix * Fix --- .../dependency/import_meta_scanner.rs | 87 ++++++++++--------- .../src/visitors/dependency/util.rs | 6 +- 2 files changed, 47 insertions(+), 46 deletions(-) diff --git a/crates/rspack_plugin_javascript/src/visitors/dependency/import_meta_scanner.rs b/crates/rspack_plugin_javascript/src/visitors/dependency/import_meta_scanner.rs index 4b3f2b895d5..4727e44163e 100644 --- a/crates/rspack_plugin_javascript/src/visitors/dependency/import_meta_scanner.rs +++ b/crates/rspack_plugin_javascript/src/visitors/dependency/import_meta_scanner.rs @@ -47,71 +47,72 @@ impl VisitAstPath for ImportMetaScanner<'_> { expr: &'ast Expr, ast_path: &mut AstNodePath>, ) { - // exclude import.meta.webpackHot - if is_member_expr_starts_with_import_meta_webpack_hot(expr) { - return; - } - // import.meta.url - if expr_matcher::is_import_meta_url(expr) { - let url = Url::from_file_path(&self.resource_data.resource).expect("should be a path"); - self.add_presentational_dependency(box ConstDependency::new( - Expr::Lit(Lit::Str(Str { - span: DUMMY_SP, - value: format!("'{}'", url.as_str()).into(), - raw: Some(format!("'{}'", url.as_str()).into()), - })), - None, - as_parent_path(ast_path), - )); - } - // import.meta.xxx - else if is_member_expr_starts_with_import_meta(expr) { - self.add_presentational_dependency(box ConstDependency::new( - quote!("undefined" as Expr), - None, - as_parent_path(ast_path), - )); - } - // import.meta - else if expr_matcher::is_import_meta(expr) { - // TODO add warning - self.add_presentational_dependency(box ConstDependency::new( - quote!("({})" as Expr), - None, - as_parent_path(ast_path), - )); - } else if let Expr::Unary(UnaryExpr { + // Deal with `typeof import.meta.url` and ``typeof import.meta.xxx` + if let Expr::Unary(UnaryExpr { op: UnaryOp::TypeOf, arg: box expr, .. }) = expr { - // typeof import.meta.url - if expr_matcher::is_import_meta_url(expr) { + // typeof import.meta + if expr_matcher::is_import_meta(expr) { + self.add_presentational_dependency(box ConstDependency::new( + quote!("'object'" as Expr), + None, + as_parent_path(ast_path), + )); + } else if expr_matcher::is_import_meta_url(expr) { + // typeof import.meta.url self.add_presentational_dependency(box ConstDependency::new( quote!("'string'" as Expr), None, as_parent_path(ast_path), )); - } - // typeof import.meta.xxx - else if is_member_expr_starts_with_import_meta(expr) { + } else if is_member_expr_starts_with_import_meta(expr) { + // typeof import.meta.xxx self.add_presentational_dependency(box ConstDependency::new( quote!("undefined" as Expr), None, as_parent_path(ast_path), )); } - // typeof import.meta - else if expr_matcher::is_import_meta(expr) { + } else { + // Deal with `import.meta` and `import.meta.xxx` + + // exclude import.meta.webpackHot + if is_member_expr_starts_with_import_meta_webpack_hot(expr) { + return; + } + + // import.meta + if expr_matcher::is_import_meta(expr) { + // TODO(underfin): add warning self.add_presentational_dependency(box ConstDependency::new( - quote!("'object'" as Expr), + quote!("({})" as Expr), + None, + as_parent_path(ast_path), + )); + } else if expr_matcher::is_import_meta_url(expr) { + // import.meta.url + let url = Url::from_file_path(&self.resource_data.resource).expect("should be a path"); + self.add_presentational_dependency(box ConstDependency::new( + Expr::Lit(Lit::Str(Str { + span: DUMMY_SP, + value: format!("'{}'", url.as_str()).into(), + raw: Some(format!("'{}'", url.as_str()).into()), + })), + None, + as_parent_path(ast_path), + )); + } else if is_member_expr_starts_with_import_meta(expr) { + // import.meta.xxx + self.add_presentational_dependency(box ConstDependency::new( + quote!("undefined" as Expr), None, as_parent_path(ast_path), )); } } - expr.visit_children_with_path(self, ast_path); } } diff --git a/crates/rspack_plugin_javascript/src/visitors/dependency/util.rs b/crates/rspack_plugin_javascript/src/visitors/dependency/util.rs index 64c82848542..2bdaa06b103 100644 --- a/crates/rspack_plugin_javascript/src/visitors/dependency/util.rs +++ b/crates/rspack_plugin_javascript/src/visitors/dependency/util.rs @@ -112,12 +112,12 @@ pub fn is_import_meta_hot_decline_call(node: &CallExpr) -> bool { .unwrap_or_default() } -// Notice: Doesn't include `import.meta` itself +// Notice: Include `import.meta` itself pub fn is_member_expr_starts_with_import_meta(mut expr: &Expr) -> bool { loop { match expr { - Expr::Member(MemberExpr { obj, .. }) if expr_matcher::is_import_meta(obj) => return true, - Expr::Member(MemberExpr { obj, .. }) if obj.is_member() => expr = obj.as_ref(), + _ if expr_matcher::is_import_meta(expr) => return true, + Expr::Member(MemberExpr { obj, .. }) => expr = obj.as_ref(), _ => return false, } }