use crate::models::{CallIntent, EntityKind, ParsedEntity};
use crate::pipeline::parser::utils::{is_capitalized, node_text};
use tree_sitter::Node;
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::new(
attr_value.clone(),
EntityKind::HtmlId,
format!("#{}", attr_value),
None,
None,
"javascript",
file_path,
line,
line,
None,
repo_name,
));
} else if attr_name == "className" {
for class_name in attr_value.split_whitespace() {
if !class_name.is_empty() {
entities.push(ParsedEntity::new(
class_name,
EntityKind::HtmlClass,
format!(".{}", class_name),
None,
None,
"javascript",
file_path,
line,
line,
None,
repo_name,
));
}
}
}
}
}
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();
}
}
#[cfg(test)]
mod tests {
use super::*;
use tree_sitter::Parser;
#[test]
fn test_jsx_markup_entities_uuids_are_deterministic() {
let mut parser = Parser::new();
let language = tree_sitter_javascript::LANGUAGE;
parser
.set_language(&language.into())
.expect("Error loading JS grammar");
let source = r#"
const App = () => <div id="app-id" className="cls1 cls2" />;
"#;
let tree1 = parser.parse(source, None).unwrap();
let mut entities1 = Vec::new();
extract_jsx_html_attributes(
tree1.root_node(),
source.as_bytes(),
&mut entities1,
"App.jsx",
"test_repo",
);
let tree2 = parser.parse(source, None).unwrap();
let mut entities2 = Vec::new();
extract_jsx_html_attributes(
tree2.root_node(),
source.as_bytes(),
&mut entities2,
"App.jsx",
"test_repo",
);
assert_eq!(entities1.len(), 3); assert_eq!(entities1.len(), entities2.len());
for (e1, e2) in entities1.iter().zip(entities2.iter()) {
assert_eq!(
e1.uuid, e2.uuid,
"UUID should be deterministic for JSX entity {}",
e1.name
);
}
}
}