swc-plugin-component-annotate 1.0.0 → 1.2.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "swc-plugin-component-annotate",
3
- "version": "1.0.0",
3
+ "version": "1.2.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",
package/src/lib.rs CHANGED
@@ -1,9 +1,11 @@
1
1
  pub mod config;
2
2
  mod constants;
3
3
  mod jsx_utils;
4
+ pub mod path_utils;
4
5
 
5
6
  use config::PluginConfig;
6
7
  use jsx_utils::*;
8
+ use path_utils::extract_filename;
7
9
  use rustc_hash::FxHashSet;
8
10
  use swc_core::{
9
11
  common::FileName,
@@ -292,23 +294,9 @@ impl VisitMut for ReactComponentAnnotateVisitor {
292
294
  }
293
295
  }
294
296
 
295
- fn extract_filename(filename: &FileName) -> Option<String> {
296
- match filename {
297
- FileName::Real(path) => path
298
- .file_name()
299
- .and_then(|name| name.to_str())
300
- .map(|s| s.to_string()),
301
- FileName::Custom(custom) => {
302
- if custom.contains('/') {
303
- custom.split('/').last().map(|s| s.to_string())
304
- } else if custom.contains('\\') {
305
- custom.split('\\').last().map(|s| s.to_string())
306
- } else {
307
- Some(custom.clone())
308
- }
309
- }
310
- _ => None,
311
- }
297
+ // Export for testing
298
+ pub fn extract_filename_for_test(filename: &FileName) -> Option<String> {
299
+ extract_filename(filename)
312
300
  }
313
301
 
314
302
  #[plugin_transform]
@@ -0,0 +1,75 @@
1
+ use swc_core::common::FileName;
2
+
3
+ // Platform-specific path parsing functions
4
+ fn parse_unix_path(path: &str) -> Vec<&str> {
5
+ path.split('/').collect()
6
+ }
7
+
8
+ fn parse_windows_path(path: &str) -> Vec<&str> {
9
+ path.split('\\').collect()
10
+ }
11
+
12
+ // Fallback for mixed or unknown paths - checks the string content
13
+ fn parse_path_with_detection(path: &str) -> Vec<&str> {
14
+ if path.contains('\\') {
15
+ parse_windows_path(path)
16
+ } else {
17
+ parse_unix_path(path)
18
+ }
19
+ }
20
+
21
+ pub fn extract_filename(filename: &FileName) -> Option<String> {
22
+ match filename {
23
+ FileName::Real(path) => {
24
+ if let Some(file_name) = path.file_name().and_then(|name| name.to_str()) {
25
+ // Check if it's an index file
26
+ if file_name.starts_with("index.") {
27
+ // Get parent directory name and combine with filename
28
+ if let Some(parent) = path
29
+ .parent()
30
+ .and_then(|p| p.file_name())
31
+ .and_then(|n| n.to_str())
32
+ {
33
+ Some(format!("{}/{}", parent, file_name))
34
+ } else {
35
+ Some(file_name.to_string())
36
+ }
37
+ } else {
38
+ Some(file_name.to_string())
39
+ }
40
+ } else {
41
+ None
42
+ }
43
+ }
44
+ FileName::Custom(custom) => {
45
+ // Always use detection for Custom filenames since they can come from any platform
46
+ let parts = parse_path_with_detection(custom);
47
+
48
+ let file_part = if parts.len() > 1 {
49
+ parts.last().copied().unwrap_or(custom)
50
+ } else {
51
+ custom
52
+ };
53
+
54
+ // Check if it's an index file
55
+ if file_part.starts_with("index.") {
56
+ // Extract parent directory from the full path
57
+ let parent = if parts.len() >= 2 {
58
+ Some(parts[parts.len() - 2])
59
+ } else {
60
+ None
61
+ };
62
+
63
+ if let Some(parent_name) = parent {
64
+ // Always use forward slash in output for consistency
65
+ Some(format!("{}/{}", parent_name, file_part))
66
+ } else {
67
+ Some(file_part.to_string())
68
+ }
69
+ } else {
70
+ Some(file_part.to_string())
71
+ }
72
+ }
73
+ _ => None,
74
+ }
75
+ }
Binary file