swc-plugin-component-annotate 1.11.0 → 1.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -2
- package/src/jsx_utils.rs +9 -3
- package/src/lib.rs +47 -21
- package/swc_plugin_component_annotate.wasm +0 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "swc-plugin-component-annotate",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.13.0",
|
|
4
4
|
"description": "Use SWC to automatically annotate React components with data attributes for component tracking",
|
|
5
5
|
"author": "scttcper <scttcper@gmail.com>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
"swc"
|
|
10
10
|
],
|
|
11
11
|
"scripts": {
|
|
12
|
-
"build": "cargo build --target wasm32-unknown-unknown",
|
|
12
|
+
"build": "cargo build --release --target wasm32-unknown-unknown",
|
|
13
13
|
"test": "cargo test",
|
|
14
14
|
"prepack": "cp -rf target/wasm32-unknown-unknown/release/swc_plugin_component_annotate.wasm ."
|
|
15
15
|
},
|
package/src/jsx_utils.rs
CHANGED
|
@@ -15,7 +15,9 @@ pub fn is_react_fragment(element: &JSXElementName) -> bool {
|
|
|
15
15
|
}
|
|
16
16
|
false
|
|
17
17
|
}
|
|
18
|
-
_ => false,
|
|
18
|
+
JSXElementName::JSXNamespacedName(_) => false,
|
|
19
|
+
#[cfg(swc_ast_unknown)]
|
|
20
|
+
_ => panic!("unknown jsx element name"),
|
|
19
21
|
}
|
|
20
22
|
}
|
|
21
23
|
|
|
@@ -30,6 +32,8 @@ pub fn get_element_name(element: &JSXElementName) -> Cow<str> {
|
|
|
30
32
|
JSXElementName::JSXNamespacedName(namespaced) => {
|
|
31
33
|
Cow::Owned(format!("{}:{}", namespaced.ns.sym, namespaced.name.sym))
|
|
32
34
|
}
|
|
35
|
+
#[cfg(swc_ast_unknown)]
|
|
36
|
+
_ => panic!("unknown jsx element name"),
|
|
33
37
|
}
|
|
34
38
|
}
|
|
35
39
|
|
|
@@ -44,6 +48,8 @@ fn get_member_expression_name(member_expr: &JSXMemberExpr) -> String {
|
|
|
44
48
|
member_expr.prop.sym
|
|
45
49
|
);
|
|
46
50
|
}
|
|
51
|
+
#[cfg(swc_ast_unknown)]
|
|
52
|
+
_ => panic!("unknown jsx object"),
|
|
47
53
|
};
|
|
48
54
|
|
|
49
55
|
format!("{}.{}", obj_name, member_expr.prop.sym)
|
|
@@ -65,10 +71,10 @@ pub fn create_jsx_attr(name: &str, value: &str) -> JSXAttrOrSpread {
|
|
|
65
71
|
JSXAttrOrSpread::JSXAttr(JSXAttr {
|
|
66
72
|
span: Default::default(),
|
|
67
73
|
name: JSXAttrName::Ident(IdentName::new(name.into(), Default::default())),
|
|
68
|
-
value: Some(JSXAttrValue::
|
|
74
|
+
value: Some(JSXAttrValue::Str(Str {
|
|
69
75
|
span: Default::default(),
|
|
70
76
|
value: value.into(),
|
|
71
77
|
raw: None,
|
|
72
|
-
}))
|
|
78
|
+
})),
|
|
73
79
|
})
|
|
74
80
|
}
|
package/src/lib.rs
CHANGED
|
@@ -86,6 +86,8 @@ impl ReactComponentAnnotateVisitor {
|
|
|
86
86
|
// Fragments are always transparent containers
|
|
87
87
|
jsx_fragment.visit_mut_with(self);
|
|
88
88
|
}
|
|
89
|
+
#[cfg(swc_ast_unknown)]
|
|
90
|
+
JSXElementChild::Unknown(..) => panic!("unknown jsx element child"),
|
|
89
91
|
_ => {}
|
|
90
92
|
}
|
|
91
93
|
}
|
|
@@ -101,6 +103,8 @@ impl ReactComponentAnnotateVisitor {
|
|
|
101
103
|
JSXElementChild::JSXFragment(jsx_fragment) => {
|
|
102
104
|
jsx_fragment.visit_mut_with(self);
|
|
103
105
|
}
|
|
106
|
+
#[cfg(swc_ast_unknown)]
|
|
107
|
+
JSXElementChild::Unknown(..) => panic!("unknown jsx element child"),
|
|
104
108
|
_ => {}
|
|
105
109
|
}
|
|
106
110
|
}
|
|
@@ -210,6 +214,8 @@ impl ReactComponentAnnotateVisitor {
|
|
|
210
214
|
Expr::Paren(paren_expr) => {
|
|
211
215
|
self.process_return_expression(&mut paren_expr.expr);
|
|
212
216
|
}
|
|
217
|
+
#[cfg(swc_ast_unknown)]
|
|
218
|
+
Expr::Unknown(..) => panic!("unknown expr"),
|
|
213
219
|
_ => {}
|
|
214
220
|
}
|
|
215
221
|
}
|
|
@@ -223,6 +229,8 @@ impl ReactComponentAnnotateVisitor {
|
|
|
223
229
|
let callee_name = match call_expr.callee.as_expr() {
|
|
224
230
|
Some(expr) => match expr.as_ref() {
|
|
225
231
|
Expr::Ident(ident) => ident.sym.as_ref(),
|
|
232
|
+
#[cfg(swc_ast_unknown)]
|
|
233
|
+
Expr::Unknown(..) => panic!("unknown expr"),
|
|
226
234
|
_ => return None,
|
|
227
235
|
},
|
|
228
236
|
_ => return None,
|
|
@@ -340,7 +348,7 @@ impl VisitMut for ReactComponentAnnotateVisitor {
|
|
|
340
348
|
fn visit_mut_import_decl(&mut self, import_decl: &mut ImportDecl) {
|
|
341
349
|
// Track imports from @emotion/styled (only if enabled)
|
|
342
350
|
if self.config.experimental_rewrite_emotion_styled
|
|
343
|
-
&& import_decl.src.value
|
|
351
|
+
&& import_decl.src.value == "@emotion/styled"
|
|
344
352
|
{
|
|
345
353
|
for specifier in &import_decl.specifiers {
|
|
346
354
|
match specifier {
|
|
@@ -352,16 +360,22 @@ impl VisitMut for ReactComponentAnnotateVisitor {
|
|
|
352
360
|
ImportSpecifier::Named(named_import) => {
|
|
353
361
|
// Check if the imported name is 'default' or 'styled'
|
|
354
362
|
let imported_name = match &named_import.imported {
|
|
355
|
-
Some(ModuleExportName::Ident(ident)) => ident.sym.as_ref(),
|
|
356
|
-
|
|
357
|
-
|
|
363
|
+
Some(ModuleExportName::Ident(ident)) => Some(ident.sym.as_ref()),
|
|
364
|
+
Some(ModuleExportName::Str(str)) => str.value.as_str(),
|
|
365
|
+
None => Some(named_import.local.sym.as_ref()),
|
|
366
|
+
#[cfg(swc_ast_unknown)]
|
|
367
|
+
Some(_) => panic!("unknown module export name"),
|
|
358
368
|
};
|
|
359
369
|
|
|
360
|
-
if imported_name
|
|
361
|
-
|
|
370
|
+
if let Some(imported_name) = imported_name {
|
|
371
|
+
if imported_name == "default" || imported_name == "styled" {
|
|
372
|
+
self.styled_import = Some(named_import.local.sym.to_string());
|
|
373
|
+
}
|
|
362
374
|
}
|
|
363
375
|
}
|
|
364
|
-
_ => {}
|
|
376
|
+
ImportSpecifier::Namespace(_) => {}
|
|
377
|
+
#[cfg(swc_ast_unknown)]
|
|
378
|
+
_ => panic!("unknown import specifier"),
|
|
365
379
|
}
|
|
366
380
|
}
|
|
367
381
|
}
|
|
@@ -416,6 +430,8 @@ impl VisitMut for ReactComponentAnnotateVisitor {
|
|
|
416
430
|
// Direct expression return
|
|
417
431
|
self.process_return_expression(expr);
|
|
418
432
|
}
|
|
433
|
+
#[cfg(swc_ast_unknown)]
|
|
434
|
+
_ => panic!("unknown block stmt or expr"),
|
|
419
435
|
}
|
|
420
436
|
|
|
421
437
|
self.current_component_name = None;
|
|
@@ -423,6 +439,8 @@ impl VisitMut for ReactComponentAnnotateVisitor {
|
|
|
423
439
|
Expr::Fn(func_expr) => {
|
|
424
440
|
self.find_jsx_in_function_body(&mut func_expr.function, component_name);
|
|
425
441
|
}
|
|
442
|
+
#[cfg(swc_ast_unknown)]
|
|
443
|
+
Expr::Unknown(..) => panic!("unknown expr"),
|
|
426
444
|
_ => {}
|
|
427
445
|
}
|
|
428
446
|
}
|
|
@@ -436,25 +454,33 @@ impl VisitMut for ReactComponentAnnotateVisitor {
|
|
|
436
454
|
|
|
437
455
|
// Look for render method
|
|
438
456
|
for member in &mut class_decl.class.body {
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
if
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
if let
|
|
449
|
-
|
|
457
|
+
match member {
|
|
458
|
+
ClassMember::Method(method) => match &method.key {
|
|
459
|
+
PropName::Ident(ident) => {
|
|
460
|
+
if ident.sym.as_ref() == "render" {
|
|
461
|
+
if let Some(body) = &mut method.function.body {
|
|
462
|
+
self.current_component_name = Some(component_name.clone());
|
|
463
|
+
|
|
464
|
+
// Look for return statements
|
|
465
|
+
for stmt in &mut body.stmts {
|
|
466
|
+
if let Stmt::Return(return_stmt) = stmt {
|
|
467
|
+
if let Some(arg) = &mut return_stmt.arg {
|
|
468
|
+
self.process_return_expression(arg);
|
|
469
|
+
}
|
|
450
470
|
}
|
|
451
471
|
}
|
|
452
|
-
}
|
|
453
472
|
|
|
454
|
-
|
|
473
|
+
self.current_component_name = None;
|
|
474
|
+
}
|
|
455
475
|
}
|
|
456
476
|
}
|
|
457
|
-
|
|
477
|
+
#[cfg(swc_ast_unknown)]
|
|
478
|
+
PropName::Unknown(..) => panic!("unknown prop name"),
|
|
479
|
+
_ => {}
|
|
480
|
+
},
|
|
481
|
+
#[cfg(swc_ast_unknown)]
|
|
482
|
+
ClassMember::Unknown(..) => panic!("unknown class member"),
|
|
483
|
+
_ => {}
|
|
458
484
|
}
|
|
459
485
|
}
|
|
460
486
|
|
|
Binary file
|