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
622 changes: 617 additions & 5 deletions crates/codegraph-core/src/complexity.rs

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions crates/codegraph-core/src/extractors/csharp.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use tree_sitter::{Node, Tree};
use crate::complexity::{compute_function_complexity, CSHARP_RULES};
use crate::complexity::compute_all_metrics;
use crate::types::*;
use super::helpers::*;
use super::SymbolExtractor;
Expand Down Expand Up @@ -104,7 +104,7 @@ fn walk_node(node: &Node, source: &[u8], symbols: &mut FileSymbols) {
line: start_line(&child),
end_line: Some(end_line(&child)),
decorators: None,
complexity: Some(compute_function_complexity(&child, &CSHARP_RULES)),
complexity: compute_all_metrics(&child, source, "c_sharp"),
});
}
}
Expand Down Expand Up @@ -141,7 +141,7 @@ fn walk_node(node: &Node, source: &[u8], symbols: &mut FileSymbols) {
line: start_line(node),
end_line: Some(end_line(node)),
decorators: None,
complexity: Some(compute_function_complexity(node, &CSHARP_RULES)),
complexity: compute_all_metrics(node, source, "c_sharp"),
});
}
}
Expand All @@ -160,7 +160,7 @@ fn walk_node(node: &Node, source: &[u8], symbols: &mut FileSymbols) {
line: start_line(node),
end_line: Some(end_line(node)),
decorators: None,
complexity: Some(compute_function_complexity(node, &CSHARP_RULES)),
complexity: compute_all_metrics(node, source, "c_sharp"),
});
}
}
Expand All @@ -179,7 +179,7 @@ fn walk_node(node: &Node, source: &[u8], symbols: &mut FileSymbols) {
line: start_line(node),
end_line: Some(end_line(node)),
decorators: None,
complexity: Some(compute_function_complexity(node, &CSHARP_RULES)),
complexity: compute_all_metrics(node, source, "c_sharp"),
});
}
}
Expand Down
6 changes: 3 additions & 3 deletions crates/codegraph-core/src/extractors/go.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use tree_sitter::{Node, Tree};
use crate::complexity::{compute_function_complexity, GO_RULES};
use crate::complexity::compute_all_metrics;
use crate::types::*;
use super::helpers::*;
use super::SymbolExtractor;
Expand All @@ -24,7 +24,7 @@ fn walk_node(node: &Node, source: &[u8], symbols: &mut FileSymbols) {
line: start_line(node),
end_line: Some(end_line(node)),
decorators: None,
complexity: Some(compute_function_complexity(node, &GO_RULES)),
complexity: compute_all_metrics(node, source, "go"),
});
}
}
Expand Down Expand Up @@ -60,7 +60,7 @@ fn walk_node(node: &Node, source: &[u8], symbols: &mut FileSymbols) {
line: start_line(node),
end_line: Some(end_line(node)),
decorators: None,
complexity: Some(compute_function_complexity(node, &GO_RULES)),
complexity: compute_all_metrics(node, source, "go"),
});
}
}
Expand Down
8 changes: 4 additions & 4 deletions crates/codegraph-core/src/extractors/java.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use tree_sitter::{Node, Tree};
use crate::complexity::{compute_function_complexity, JAVA_RULES};
use crate::complexity::compute_all_metrics;
use crate::types::*;
use super::helpers::*;
use super::SymbolExtractor;
Expand Down Expand Up @@ -110,7 +110,7 @@ fn walk_node(node: &Node, source: &[u8], symbols: &mut FileSymbols) {
line: start_line(&child),
end_line: Some(end_line(&child)),
decorators: None,
complexity: Some(compute_function_complexity(&child, &JAVA_RULES)),
complexity: compute_all_metrics(&child, source, "java"),
});
}
}
Expand Down Expand Up @@ -147,7 +147,7 @@ fn walk_node(node: &Node, source: &[u8], symbols: &mut FileSymbols) {
line: start_line(node),
end_line: Some(end_line(node)),
decorators: None,
complexity: Some(compute_function_complexity(node, &JAVA_RULES)),
complexity: compute_all_metrics(node, source, "java"),
});
}
}
Expand All @@ -166,7 +166,7 @@ fn walk_node(node: &Node, source: &[u8], symbols: &mut FileSymbols) {
line: start_line(node),
end_line: Some(end_line(node)),
decorators: None,
complexity: Some(compute_function_complexity(node, &JAVA_RULES)),
complexity: compute_all_metrics(node, source, "java"),
});
}
}
Expand Down
14 changes: 7 additions & 7 deletions crates/codegraph-core/src/extractors/javascript.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use tree_sitter::{Node, Tree};
use crate::complexity::{compute_function_complexity, JS_TS_RULES};
use crate::complexity::compute_all_metrics;
use crate::types::*;
use super::helpers::*;
use super::SymbolExtractor;
Expand All @@ -24,7 +24,7 @@ fn walk_node(node: &Node, source: &[u8], symbols: &mut FileSymbols) {
line: start_line(node),
end_line: Some(end_line(node)),
decorators: None,
complexity: Some(compute_function_complexity(node, &JS_TS_RULES)),
complexity: compute_all_metrics(node, source, "javascript"),
});
}
}
Expand Down Expand Up @@ -80,7 +80,7 @@ fn walk_node(node: &Node, source: &[u8], symbols: &mut FileSymbols) {
line: start_line(node),
end_line: Some(end_line(node)),
decorators: None,
complexity: Some(compute_function_complexity(node, &JS_TS_RULES)),
complexity: compute_all_metrics(node, source, "javascript"),
});
}
}
Expand Down Expand Up @@ -138,7 +138,7 @@ fn walk_node(node: &Node, source: &[u8], symbols: &mut FileSymbols) {
line: start_line(node),
end_line: Some(end_line(&value_n)),
decorators: None,
complexity: Some(compute_function_complexity(&value_n, &JS_TS_RULES)),
complexity: compute_all_metrics(&value_n, source, "javascript"),
});
}
}
Expand Down Expand Up @@ -562,7 +562,7 @@ fn extract_callback_definition(call_node: &Node, source: &[u8]) -> Option<Defini
line: start_line(&cb),
end_line: Some(end_line(&cb)),
decorators: None,
complexity: Some(compute_function_complexity(&cb, &JS_TS_RULES)),
complexity: compute_all_metrics(&cb, source, "javascript"),
});
}

