Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DRAFT] chore: Add @swc/plugin-debug-jsx #282

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
379 changes: 230 additions & 149 deletions Cargo.lock

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,29 +37,29 @@ regex = { version = "1.10.4", default-features = false }
serde = "1.0.203"
serde_json = "1.0.117"
sourcemap = "9.0.0"
swc_atoms = "1.0.0"
swc_atoms = "1.0.1"
swc_cached = "0.3.20"
swc_common = "0.40.0"
swc_core = "0.106.0"
swc_common = "0.40.1"
swc_core = "0.106.4"
swc_css_ast = "0.147.0"
swc_css_codegen = "0.158.0"
swc_css_compat = "0.34.0"
swc_css_minifier = "0.123.0"
swc_css_parser = "0.157.0"
swc_css_prefixer = "0.161.0"
swc_css_visit = "0.146.0"
swc_ecma_ast = "0.121.0"
swc_ecma_codegen = "0.158.0"
swc_ecma_minifier = "0.208.0"
swc_ecma_parser = "0.152.0"
swc_ecma_ast = "0.121.1"
swc_ecma_codegen = "0.158.1"
swc_ecma_minifier = "0.208.1"
swc_ecma_parser = "0.152.1"
swc_ecma_transforms = "0.243.0"
swc_ecma_transforms_base = "0.149.0"
swc_ecma_transforms_react = "0.195.0"
swc_ecma_transforms_testing = "0.152.0"
swc_ecma_utils = "0.137.0"
swc_ecma_visit = "0.107.0"
swc_plugin_macro = "0.9.16"
swc_plugin_proxy = "0.50.0"
swc_plugin_proxy = "0.50.1"
swc_trace_macro = "1.0.0"
testing = "0.42.0"
tracing = "0.1.40"
Expand Down
2 changes: 2 additions & 0 deletions packages/swc-debug-jsx/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
transform/
tests/
25 changes: 25 additions & 0 deletions packages/swc-debug-jsx/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[package]

description = "SWC Plugin for swc-magic"


authors = { workspace = true }
edition = { workspace = true }
homepage = { workspace = true }
license = { workspace = true }
name = "swc_plugin_debug_jsx"
publish = false
repository = { workspace = true }
rust-version = { workspace = true }
version = "0.1.0"


[lib]
crate-type = ["cdylib", "rlib"]

[dependencies]
serde_json = "1.0.107"
swc_core = { version = "0.106.4", features = ["ecma_plugin_transform"] }


debug_jsx = { path = "./transform" }
3 changes: 3 additions & 0 deletions packages/swc-debug-jsx/README.md.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# @swc/plugin-debug-jsx

${CHANGELOG}
24 changes: 24 additions & 0 deletions packages/swc-debug-jsx/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "@swc/plugin-debug-jsx",
"version": "1.5.121",
"description": "SWC plugin for debugging jsx",
"main": "swc_plugin_debug_jsx.wasm",
"scripts": {
"prepack": "cp ../../target/wasm32-wasi/release/swc_plugin_debug_jsx.wasm ."
},
"homepage": "https://swc.rs",
"repository": {
"type": "git",
"url": "+https://github.com/swc-project/plugins.git"
},
"bugs": {
"url": "https://github.com/swc-project/plugins/issues"
},
"author": "강동윤 <kdy1997.dev@gmail.com>",
"keywords": [],
"license": "Apache-2.0",
"preferUnplugged": true,
"dependencies": {
"@swc/counter": "^0.1.3"
}
}
17 changes: 17 additions & 0 deletions packages/swc-debug-jsx/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +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::TransformPluginProgramMetadata},
};

#[plugin_transform]
fn swc_debug_jsx_plugin(mut program: Program, data: TransformPluginProgramMetadata) -> Program {
let cm = data.source_map;

program.visit_mut_with(&mut swc_debug_jsx(Box::new(cm)));

program
}
28 changes: 28 additions & 0 deletions packages/swc-debug-jsx/transform/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
[package]

description = "AST transforms visitor for swc-magic"


authors = { workspace = true }
edition = { workspace = true }
homepage = { workspace = true }
license = { workspace = true }
name = "debug_jsx"
repository = { workspace = true }
rust-version = { workspace = true }
version = "0.1.0"


[dependencies]
serde = { version = "1.0.189", features = ["derive"] }
swc_atoms = "1.0.1"
swc_common = "0.40.1"
swc_ecma_ast = "0.121.1"
swc_ecma_utils = "0.137.0"
swc_ecma_visit = "0.107.0"

[dev-dependencies]
swc_ecma_parser = "0.152.1"
swc_ecma_transforms_base = "0.149.0"
swc_ecma_transforms_testing = "0.152.0"
testing = "0.42.0"
59 changes: 59 additions & 0 deletions packages/swc-debug-jsx/transform/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use swc_common::{SourceMapper, DUMMY_SP};
use swc_ecma_ast::*;
use swc_ecma_utils::quote_ident;
use swc_ecma_visit::{noop_visit_mut_type, VisitMut, VisitMutWith};

pub fn swc_debug_jsx(cm: Box<dyn SourceMapper>) -> impl VisitMut {
JsxSrc { cm }
}

struct JsxSrc {
cm: Box<dyn SourceMapper>,
}

impl VisitMut for JsxSrc {
noop_visit_mut_type!();

fn visit_mut_jsx_opening_element(&mut self, e: &mut JSXOpeningElement) {
if e.span == DUMMY_SP {
return;
}

e.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(),
)),
})),
}));
}
}
2 changes: 1 addition & 1 deletion rust-toolchain
Original file line number Diff line number Diff line change
@@ -1 +1 @@
nightly-2024-04-16
nightly-2024-07-21
Loading