From 0ea6238dc67017b3a22f258d2bb52a98001d7d20 Mon Sep 17 00:00:00 2001 From: Caspian Baska Date: Sat, 5 Jun 2021 21:29:57 +1000 Subject: [PATCH 1/3] Count an exhaustive `case`'s complexity as 1 --- .../ast/visitors/counting_visitor_spec.cr | 21 ++++++++++++++++++- src/ameba/ast/visitors/counting_visitor.cr | 11 +++++++++- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/spec/ameba/ast/visitors/counting_visitor_spec.cr b/spec/ameba/ast/visitors/counting_visitor_spec.cr index 5ee964ae..9a7f485f 100644 --- a/spec/ameba/ast/visitors/counting_visitor_spec.cr +++ b/spec/ameba/ast/visitors/counting_visitor_spec.cr @@ -44,14 +44,33 @@ module Ameba::AST visitor.count.should eq 1 end + it "increases count for every exhaustive case" do + code = %( + def hello(a : Int32 | Int64 | Float32 | Float64) + case a + in Int32 then "int32" + in Int64 then "int64" + in Float32 then "float32" + in Float64 then "float64" + end + end + ) + node = Crystal::Parser.new(code).parse + visitor = CountingVisitor.new node + visitor.count.should eq 2 + end + {% for pair in [ {code: "if true; end", description: "conditional"}, {code: "while true; end", description: "while loop"}, {code: "until 1 < 2; end", description: "until loop"}, {code: "begin; rescue; end", description: "rescue"}, - {code: "case 1 when 1; end", description: "when"}, {code: "true || false", description: "or"}, {code: "true && false", description: "and"}, + { + code: "a : String | Int32 = 1; case a when true; end", + description: "inexhaustive when", + }, ] %} it "increases count for every {{ pair[:description].id }}" do node = Crystal::Parser.new("def hello; {{ pair[:code].id }} end").parse diff --git a/src/ameba/ast/visitors/counting_visitor.cr b/src/ameba/ast/visitors/counting_visitor.cr index 24e560e1..2ab64231 100644 --- a/src/ameba/ast/visitors/counting_visitor.cr +++ b/src/ameba/ast/visitors/counting_visitor.cr @@ -23,13 +23,22 @@ module Ameba::AST # Uses the same logic than rubocop. See # https://github.com/rubocop-hq/rubocop/blob/master/lib/rubocop/cop/metrics/cyclomatic_complexity.rb#L21 # Except "for", because crystal doesn't have a "for" loop. - {% for node in %i(if while until rescue when or and) %} + {% for node in %i(if while until rescue or and) %} # :nodoc: def visit(node : Crystal::{{ node.id.capitalize }}) @complexity += 1 unless macro_condition end {% end %} + # :nodoc: + def visit(node : Crystal::Case) + # Count the complexity of an exhaustive `Case` as 1 + # Otherwise count the number of `When`s + case_complexity = node.exhaustive? ? 1 : node.whens.size + @complexity += case_complexity unless macro_condition + true + end + def visit(node : Crystal::MacroIf | Crystal::MacroFor) @macro_condition = true @complexity = DEFAULT_COMPLEXITY From c49c69ed9f2c285db7aded15bef86296986deeb8 Mon Sep 17 00:00:00 2001 From: Caspian Baska Date: Sun, 6 Jun 2021 04:12:56 +1000 Subject: [PATCH 2/3] Early return in CountVisitor if macro node Co-authored-by: Sijawusz Pur Rahnama --- src/ameba/ast/visitors/counting_visitor.cr | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/ameba/ast/visitors/counting_visitor.cr b/src/ameba/ast/visitors/counting_visitor.cr index 2ab64231..896baa64 100644 --- a/src/ameba/ast/visitors/counting_visitor.cr +++ b/src/ameba/ast/visitors/counting_visitor.cr @@ -32,10 +32,12 @@ module Ameba::AST # :nodoc: def visit(node : Crystal::Case) + return true if macro_condition + # Count the complexity of an exhaustive `Case` as 1 # Otherwise count the number of `When`s - case_complexity = node.exhaustive? ? 1 : node.whens.size - @complexity += case_complexity unless macro_condition + @complexity += node.exhaustive? ? 1 : node.whens.size + true end From 651f70f68059a77896635e38d3723b3785a0bb4e Mon Sep 17 00:00:00 2001 From: Sijawusz Pur Rahnama Date: Sat, 5 Jun 2021 20:49:00 +0200 Subject: [PATCH 3/3] Update spec/ameba/ast/visitors/counting_visitor_spec.cr --- spec/ameba/ast/visitors/counting_visitor_spec.cr | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/ameba/ast/visitors/counting_visitor_spec.cr b/spec/ameba/ast/visitors/counting_visitor_spec.cr index 9a7f485f..db121152 100644 --- a/spec/ameba/ast/visitors/counting_visitor_spec.cr +++ b/spec/ameba/ast/visitors/counting_visitor_spec.cr @@ -48,8 +48,8 @@ module Ameba::AST code = %( def hello(a : Int32 | Int64 | Float32 | Float64) case a - in Int32 then "int32" - in Int64 then "int64" + in Int32 then "int32" + in Int64 then "int64" in Float32 then "float32" in Float64 then "float64" end