Expand All @@ -579,7 +579,7 @@ fn extract_callback_definition(call_node: &Node, source: &[u8]) -> Option<Defini
line: start_line(&cb),
end_line: Some(end_line(&cb)),
decorators: None,
complexity: Some(compute_function_complexity(&cb, &JS_TS_RULES)),
complexity: compute_all_metrics(&cb, source, "javascript"),
});
}

Expand All @@ -593,7 +593,7 @@ fn extract_callback_definition(call_node: &Node, source: &[u8]) -> Option<Defini
line: start_line(&cb),
end_line: Some(end_line(&cb)),
decorators: None,
complexity: Some(compute_function_complexity(&cb, &JS_TS_RULES)),
complexity: compute_all_metrics(&cb, source, "javascript"),
});
}

Expand Down
8 changes: 4 additions & 4 deletions crates/codegraph-core/src/extractors/php.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use tree_sitter::{Node, Tree};
use crate::complexity::{compute_function_complexity, PHP_RULES};
use crate::complexity::compute_all_metrics;
use crate::types::*;
use super::helpers::*;
use super::SymbolExtractor;
Expand Down Expand Up @@ -40,7 +40,7 @@ fn walk_node(node: &Node, source: &[u8], symbols: &mut FileSymbols) {
line: start_line(node),
end_line: Some(end_line(node)),
decorators: None,
complexity: Some(compute_function_complexity(node, &PHP_RULES)),
complexity: compute_all_metrics(node, source, "php"),
});
}
}
Expand Down Expand Up @@ -122,7 +122,7 @@ fn walk_node(node: &Node, source: &[u8], symbols: &mut FileSymbols) {
line: start_line(&child),
end_line: Some(end_line(&child)),
decorators: None,
complexity: Some(compute_function_complexity(&child, &PHP_RULES)),
complexity: compute_all_metrics(&child, source, "php"),
});
}
}
Expand Down Expand Up @@ -172,7 +172,7 @@ fn walk_node(node: &Node, source: &[u8], symbols: &mut FileSymbols) {
line: start_line(node),
end_line: Some(end_line(node)),
decorators: None,
complexity: Some(compute_function_complexity(node, &PHP_RULES)),
complexity: compute_all_metrics(node, source, "php"),
});
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/codegraph-core/src/extractors/python.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use tree_sitter::{Node, Tree};
use crate::complexity::{compute_function_complexity, PYTHON_RULES};
use crate::complexity::compute_all_metrics;
use crate::types::*;
use super::helpers::*;
use super::SymbolExtractor;
Expand Down Expand Up @@ -40,7 +40,7 @@ fn walk_node(node: &Node, source: &[u8], symbols: &mut FileSymbols) {
} else {
Some(decorators)
},
complexity: Some(compute_function_complexity(node, &PYTHON_RULES)),
complexity: compute_all_metrics(node, source, "python"),
});
}
}
Expand Down
6 changes: 3 additions & 3 deletions crates/codegraph-core/src/extractors/ruby.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use tree_sitter::{Node, Tree};
use crate::complexity::{compute_function_complexity, RUBY_RULES};
use crate::complexity::compute_all_metrics;
use crate::types::*;
use super::helpers::*;
use super::SymbolExtractor;
Expand Down Expand Up @@ -77,7 +77,7 @@ fn walk_node(node: &Node, source: &[u8], symbols: &mut FileSymbols) {
line: start_line(node),
end_line: Some(end_line(node)),
decorators: None,
complexity: Some(compute_function_complexity(node, &RUBY_RULES)),
complexity: compute_all_metrics(node, source, "ruby"),
});
}
}
Expand All @@ -96,7 +96,7 @@ fn walk_node(node: &Node, source: &[u8], symbols: &mut FileSymbols) {
line: start_line(node),
end_line: Some(end_line(node)),
decorators: None,
complexity: Some(compute_function_complexity(node, &RUBY_RULES)),
complexity: compute_all_metrics(node, source, "ruby"),
});
}
}
Expand Down
6 changes: 3 additions & 3 deletions crates/codegraph-core/src/extractors/rust_lang.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use tree_sitter::{Node, Tree};
use crate::complexity::{compute_function_complexity, RUST_LANG_RULES};
use crate::complexity::compute_all_metrics;
use crate::types::*;
use super::helpers::*;
use super::SymbolExtractor;
Expand Down Expand Up @@ -43,7 +43,7 @@ fn walk_node(node: &Node, source: &[u8], symbols: &mut FileSymbols) {
line: start_line(node),
end_line: Some(end_line(node)),
decorators: None,
complexity: Some(compute_function_complexity(node, &RUST_LANG_RULES)),
complexity: compute_all_metrics(node, source, "rust"),
});
}
}
Expand Down Expand Up @@ -102,7 +102,7 @@ fn walk_node(node: &Node, source: &[u8], symbols: &mut FileSymbols) {
line: start_line(&child),
end_line: Some(end_line(&child)),
decorators: None,
complexity: Some(compute_function_complexity(&child, &RUST_LANG_RULES)),
complexity: compute_all_metrics(&child, source, "rust"),
});
}
}
Expand Down
45 changes: 45 additions & 0 deletions crates/codegraph-core/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,58 @@
use napi_derive::napi;
use serde::{Deserialize, Serialize};

