Skip to content

Commit

Permalink
Merge pull request #44 from rohaquinlop/issue-40
Browse files Browse the repository at this point in the history
feat(back): #40 update cognitive complexity algorithm
  • Loading branch information
rohaquinlop authored Jun 21, 2024
2 parents 0634bd6 + 37e4ac0 commit 5df86cb
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 34 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "complexipy"
version = "0.3.3"
version = "0.4.0"
edition = "2021"
authors = ["Robin Quintero <[email protected]>"]
license = "MIT"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ The cognitive complexity of the file is 1, and the output of the command
`complexipy path/to/file.py` will be:

```txt
───────────────────────────── 🐙 complexipy 0.3.3 ──────────────────────────────
───────────────────────────── 🐙 complexipy 0.4.0 ──────────────────────────────
Summary
┏━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━┳━━━━━━━━━━━━┓
┃ Path ┃ File ┃ Function ┃ Complexity ┃
Expand Down
2 changes: 1 addition & 1 deletion complexipy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
root_dir = Path(__file__).resolve().parent.parent
app = typer.Typer(name="complexipy")
console = Console()
version = "0.3.3"
version = "0.4.0"


@app.command()
Expand Down
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ The cognitive complexity of the file is 1, and the output of the command
`complexipy path/to/file.py` will be:

```txt
───────────────────────────── 🐙 complexipy 0.3.3 ──────────────────────────────
───────────────────────────── 🐙 complexipy 0.4.0 ──────────────────────────────
Summary
┏━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━┳━━━━━━━━━━━━┓
┃ Path ┃ File ┃ Function ┃ Complexity ┃
Expand Down
37 changes: 17 additions & 20 deletions src/cognitive_complexity/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,47 +271,44 @@ fn statement_cognitive_complexity(statement: Stmt, nesting_level: u64) -> PyResu
}
}
Stmt::Assign(a) => {
match *a.value {
ast::Expr::IfExp(..) => {
complexity += count_bool_ops(*a.value);
}
_ => {}
}

if complexity > 0 {
complexity += nesting_level;
}
complexity += count_bool_ops(*a.value, nesting_level);
}
Stmt::For(f) => {
complexity += 1 + nesting_level;
for node in f.body.iter() {
complexity += statement_cognitive_complexity(node.clone(), nesting_level + 1)?;
}

complexity += count_bool_ops(*f.iter, nesting_level);
}
Stmt::While(w) => {
complexity += 1 + nesting_level;
complexity += count_bool_ops(*w.test);
complexity += count_bool_ops(*w.test, nesting_level);
for node in w.body.iter() {
complexity += statement_cognitive_complexity(node.clone(), nesting_level + 1)?;
}
}
Stmt::If(i) => {
complexity += 1 + nesting_level;
complexity += count_bool_ops(*i.test);
complexity += count_bool_ops(*i.test, nesting_level);
for node in i.body.iter() {
complexity += statement_cognitive_complexity(node.clone(), nesting_level + 1)?;
}

let mut orelse_complexity: u64 = 0;
for node in i.orelse.iter() {
orelse_complexity += statement_cognitive_complexity(node.clone(), nesting_level)?;
}
let orelse_complexities: Vec<u64> = i
.orelse
.iter()
.map(|node| statement_cognitive_complexity(node.clone(), nesting_level))
.filter(|complexity| complexity.is_ok())
.filter(|complexity| *complexity.as_ref().unwrap() > 0)
.map(|complexity| complexity.unwrap())
.collect();

if orelse_complexity > 0 && i.orelse.len() > 0 {
complexity -= (nesting_level) * i.orelse.len() as u64;
let orelse_complexity: u64 = orelse_complexities.iter().sum();

if orelse_complexities.len() > 0 {
complexity += orelse_complexity;
} else if i.orelse.len() > 0 {
complexity += 1;
complexity -= (nesting_level) * orelse_complexities.len() as u64;
}
}
Stmt::Try(t) => {
Expand Down
48 changes: 39 additions & 9 deletions src/cognitive_complexity/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,30 +130,60 @@ pub fn is_decorator(statement: Stmt) -> bool {
ans
}

pub fn count_bool_ops(expr: ast::Expr) -> u64 {
pub fn count_bool_ops(expr: ast::Expr, nesting_level: u64) -> u64 {
let mut complexity: u64 = 0;

match expr {
ast::Expr::BoolOp(b) => {
complexity += 1;
for value in b.values.iter() {
complexity += count_bool_ops(value.clone());
complexity += count_bool_ops(value.clone(), nesting_level);
}
}
ast::Expr::UnaryOp(u) => {
complexity += count_bool_ops(*u.operand);
complexity += count_bool_ops(*u.operand, nesting_level);
}
ast::Expr::Compare(c) => {
complexity += count_bool_ops(*c.left);
complexity += count_bool_ops(*c.left, nesting_level);
for comparator in c.comparators.iter() {
complexity += count_bool_ops(comparator.clone());
complexity += count_bool_ops(comparator.clone(), nesting_level);
}
}
ast::Expr::IfExp(i) => {
complexity += 1;
complexity += count_bool_ops(*i.test);
complexity += count_bool_ops(*i.body);
complexity += count_bool_ops(*i.orelse);
complexity += 1 + nesting_level;
complexity += count_bool_ops(*i.test, nesting_level);
complexity += count_bool_ops(*i.body, nesting_level);
complexity += count_bool_ops(*i.orelse, nesting_level);
}
ast::Expr::Call(c) => {
for arg in c.args.iter() {
complexity += count_bool_ops(arg.clone(), nesting_level);
}
}
ast::Expr::Tuple(t) => {
for element in t.elts.iter() {
complexity += count_bool_ops(element.clone(), nesting_level);
}
}
ast::Expr::List(l) => {
for element in l.elts.iter() {
complexity += count_bool_ops(element.clone(), nesting_level);
}
}
ast::Expr::Set(s) => {
for element in s.elts.iter() {
complexity += count_bool_ops(element.clone(), nesting_level);
}
}
ast::Expr::Dict(d) => {
for key in d.keys.iter() {
if let Some(key_value) = key {
complexity += count_bool_ops(key_value.clone(), nesting_level);
}
}
for value in d.values.iter() {
complexity += count_bool_ops(value.clone(), nesting_level);
}
}
_ => {}
}
Expand Down

0 comments on commit 5df86cb

Please sign in to comment.