mirror of
				https://gitea.invidious.io/iv-org/shard-ameba.git
				synced 2024-08-15 00:53:29 +00:00 
			
		
		
		
	Merge pull request #238 from caspiano/exhaustive-case-complexity
Count an exhaustive `case` complexity as 1
This commit is contained in:
		
						commit
						720810c3cb
					
				
					 2 changed files with 32 additions and 2 deletions
				
			
		|  | @ -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 | ||||
|  |  | |||
|  | @ -23,13 +23,24 @@ 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) | ||||
|       return true if macro_condition | ||||
| 
 | ||||
|       # Count the complexity of an exhaustive `Case` as 1 | ||||
|       # Otherwise count the number of `When`s | ||||
|       @complexity += node.exhaustive? ? 1 : node.whens.size | ||||
| 
 | ||||
|       true | ||||
|     end | ||||
| 
 | ||||
|     def visit(node : Crystal::MacroIf | Crystal::MacroFor) | ||||
|       @macro_condition = true | ||||
|       @complexity = DEFAULT_COMPLEXITY | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue