swc-plugin-component-annotate 1.7.0 → 1.8.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.7.0",
3
+ "version": "1.8.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/config.rs CHANGED
@@ -21,6 +21,10 @@ pub struct PluginConfig {
21
21
  /// Custom source file attribute name (overrides default and native setting)
22
22
  #[serde(default, rename = "source-file-attr")]
23
23
  pub source_file_attr: Option<String>,
24
+
25
+ /// Custom source path attribute name (overrides default and native setting)
26
+ #[serde(default, rename = "source-path-attr")]
27
+ pub source_path_attr: Option<String>,
24
28
  }
25
29
 
26
30
  impl PluginConfig {
@@ -53,4 +57,14 @@ impl PluginConfig {
53
57
  "data-source-file"
54
58
  }
55
59
  }
60
+
61
+ pub fn source_path_attr_name(&self) -> &str {
62
+ if let Some(ref custom) = self.source_path_attr {
63
+ custom
64
+ } else if self.native {
65
+ "dataSourcePath"
66
+ } else {
67
+ "data-source-path"
68
+ }
69
+ }
56
70
  }
package/src/lib.rs CHANGED
@@ -5,7 +5,7 @@ pub mod path_utils;
5
5
 
6
6
  use config::PluginConfig;
7
7
  use jsx_utils::*;
8
- use path_utils::extract_filename;
8
+ use path_utils::{extract_absolute_path, extract_filename};
9
9
  use rustc_hash::FxHashSet;
10
10
  use swc_core::{
11
11
  common::FileName,
@@ -22,6 +22,7 @@ use swc_core::{
22
22
  pub struct ReactComponentAnnotateVisitor {
23
23
  config: PluginConfig,
24
24
  source_file_name: Option<String>,
25
+ source_file_path: Option<String>,
25
26
  current_component_name: Option<String>,
26
27
  ignored_elements: FxHashSet<&'static str>,
27
28
  ignored_components_set: FxHashSet<String>,
@@ -30,6 +31,7 @@ pub struct ReactComponentAnnotateVisitor {
30
31
  impl ReactComponentAnnotateVisitor {
31
32
  pub fn new(config: PluginConfig, filename: &FileName) -> Self {
32
33
  let source_file_name = extract_filename(filename);
34
+ let source_file_path = extract_absolute_path(filename);
33
35
 
34
36
  // Pre-compute ignored components set for O(1) lookups
35
37
  let ignored_components_set: FxHashSet<String> =
@@ -38,6 +40,7 @@ impl ReactComponentAnnotateVisitor {
38
40
  Self {
39
41
  config,
40
42
  source_file_name,
43
+ source_file_path,
41
44
  current_component_name: None,
42
45
  ignored_elements: constants::default_ignored_elements(),
43
46
  ignored_components_set,
@@ -155,6 +158,20 @@ impl ReactComponentAnnotateVisitor {
155
158
  ));
156
159
  }
157
160
  }
161
+
162
+ // Add source path attribute (only if explicitly configured)
163
+ if self.config.source_path_attr.is_some() {
164
+ if let Some(ref source_path) = self.source_file_path {
165
+ if (self.current_component_name.is_some() || !is_ignored_html)
166
+ && !has_attribute(opening_element, self.config.source_path_attr_name())
167
+ {
168
+ opening_element.attrs.push(create_jsx_attr(
169
+ self.config.source_path_attr_name(),
170
+ source_path,
171
+ ));
172
+ }
173
+ }
174
+ }
158
175
  }
159
176
 
160
177
  fn find_jsx_in_function_body(&mut self, func: &mut Function, component_name: String) {
package/src/path_utils.rs CHANGED
@@ -18,6 +18,14 @@ fn parse_path_with_detection(path: &str) -> Vec<&str> {
18
18
  }
19
19
  }
20
20
 
21
+ pub fn extract_absolute_path(filename: &FileName) -> Option<String> {
22
+ match filename {
23
+ FileName::Real(path) => path.to_str().map(|s| s.to_string()),
24
+ FileName::Custom(custom) => Some(custom.clone()),
25
+ _ => None,
26
+ }
27
+ }
28
+
21
29
  pub fn extract_filename(filename: &FileName) -> Option<String> {
22
30
  match filename {
23
31
  FileName::Real(path) => {
Binary file