Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion ast/src/lang/call_finder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,9 @@ fn find_nested_function_in_variable<G: Graph>(

for func in func_nodes.clone() {
if let Some(nested_in) = func.meta.get("nested_in") {
if nested_in == var_name {
let n = crate::lang::parse::utils::trim_quotes(nested_in);
let v = crate::lang::parse::utils::trim_quotes(var_name);
if n == v {
return Some(func);
}
}
Expand Down
10 changes: 4 additions & 6 deletions ast/src/lang/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub mod linker;
pub mod parse;
pub mod queries;

use crate::lang::parse::utils::trim_quotes;
pub use asg::NodeData;
use asg::*;
use consts::*;
Expand Down Expand Up @@ -500,8 +501,7 @@ impl Lang {
self.collect_functions(&qo, code, file, graph, lsp_tx, &identified_tests)?;
self.attach_function_comments(code, &mut funcs1)?;

let mut func_nodes: Vec<NodeData> = funcs1.iter().map(|f| f.0.clone()).collect();
let nested_pairs = self.find_nested_functions(&func_nodes);
let nested_pairs = self.find_nested_functions(&funcs1);

let mut nested_edges_by_child: std::collections::HashMap<NodeKeys, Vec<Edge>> =
std::collections::HashMap::new();
Expand All @@ -518,8 +518,7 @@ impl Lang {
}

let all_variables = graph.find_nodes_by_type(NodeType::Var);
let var_nested_pairs =
self.find_functions_nested_in_variables(&mut func_nodes, &all_variables);
let var_nested_pairs = self.find_functions_nested_in_variables(&mut funcs1, &all_variables);
for (func, var) in var_nested_pairs {
let edge = Edge::new(
EdgeType::NestedIn,
Expand Down Expand Up @@ -669,7 +668,7 @@ impl Lang {
let mut attributes = Vec::new();
Self::loop_captures(&q_tests, tm, code, |body, node, o| {
if o == FUNCTION_NAME {
caller_name = body;
caller_name = trim_quotes(&body).to_string();
} else if o == ATTRIBUTES {
attributes.push(body);
} else if o == FUNCTION_DEFINITION {
Expand All @@ -686,7 +685,6 @@ impl Lang {
lsp_tx,
graph.get_allow_unverified_calls(),
)?;
// Combine attributes and body for accurate test detection
let full_body = if !attributes.is_empty() {
format!("{} {}", attributes.join(" "), body)
} else {
Expand Down
36 changes: 15 additions & 21 deletions ast/src/lang/parse/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,8 @@ impl Lang {

if !targets.is_empty() {
let target = targets
.iter().find(|node_data| node_data.file.contains(&resolved_path));
.iter()
.find(|node_data| node_data.file.contains(&resolved_path));

let file_nodes =
graph.find_nodes_by_file_ends_with(NodeType::File, file);
Expand Down Expand Up @@ -540,7 +541,8 @@ impl Lang {
let all_vars = graph.find_nodes_by_type(NodeType::Var);

let imports = graph.find_nodes_by_file_ends_with(NodeType::Import, &func.file);
let import_body = imports.first()
let import_body = imports
.first()
.map(|imp| imp.body.clone())
.unwrap_or_default();

Expand Down Expand Up @@ -637,12 +639,7 @@ impl Lang {
.iter()
.find(|v| target_file.ends_with(&v.file))
{
edges.push(Edge::contains(
NodeType::Function,
func,
NodeType::Var,
var,
));
edges.push(Edge::contains(NodeType::Function, func, NodeType::Var, var));
processed.insert(key);
}
}
Expand Down Expand Up @@ -676,16 +673,16 @@ impl Lang {
}
pub fn find_nested_functions<'a>(
&self,
functions: &'a [NodeData],
functions: &'a [Function],
) -> Vec<(&'a NodeData, &'a NodeData)> {
let mut nested = Vec::new();
for child in functions {
for parent in functions {
if std::ptr::eq(child, parent) {
if std::ptr::eq(&child.0, &parent.0) {
continue;
}
if child.start > parent.start && child.end < parent.end {
nested.push((child, parent));
if child.0.start > parent.0.start && child.0.end < parent.0.end {
nested.push((&child.0, &parent.0));
}
}
}
Expand All @@ -694,7 +691,7 @@ impl Lang {

pub fn find_functions_nested_in_variables<'a>(
&self,
functions: &'a mut [NodeData],
functions: &'a mut [Function],
variables: &'a [NodeData],
) -> Vec<(&'a NodeData, &'a NodeData)> {
use std::collections::HashMap;
Expand All @@ -703,18 +700,15 @@ impl Lang {

let mut var_index: HashMap<String, Vec<&NodeData>> = HashMap::new();
for var in variables {
var_index
.entry(var.file.clone())
.or_default()
.push(var);
var_index.entry(var.file.clone()).or_default().push(var);
}

for func in functions.iter_mut() {
if let Some(vars_in_file) = var_index.get(&func.file) {
if let Some(vars_in_file) = var_index.get(&func.0.file) {
for var in vars_in_file {
if func.start > var.start && func.end < var.end {
func.add_nested_in(&var.name);
nested.push((func as &NodeData, var as &NodeData));
if func.0.start > var.start && func.0.end < var.end {
func.0.add_nested_in(&var.name);
nested.push((&func.0 as &NodeData, var as &NodeData));
break;
}
}
Expand Down
26 changes: 13 additions & 13 deletions ast/src/testing/coverage/nextjs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ async fn test_btreemap_graph_structure() -> Result<()> {
assert_eq!(endpoints.len(), 21);

let integration_tests = btree_graph.find_nodes_by_type(NodeType::IntegrationTest);
assert_eq!(integration_tests.len(), 18);
assert_eq!(integration_tests.len(), 19);

let test_edges = btree_graph.find_nodes_with_edge_type(
NodeType::IntegrationTest,
Expand Down Expand Up @@ -213,8 +213,8 @@ async fn test_nextjs_graph_upload() -> Result<()> {
let graph_ops = setup_nextjs_graph().await?;
let (nodes, edges) = graph_ops.get_graph_size().await?;

assert_eq!(nodes, 527);
assert_eq!(edges, 874);
assert_eq!(nodes, 564);
assert_eq!(edges, 962);

Ok(())
}
Expand All @@ -230,7 +230,7 @@ async fn test_coverage_default_params() -> Result<()> {

if let Some(integration) = &coverage.integration_tests {
assert_eq!(integration.total, 21);
assert_eq!(integration.total_tests, 18);
assert_eq!(integration.total_tests, 19);
}

Ok(())
Expand Down Expand Up @@ -388,8 +388,8 @@ async fn test_nodes_function_type() -> Result<()> {
)
.await?;

assert_eq!(count, 38);
assert_eq!(results.len(), 38);
assert_eq!(count, 49);
assert_eq!(results.len(), 49);

for (node_type, _, _, _, _, _, _, _, _) in &results {
assert_eq!(*node_type, NodeType::Function);
Expand Down Expand Up @@ -446,8 +446,8 @@ async fn test_nodes_integration_test_type() -> Result<()> {
)
.await?;

assert_eq!(count, 18);
assert_eq!(results.len(), 18);
assert_eq!(count, 19);
assert_eq!(results.len(), 19);

Ok(())
}
Expand All @@ -473,8 +473,8 @@ async fn test_nodes_unit_test_type() -> Result<()> {
)
.await?;

assert_eq!(count, 25);
assert_eq!(results.len(), 25);
assert_eq!(count, 27);
assert_eq!(results.len(), 27);

Ok(())
}
Expand Down Expand Up @@ -527,7 +527,7 @@ async fn test_nodes_multi_type() -> Result<()> {
)
.await?;

assert_eq!(count, 59);
assert_eq!(count, 70);

let has_function = results
.iter()
Expand Down Expand Up @@ -567,8 +567,8 @@ async fn test_nodes_all_test_types() -> Result<()> {
)
.await?;

assert_eq!(count, 48);
assert_eq!(results.len(), 48);
assert_eq!(count, 51);
assert_eq!(results.len(), 51);

Ok(())
}
Expand Down
18 changes: 9 additions & 9 deletions ast/src/testing/coverage/react.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ async fn test_btreemap_graph_structure() -> Result<()> {
assert_eq!(endpoints.len(), 5);

let functions = graph.find_nodes_by_type(NodeType::Function);
assert_eq!(functions.len(), 52);
assert_eq!(functions.len(), 56);

let unit_tests = graph.find_nodes_by_type(NodeType::UnitTest);
assert_eq!(unit_tests.len(), 3);
Expand All @@ -82,7 +82,7 @@ async fn test_btreemap_graph_structure() -> Result<()> {
assert_eq!(classes.len(), 4);

let data_models = graph.find_nodes_by_type(NodeType::DataModel);
assert_eq!(data_models.len(), 22);
assert_eq!(data_models.len(), 25);

let pages = graph.find_nodes_by_type(NodeType::Page);
assert_eq!(pages.len(), 4);
Expand All @@ -109,7 +109,7 @@ async fn test_btreemap_edges() -> Result<()> {
assert_eq!(renders_edges, 4);

let contains_edges = graph.count_edges_of_type(EdgeType::Contains);
assert_eq!(contains_edges, 208);
assert_eq!(contains_edges, 218);

let handler_edges = graph.count_edges_of_type(EdgeType::Handler);
assert_eq!(handler_edges, 5);
Expand All @@ -134,8 +134,8 @@ async fn test_react_graph_upload() -> Result<()> {
// Requests(14) + Pages(4) + Variables(7) + DataModels(22) + Endpoints(5)
// UnitTests(3) + IntegrationTests(2) + E2eTests(2) + Files(~11 TSX + others) + Dirs(14)
// Total approx 187+. We will adjust based on actual test run.
assert_eq!(nodes, 200);
assert_eq!(edges, 256); // Derived from previous test runs matches actual output
assert_eq!(nodes, 209);
assert_eq!(edges, 266); // Derived from previous test runs matches actual output

Ok(())
}
Expand Down Expand Up @@ -280,8 +280,8 @@ async fn test_nodes_function_type() -> Result<()> {
// due to unique_functions_filters (component=true or operand=true).
// We will adjust this assertion after the first run if needed.
// We expect 52 functions in BTreeMap, but Neo4j query filters components/operands
assert_eq!(count, 23);
assert_eq!(results.len(), 23);
assert_eq!(count, 26);
assert_eq!(results.len(), 26);

for (node_type, _, _, _, _, _, _, _, _) in &results {
assert_eq!(*node_type, NodeType::Function);
Expand Down Expand Up @@ -365,8 +365,8 @@ async fn test_nodes_data_model_type() -> Result<()> {
)
.await?;

assert_eq!(count, 22);
assert_eq!(results.len(), 22);
assert_eq!(count, 25);
assert_eq!(results.len(), 25);

Ok(())
}
Expand Down
34 changes: 17 additions & 17 deletions ast/src/testing/coverage/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ async fn test_btreemap_graph_structure() -> Result<()> {
assert_eq!(endpoints.len(), 22);

let functions = graph.find_nodes_by_type(NodeType::Function);
assert_eq!(functions.len(), 33);
assert_eq!(functions.len(), 36);

let unit_tests = graph.find_nodes_by_type(NodeType::UnitTest);
assert_eq!(unit_tests.len(), 9);
Expand All @@ -79,13 +79,13 @@ async fn test_btreemap_graph_structure() -> Result<()> {
assert_eq!(e2e_tests.len(), 4);

let classes = graph.find_nodes_by_type(NodeType::Class);
assert_eq!(classes.len(), 7);
assert_eq!(classes.len(), 9);

let data_models = graph.find_nodes_by_type(NodeType::DataModel);
assert_eq!(data_models.len(), 17);
assert_eq!(data_models.len(), 27);

let traits = graph.find_nodes_by_type(NodeType::Trait);
assert_eq!(traits.len(), 4);
assert_eq!(traits.len(), 6);

Ok(())
}
Expand All @@ -109,7 +109,7 @@ async fn test_btreemap_test_to_function_edges() -> Result<()> {
assert_eq!(calls_edges, 10);

let contains_edges = graph.count_edges_of_type(EdgeType::Contains);
assert_eq!(contains_edges, 162);
assert_eq!(contains_edges, 180);

let handler_edges = graph.count_edges_of_type(EdgeType::Handler);
assert_eq!(handler_edges, 22);
Expand All @@ -126,8 +126,8 @@ async fn test_typescript_graph_upload() -> Result<()> {
let graph_ops = setup_typescript_graph().await?;
let (nodes, edges) = graph_ops.get_graph_size().await?;

assert_eq!(nodes, 172);
assert_eq!(edges, 213);
assert_eq!(nodes, 190);
assert_eq!(edges, 231);

Ok(())
}
Expand Down Expand Up @@ -303,8 +303,8 @@ async fn test_nodes_function_type() -> Result<()> {
)
.await?;

assert_eq!(count, 12);
assert_eq!(results.len(), 12);
assert_eq!(count, 15);
assert_eq!(results.len(), 15);

for (node_type, _, _, _, _, _, _, _, _) in &results {
assert_eq!(*node_type, NodeType::Function);
Expand Down Expand Up @@ -334,8 +334,8 @@ async fn test_nodes_class_type() -> Result<()> {
)
.await?;

assert_eq!(count, 7);
assert_eq!(results.len(), 7);
assert_eq!(count, 9);
assert_eq!(results.len(), 9);

Ok(())
}
Expand All @@ -361,8 +361,8 @@ async fn test_nodes_data_model_type() -> Result<()> {
)
.await?;

assert_eq!(count, 17);
assert_eq!(results.len(), 17);
assert_eq!(count, 27);
assert_eq!(results.len(), 27);

Ok(())
}
Expand All @@ -388,8 +388,8 @@ async fn test_nodes_trait_type() -> Result<()> {
)
.await?;

assert_eq!(count, 4);
assert_eq!(results.len(), 4);
assert_eq!(count, 6);
assert_eq!(results.len(), 6);

Ok(())
}
Expand Down Expand Up @@ -496,7 +496,7 @@ async fn test_nodes_multi_type() -> Result<()> {
)
.await?;

assert_eq!(count, 34);
assert_eq!(count, 37);

let has_function = results
.iter()
Expand Down Expand Up @@ -932,7 +932,7 @@ async fn test_nodes_with_repo_filter() -> Result<()> {
)
.await?;

assert_eq!(count, 12);
assert_eq!(count, 15);

let (empty_count, _) = graph_ops
.query_nodes_with_count(
Expand Down
Loading
Loading