use crate::models::{CallIntent, EntityKind, ParsedEntity};
use crate::pipeline::parser::utils::{is_capitalized, node_text};
use tree_sitter::Node;
use uuid::Uuid;
pub(crate) fn extract_jsx_component_invocation(
node: Node<'_>,
source: &[u8],
intents: &mut Vec<CallIntent>,
) {
let line = node.start_position().row + 1;
if let Some(name_node) = node.child_by_field_name("name") {
let comp_name = node_text(name_node, source);
if is_capitalized(&comp_name) {
if comp_name.contains('.') {
let mut parts = comp_name.split('.');
let receiver = parts.next().map(|s| s.to_string());
let method = parts.collect::<Vec<_>>().join(".");
intents.push(CallIntent {
method,
receiver,
line,
arg_count: None,
});
} else {
intents.push(CallIntent {
method: comp_name,
receiver: None,
line,
arg_count: None,
});
}
}
}
}
pub(crate) fn extract_jsx_attributes(
node: Node<'_>,
source: &[u8],
) -> Vec<(String, String, usize)> {
let mut attributes = Vec::new();
let mut child = node.child(0);
while let Some(c) = child {
if c.kind() == "jsx_attribute" {
let line = c.start_position().row + 1;
let mut attr_name = String::new();
let mut attr_value = String::new();
let mut attr_child = c.child(0);
while let Some(ac) = attr_child {
if ac.kind() == "property_identifier" {
attr_name = node_text(ac, source);
} else if ac.kind() == "string" {
let raw = node_text(ac, source);
attr_value = raw.trim_matches(|c| c == '"' || c == '\'').to_string();
} else if ac.kind() == "jsx_expression" {
attr_child = ac.next_sibling();
continue;
}
attr_child = ac.next_sibling();
}
if (attr_name == "id" || attr_name == "className") && !attr_value.is_empty() {
attributes.push((attr_name, attr_value, line));
}
}
child = c.next_sibling();
}
attributes
}
pub(crate) fn extract_jsx_html_attributes(
node: Node<'_>,
source: &[u8],
entities: &mut Vec<ParsedEntity>,
file_path: &str,
repo_name: &str,
) {
if matches!(
node.kind(),
"jsx_self_closing_element" | "jsx_opening_element"
) {
let attrs = extract_jsx_attributes(node, source);
let line = node.start_position().row + 1;
for (attr_name, attr_value, _) in attrs {
if attr_name == "id" {
entities.push(ParsedEntity {
uuid: Uuid::new_v4(),
name: attr_value.clone(),
kind: EntityKind::HtmlId,
fqn: format!("#{}", attr_value),
signature: None,
docstring: None,
inline_comments: Vec::new(),
decorators: Vec::new(),
language: "javascript".to_string(),
file_path: file_path.to_string(),
start_line: line,
end_line: line,
enclosing_class: None,
repo_name: repo_name.to_string(),
reference_intents: Vec::new(),
calls: Vec::new(),
relationships: Vec::new(),
embed_text: String::new(),
rust_attributes: None,
impl_trait: None,
impl_target: None,
generics: None,
lifetimes: None,
alias_module_path: None,
original_export_name: None,
enclosing_class_fqn: None,
default_export: None,
is_test_context: false,
});
} else if attr_name == "className" {
for class_name in attr_value.split_whitespace() {
if !class_name.is_empty() {
entities.push(ParsedEntity {
uuid: Uuid::new_v4(),
name: class_name.to_string(),
kind: EntityKind::HtmlClass,
fqn: format!(".{}", class_name),
signature: None,
docstring: None,
inline_comments: Vec::new(),
decorators: Vec::new(),
language: "javascript".to_string(),
file_path: file_path.to_string(),
start_line: line,
end_line: line,
enclosing_class: None,
repo_name: repo_name.to_string(),
reference_intents: Vec::new(),
calls: Vec::new(),
relationships: Vec::new(),
embed_text: String::new(),
rust_attributes: None,
impl_trait: None,
impl_target: None,
generics: None,
lifetimes: None,
alias_module_path: None,
original_export_name: None,
enclosing_class_fqn: None,
default_export: None,
is_test_context: false,
});
}
}
}
}
}
let mut child = node.child(0);
while let Some(c) = child {
extract_jsx_html_attributes(c, source, entities, file_path, repo_name);
child = c.next_sibling();
}
}