From fc3d002abdd673c7018fd8f7303d6f07dbb3c02c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Thu, 11 Apr 2024 09:59:30 +0900 Subject: [PATCH] Impl --- Cargo.lock | 80 +++++----- packages/swc-debug-jsx/package.json | 4 +- packages/swc-debug-jsx/src/lib.rs | 23 +-- .../transform/src/import_analyzer.rs | 92 ----------- packages/swc-debug-jsx/transform/src/lib.rs | 147 ++++++------------ .../swc-debug-jsx/transform/tests/fixture.rs | 33 ---- 6 files changed, 96 insertions(+), 283 deletions(-) delete mode 100644 packages/swc-debug-jsx/transform/src/import_analyzer.rs delete mode 100644 packages/swc-debug-jsx/transform/tests/fixture.rs diff --git a/Cargo.lock b/Cargo.lock index 24f08de42..73be8d2ac 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -154,7 +154,7 @@ dependencies = [ "proc-macro2", "quote", "swc_macros_common", - "syn 2.0.32", + "syn 2.0.58", ] [[package]] @@ -424,7 +424,7 @@ dependencies = [ "heck", "proc-macro2", "quote", - "syn 2.0.32", + "syn 2.0.58", ] [[package]] @@ -562,14 +562,14 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "13b588ba4ac1a99f7f2964d24b3d896ddc6bf847ee3855dbd4366f058cfcd331" dependencies = [ "quote", - "syn 2.0.32", + "syn 2.0.58", ] [[package]] name = "dashmap" -version = "5.5.0" +version = "5.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6943ae99c34386c84a470c499d3414f66502a41340aa895406e0d2e4a207b91d" +checksum = "978747c1d849a7d2ee5e8adc0159961c48fb7e5db2f06af6723b80123bb53856" dependencies = [ "cfg-if", "hashbrown 0.14.3", @@ -718,7 +718,7 @@ checksum = "3a0b11eeb173ce52f84ebd943d42e58813a2ebb78a6a3ff0a243b71c5199cd7b" dependencies = [ "proc-macro2", "swc_macros_common", - "syn 2.0.32", + "syn 2.0.58", ] [[package]] @@ -908,7 +908,7 @@ dependencies = [ "pmutil", "proc-macro2", "quote", - "syn 2.0.32", + "syn 2.0.58", ] [[package]] @@ -1490,7 +1490,7 @@ dependencies = [ "phf_shared 0.11.2", "proc-macro2", "quote", - "syn 2.0.32", + "syn 2.0.58", ] [[package]] @@ -1525,7 +1525,7 @@ checksum = "52a40bc70c2c58040d2d8b167ba9a5ff59fc9dab7ad44771cfde3dcfde7a09c6" dependencies = [ "proc-macro2", "quote", - "syn 2.0.32", + "syn 2.0.58", ] [[package]] @@ -1600,9 +1600,9 @@ checksum = "dc375e1527247fe1a97d8b7156678dfe7c1af2fc075c9a4db3690ecd2a148068" [[package]] name = "proc-macro2" -version = "1.0.66" +version = "1.0.79" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "18fb31db3f9bddb2ea821cde30a9f70117e3f119938b5ee630b7403aa6e2ead9" +checksum = "e835ff2298f5721608eb1a980ecaee1aef2c132bf95ecc026a11b7bf3c01c02e" dependencies = [ "unicode-ident", ] @@ -1638,9 +1638,9 @@ dependencies = [ [[package]] name = "quote" -version = "1.0.33" +version = "1.0.36" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae" +checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" dependencies = [ "proc-macro2", ] @@ -1933,22 +1933,22 @@ checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" [[package]] name = "serde" -version = "1.0.189" +version = "1.0.197" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e422a44e74ad4001bdc8eede9a4570ab52f71190e9c076d14369f38b9200537" +checksum = "3fb1c873e1b9b056a4dc4c0c198b24c3ffa059243875552b2bd0933b1aee4ce2" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.189" +version = "1.0.197" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e48d1f918009ce3145511378cf68d613e3b3d9137d67272562080d68a2b32d5" +checksum = "7eb0b34b42edc17f6b7cac84a52a1c5f0e1bb2227e997ca9011ea3dd34e8610b" dependencies = [ "proc-macro2", "quote", - "syn 2.0.32", + "syn 2.0.58", ] [[package]] @@ -2099,7 +2099,7 @@ dependencies = [ "pmutil", "proc-macro2", "quote", - "syn 2.0.32", + "syn 2.0.58", ] [[package]] @@ -2164,7 +2164,7 @@ dependencies = [ "proc-macro2", "quote", "swc_macros_common", - "syn 2.0.32", + "syn 2.0.58", ] [[package]] @@ -2335,7 +2335,7 @@ dependencies = [ "proc-macro2", "quote", "swc_macros_common", - "syn 2.0.32", + "syn 2.0.58", ] [[package]] @@ -2396,7 +2396,7 @@ dependencies = [ "proc-macro2", "quote", "swc_macros_common", - "syn 2.0.32", + "syn 2.0.58", ] [[package]] @@ -2536,7 +2536,7 @@ dependencies = [ "proc-macro2", "quote", "swc_macros_common", - "syn 2.0.32", + "syn 2.0.58", ] [[package]] @@ -2608,7 +2608,7 @@ dependencies = [ "swc_ecma_ast", "swc_ecma_parser", "swc_macros_common", - "syn 2.0.32", + "syn 2.0.58", ] [[package]] @@ -2670,7 +2670,7 @@ dependencies = [ "proc-macro2", "quote", "swc_macros_common", - "syn 2.0.32", + "syn 2.0.58", ] [[package]] @@ -2699,9 +2699,9 @@ dependencies = [ [[package]] name = "swc_ecma_transforms_react" -version = "0.183.17" +version = "0.183.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "762dad12edcdca424213354518ce60bc3f03a026f8e1990b11459311cef04c91" +checksum = "1b7b7de90ff41560bf021acda3fb16fb0f4f5885aeb44b6b7e638b563124d087" dependencies = [ "base64 0.21.5", "dashmap", @@ -2831,7 +2831,7 @@ checksum = "695a1d8b461033d32429b5befbf0ad4d7a2c4d6ba9cd5ba4e0645c615839e8e4" dependencies = [ "proc-macro2", "quote", - "syn 2.0.32", + "syn 2.0.58", ] [[package]] @@ -2867,7 +2867,7 @@ checksum = "50176cfc1cbc8bb22f41c6fe9d1ec53fbe057001219b5954961b8ad0f336fce9" dependencies = [ "proc-macro2", "quote", - "syn 2.0.32", + "syn 2.0.58", ] [[package]] @@ -2962,7 +2962,7 @@ checksum = "3232db481484070637b20a155c064096c0ea1ba04fa2247b89b618661b3574f4" dependencies = [ "proc-macro2", "quote", - "syn 2.0.32", + "syn 2.0.58", ] [[package]] @@ -3138,7 +3138,7 @@ checksum = "ff9719b6085dd2824fd61938a881937be14b08f95e2d27c64c825a9f65e052ba" dependencies = [ "proc-macro2", "quote", - "syn 2.0.32", + "syn 2.0.58", ] [[package]] @@ -3161,7 +3161,7 @@ dependencies = [ "proc-macro2", "quote", "swc_macros_common", - "syn 2.0.32", + "syn 2.0.58", ] [[package]] @@ -3177,9 +3177,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.32" +version = "2.0.58" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "239814284fd6f1a4ffe4ca893952cdd93c224b6a1571c9a9eadd670295c0c9e2" +checksum = "44cfb93f38070beee36b3fef7d4f5a16f27751d94b187b666a5cc5e9b0d30687" dependencies = [ "proc-macro2", "quote", @@ -3258,7 +3258,7 @@ dependencies = [ "quote", "regex", "relative-path", - "syn 2.0.32", + "syn 2.0.58", ] [[package]] @@ -3289,7 +3289,7 @@ checksum = "6bb623b56e39ab7dcd4b1b98bb6c8f8d907ed255b18de254088016b27a8ee19b" dependencies = [ "proc-macro2", "quote", - "syn 2.0.32", + "syn 2.0.58", ] [[package]] @@ -3337,7 +3337,7 @@ checksum = "5f4f31f56159e98206da9efd823404b79b6ef3143b4a7ab76e67b1751b25a4ab" dependencies = [ "proc-macro2", "quote", - "syn 2.0.32", + "syn 2.0.58", ] [[package]] @@ -3520,7 +3520,7 @@ dependencies = [ "once_cell", "proc-macro2", "quote", - "syn 2.0.32", + "syn 2.0.58", "wasm-bindgen-shared", ] @@ -3542,7 +3542,7 @@ checksum = "54681b18a46765f095758388f2d0cf16eb8d4169b639ab575a8f5693af210c7b" dependencies = [ "proc-macro2", "quote", - "syn 2.0.32", + "syn 2.0.58", "wasm-bindgen-backend", "wasm-bindgen-shared", ] @@ -3700,5 +3700,5 @@ checksum = "c2f140bda219a26ccc0cdb03dba58af72590c53b22642577d88a927bc5c87d6b" dependencies = [ "proc-macro2", "quote", - "syn 2.0.32", + "syn 2.0.58", ] diff --git a/packages/swc-debug-jsx/package.json b/packages/swc-debug-jsx/package.json index 1c6dc2e66..6798d8ce4 100644 --- a/packages/swc-debug-jsx/package.json +++ b/packages/swc-debug-jsx/package.json @@ -2,9 +2,9 @@ "name": "@swc/plugin-debug-jsx", "version": "1.5.121", "description": "SWC plugin for debugging jsx", - "main": "swc_plugin_swc_magic.wasm", + "main": "swc_plugin_debug_jsx.wasm", "scripts": { - "prepack": "cp ../../target/wasm32-wasi/release/swc_plugin_swc_magic.wasm ." + "prepack": "cp ../../target/wasm32-wasi/release/swc_plugin_debug_jsx.wasm ." }, "homepage": "https://swc.rs", "repository": { diff --git a/packages/swc-debug-jsx/src/lib.rs b/packages/swc-debug-jsx/src/lib.rs index 6d2ac561c..8be7c2445 100644 --- a/packages/swc-debug-jsx/src/lib.rs +++ b/packages/swc-debug-jsx/src/lib.rs @@ -1,30 +1,17 @@ #![allow(clippy::not_unsafe_ptr_arg_deref)] #![feature(box_patterns)] +use debug_jsx::swc_debug_jsx; use swc_core::{ ecma::{ast::Program, visit::VisitMutWith}, - plugin::{ - plugin_transform, - proxies::{PluginCommentsProxy, TransformPluginProgramMetadata}, - }, + plugin::{plugin_transform, proxies::TransformPluginProgramMetadata}, }; #[plugin_transform] -fn swc_magic_plugin(mut program: Program, data: TransformPluginProgramMetadata) -> Program { - let config = serde_json::from_str::( - &data - .get_transform_plugin_config() - .expect("failed to get plugin config for swc-magic"), - ) - .expect("invalid config for swc-magic"); +fn swc_debug_jsx_plugin(mut program: Program, data: TransformPluginProgramMetadata) -> Program { + let cm = data.source_map; - let unresolved_mark = data.unresolved_mark; - - program.visit_mut_with(&mut swc_magic::swc_magic( - unresolved_mark, - config, - PluginCommentsProxy, - )); + program.visit_mut_with(&mut swc_debug_jsx(Box::new(cm))); program } diff --git a/packages/swc-debug-jsx/transform/src/import_analyzer.rs b/packages/swc-debug-jsx/transform/src/import_analyzer.rs deleted file mode 100644 index be4667100..000000000 --- a/packages/swc-debug-jsx/transform/src/import_analyzer.rs +++ /dev/null @@ -1,92 +0,0 @@ -use swc_atoms::JsWord; -use swc_common::collections::{AHashMap, AHashSet}; -use swc_ecma_ast::*; -use swc_ecma_visit::{noop_visit_type, Visit, VisitWith}; - -#[derive(Debug, Default)] -pub(crate) struct ImportMap { - /// Map from module name to (module path, exported symbol) - imports: AHashMap, - - namespace_imports: AHashMap, - - imported_modules: AHashSet, -} - -impl ImportMap { - /// Returns true if `e` is an import of `orig_name` from `module`. - pub fn is_import(&self, e: &Expr, module: &str, orig_name: &str) -> bool { - match e { - Expr::Ident(i) => { - if let Some((i_src, i_sym)) = self.imports.get(&i.to_id()) { - i_src == module && i_sym == orig_name - } else { - false - } - } - - Expr::Member(MemberExpr { - obj: box Expr::Ident(obj), - prop: MemberProp::Ident(prop), - .. - }) => { - if let Some(obj_src) = self.namespace_imports.get(&obj.to_id()) { - obj_src == module && prop.sym == *orig_name - } else { - false - } - } - - _ => false, - } - } - - pub fn analyze(m: &Module) -> Self { - let mut data = ImportMap::default(); - - m.visit_with(&mut Analyzer { data: &mut data }); - - data - } -} - -struct Analyzer<'a> { - data: &'a mut ImportMap, -} - -impl Visit for Analyzer<'_> { - noop_visit_type!(); - - fn visit_import_decl(&mut self, import: &ImportDecl) { - self.data.imported_modules.insert(import.src.value.clone()); - - for s in &import.specifiers { - let (local, orig_sym) = match s { - ImportSpecifier::Named(ImportNamedSpecifier { - local, imported, .. - }) => match imported { - Some(imported) => (local.to_id(), orig_name(imported)), - _ => (local.to_id(), local.sym.clone()), - }, - ImportSpecifier::Default(s) => (s.local.to_id(), "default".into()), - ImportSpecifier::Namespace(s) => { - self.data - .namespace_imports - .insert(s.local.to_id(), import.src.value.clone()); - continue; - } - }; - - self.data - .imports - .insert(local, (import.src.value.clone(), orig_sym)); - } - } -} - -fn orig_name(n: &ModuleExportName) -> JsWord { - match n { - ModuleExportName::Ident(v) => v.sym.clone(), - ModuleExportName::Str(v) => v.value.clone(), - } -} diff --git a/packages/swc-debug-jsx/transform/src/lib.rs b/packages/swc-debug-jsx/transform/src/lib.rs index a48121522..59fb82bb6 100644 --- a/packages/swc-debug-jsx/transform/src/lib.rs +++ b/packages/swc-debug-jsx/transform/src/lib.rs @@ -1,110 +1,61 @@ -#![feature(box_patterns)] +use std::sync::Arc; -use import_analyzer::ImportMap; -use serde::Deserialize; -use swc_atoms::Atom; -use swc_common::{ - comments::Comments, errors::HANDLER, util::take::Take, Mark, Span, Spanned, DUMMY_SP, -}; -use swc_ecma_ast::{CallExpr, Callee, EmptyStmt, Expr, Module, ModuleDecl, ModuleItem, Stmt}; -use swc_ecma_visit::{VisitMut, VisitMutWith}; +use swc_common::{SourceMap, SourceMapper, DUMMY_SP}; +use swc_ecma_ast::*; +use swc_ecma_utils::quote_ident; +use swc_ecma_visit::{noop_visit_mut_type, VisitMut, VisitMutWith}; -mod import_analyzer; - -#[derive(Debug, Clone, Deserialize)] -pub struct Config { - #[serde(default = "default_import_path")] - pub import_path: Atom, -} - -fn default_import_path() -> Atom { - Atom::from("@swc/magic") -} - -impl Config {} - -pub fn swc_magic(_unreolved_mark: Mark, config: Config, comments: C) -> impl VisitMut -where - C: Comments, -{ - Magic { - config, - comments, - imports: Default::default(), - } +pub fn swc_debug_jsx(cm: Box) -> impl VisitMut { + JsxSrc { cm } } -const MARK_AS_PURE_FN_NAME: &str = "markAsPure"; - -/// Handles functions from `@swc/magic`. -struct Magic -where - C: Comments, -{ - config: Config, - comments: C, - imports: ImportMap, +struct JsxSrc { + cm: Box, } -impl VisitMut for Magic -where - C: Comments, -{ - fn visit_mut_expr(&mut self, e: &mut Expr) { - e.visit_mut_children_with(self); - - if let Expr::Call(CallExpr { - span, - callee: Callee::Expr(callee), - args, - .. - }) = e - { - if !self - .imports - .is_import(callee, &self.config.import_path, MARK_AS_PURE_FN_NAME) - { - return; - } +impl VisitMut for JsxSrc { + noop_visit_mut_type!(); - if args.len() != 1 { - HANDLER.with(|handler| { - handler - .struct_span_err(*span, "markAsPure() does not support multiple arguments") - .emit(); - }); - return; - } - - *e = *args[0].expr.take(); - - let mut lo = e.span().lo; - if lo.is_dummy() { - lo = Span::dummy_with_cmt().lo; - } - - self.comments.add_pure_comment(lo); + fn visit_mut_jsx_opening_element(&mut self, e: &mut JSXOpeningElement) { + if e.span == DUMMY_SP { + return; } - } - - fn visit_mut_module(&mut self, m: &mut Module) { - self.imports = ImportMap::analyze(m); - m.visit_mut_children_with(self); - - // Remove Stmt::Empty - m.body - .retain(|item| !matches!(item, ModuleItem::Stmt(Stmt::Empty(..)))); - } - - fn visit_mut_module_item(&mut self, m: &mut ModuleItem) { - if let ModuleItem::ModuleDecl(ModuleDecl::Import(import)) = m { - if import.src.value == self.config.import_path { - *m = ModuleItem::Stmt(Stmt::Empty(EmptyStmt { span: DUMMY_SP })); - return; - } - } + e.visit_mut_children_with(self); - m.visit_mut_children_with(self); + let loc = self.cm.lookup_char_pos(e.span.lo); + let file_name = loc.file.name.to_string(); + + e.attrs.push(JSXAttrOrSpread::JSXAttr(JSXAttr { + span: DUMMY_SP, + name: JSXAttrName::Ident(quote_ident!("__source")), + value: Some(JSXAttrValue::JSXExprContainer(JSXExprContainer { + span: DUMMY_SP, + expr: JSXExpr::Expr(Box::new( + ObjectLit { + span: DUMMY_SP, + props: vec![ + PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp { + key: PropName::Ident(quote_ident!("fileName")), + value: Box::new(Expr::Lit(Lit::Str(Str { + span: DUMMY_SP, + raw: None, + value: file_name.into(), + }))), + }))), + PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp { + key: PropName::Ident(quote_ident!("lineNumber")), + value: loc.line.into(), + }))), + PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp { + key: PropName::Ident(quote_ident!("columnNumber")), + value: (loc.col.0 + 1).into(), + }))), + ], + } + .into(), + )), + })), + })); } } diff --git a/packages/swc-debug-jsx/transform/tests/fixture.rs b/packages/swc-debug-jsx/transform/tests/fixture.rs deleted file mode 100644 index 0bb37107f..000000000 --- a/packages/swc-debug-jsx/transform/tests/fixture.rs +++ /dev/null @@ -1,33 +0,0 @@ -use std::path::PathBuf; - -use swc_common::{chain, Mark}; -use swc_ecma_parser::Syntax; -use swc_ecma_transforms_base::resolver; -use swc_ecma_transforms_testing::test_fixture; -use swc_ecma_visit::as_folder; - -#[testing::fixture("tests/fixture/**/input.js")] -fn pure(input: PathBuf) { - let output = input.parent().unwrap().join("output.js"); - test_fixture( - Syntax::default(), - &|tr| { - let unresolved_mark = Mark::new(); - let top_level_mark = Mark::new(); - - chain!( - resolver(unresolved_mark, top_level_mark, false), - as_folder(swc_magic::swc_magic( - unresolved_mark, - swc_magic::Config { - import_path: "@swc/magic".into() - }, - tr.comments.clone() - )) - ) - }, - &input, - &output, - Default::default(), - ); -}