#[napi(object)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HalsteadMetrics {
pub n1: u32,
pub n2: u32,
#[napi(js_name = "bigN1")]
pub big_n1: u32,
#[napi(js_name = "bigN2")]
pub big_n2: u32,
pub vocabulary: u32,
pub length: u32,
pub volume: f64,
pub difficulty: f64,
pub effort: f64,
pub bugs: f64,
}

#[napi(object)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LocMetrics {
pub loc: u32,
pub sloc: u32,
#[napi(js_name = "commentLines")]
pub comment_lines: u32,
}

#[napi(object)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ComplexityMetrics {
pub cognitive: u32,
pub cyclomatic: u32,
#[napi(js_name = "maxNesting")]
pub max_nesting: u32,
pub halstead: Option<HalsteadMetrics>,
pub loc: Option<LocMetrics>,
#[napi(js_name = "maintainabilityIndex")]
pub maintainability_index: Option<f64>,
}

impl ComplexityMetrics {
/// Construct a basic metrics result with only cognitive/cyclomatic/maxNesting.
/// Used by `compute_function_complexity` and existing tests.
pub fn basic(cognitive: u32, cyclomatic: u32, max_nesting: u32) -> Self {
Self {
cognitive,
cyclomatic,
max_nesting,
halstead: None,
loc: None,
maintainability_index: None,
}
}
}

#[napi(object)]
Expand Down
Loading