swc-plugin-component-annotate 1.0.0 → 1.1.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.1.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
@@ -14,6 +14,22 @@ use swc_core::{
14
14
  plugin::{plugin_transform, proxies::TransformPluginProgramMetadata},
15
15
  };
16
16
 
17
+ // Macro to efficiently determine path separator and extract components
18
+ macro_rules! path_info {
19
+ ($path:expr) => {{
20
+ let has_forward_slash = $path.contains('/');
21
+ let has_backslash = $path.contains('\\');
22
+
23
+ if has_forward_slash {
24
+ (true, false, '/', $path.split('/'))
25
+ } else if has_backslash {
26
+ (false, true, '\\', $path.split('\\'))
27
+ } else {
28
+ (false, false, '/', $path.split('/')) // Default to forward slash for single component
29
+ }
30
+ }};
31
+ }
32
+
17
33
  pub struct ReactComponentAnnotateVisitor {
18
34
  config: PluginConfig,
19
35
  source_file_name: Option<String>,
@@ -294,23 +310,64 @@ impl VisitMut for ReactComponentAnnotateVisitor {
294
310
 
295
311
  fn extract_filename(filename: &FileName) -> Option<String> {
296
312
  match filename {
297
- FileName::Real(path) => path
298
- .file_name()
299
- .and_then(|name| name.to_str())
300
- .map(|s| s.to_string()),
313
+ FileName::Real(path) => {
314
+ if let Some(file_name) = path.file_name().and_then(|name| name.to_str()) {
315
+ // Check if it's an index file
316
+ if file_name.starts_with("index.") {
317
+ // Get parent directory name and combine with filename
318
+ if let Some(parent) = path
319
+ .parent()
320
+ .and_then(|p| p.file_name())
321
+ .and_then(|n| n.to_str())
322
+ {
323
+ Some(format!("{}/{}", parent, file_name))
324
+ } else {
325
+ Some(file_name.to_string())
326
+ }
327
+ } else {
328
+ Some(file_name.to_string())
329
+ }
330
+ } else {
331
+ None
332
+ }
333
+ }
301
334
  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())
335
+ let (has_forward_slash, has_backslash, _separator, parts) = path_info!(custom);
336
+ let parts_vec: Vec<&str> = parts.collect();
337
+
338
+ let file_part = if has_forward_slash || has_backslash {
339
+ parts_vec.last().copied().unwrap_or(custom)
306
340
  } else {
307
- Some(custom.clone())
341
+ custom
342
+ };
343
+
344
+ // Check if it's an index file
345
+ if file_part.starts_with("index.") {
346
+ // Extract parent directory from the full path
347
+ let parent = if (has_forward_slash || has_backslash) && parts_vec.len() >= 2 {
348
+ Some(parts_vec[parts_vec.len() - 2])
349
+ } else {
350
+ None
351
+ };
352
+
353
+ if let Some(parent_name) = parent {
354
+ Some(format!("{}/{}", parent_name, file_part))
355
+ } else {
356
+ Some(file_part.to_string())
357
+ }
358
+ } else {
359
+ Some(file_part.to_string())
308
360
  }
309
361
  }
310
362
  _ => None,
311
363
  }
312
364
  }
313
365
 
366
+ // Export for testing
367
+ pub fn extract_filename_for_test(filename: &FileName) -> Option<String> {
368
+ extract_filename(filename)
369
+ }
370
+
314
371
  #[plugin_transform]
315
372
  pub fn process_transform(
316
373
  mut program: Program,
Binary file