Skip to content

Commit 08afb21

Browse files
committed
fix(typescript): Ignore nullish defaults in complexity
1 parent 63d63bc commit 08afb21

1 file changed

Lines changed: 33 additions & 3 deletions

File tree

src/analyzers/typescript/visitor/helpers.rs

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,12 @@ fn traverse_for_cyclomatic(node: &Node, source: &str, complexity: &mut u32) {
3333
}
3434
"catch_clause" => *complexity += 1,
3535
"ternary_expression" | "conditional_expression" => *complexity += 1,
36-
"optional_chain_expression" => *complexity += 1,
3736

3837
// Logical operators create branches
3938
"binary_expression" => {
4039
if let Some(op) = node.child_by_field_name("operator") {
4140
let op_text = node_text(&op, source);
42-
if op_text == "&&" || op_text == "||" || op_text == "??" {
41+
if op_text == "&&" || op_text == "||" {
4342
*complexity += 1;
4443
}
4544
}
@@ -131,7 +130,7 @@ fn traverse_for_cognitive(node: &Node, source: &str, complexity: &mut u32, nesti
131130
"binary_expression" => {
132131
if let Some(op) = node.child_by_field_name("operator") {
133132
let op_text = node_text(&op, source);
134-
if op_text == "&&" || op_text == "||" || op_text == "??" {
133+
if op_text == "&&" || op_text == "||" {
135134
*complexity += 1;
136135
}
137136
}
@@ -430,6 +429,37 @@ mod tests {
430429
assert_eq!(complexity, 4); // 1 base + 1 if + 2 logical ops
431430
}
432431

432+
#[test]
433+
fn test_nullish_defaults_do_not_inflate_async_persistence_complexity() {
434+
let source = r#"
435+
async function persist(db, input) {
436+
await prepare(db);
437+
const artifact = input.artifact;
438+
const promptHash = artifact?.prompt ? await hash(artifact.prompt) : null;
439+
await db
440+
.prepare("insert into table values (?, ?, ?, ?, ?, ?)")
441+
.bind(
442+
artifact?.artifactId ?? null,
443+
artifact?.format ?? input.request?.format ?? null,
444+
artifact?.variant ?? input.request?.variant ?? null,
445+
input.request?.presetId ?? null,
446+
input.model ?? null,
447+
promptHash,
448+
)
449+
.run();
450+
return [
451+
artifact?.artifactId ?? null,
452+
artifact?.format ?? input.request?.format ?? null,
453+
];
454+
}
455+
"#;
456+
let (tree, source) = parse_and_get_body(source);
457+
let root = tree.root_node();
458+
459+
assert_eq!(calculate_cyclomatic_complexity(&root, &source), 2);
460+
assert_eq!(calculate_cognitive_complexity(&root, &source), 4);
461+
}
462+
433463
#[test]
434464
fn test_cognitive_simple_function() {
435465
let source = "function foo() { return 1; }";

0 commit comments

Comments
 (0)