Class: Hashira::Complexity::CognitiveScore
- Inherits:
-
Object
- Object
- Hashira::Complexity::CognitiveScore
- Defined in:
- lib/hashira/complexity/cognitive_score.rb
Constant Summary collapse
- HANDLERS =
{ Prism::IfNode => :on_if, Prism::UnlessNode => :on_nester, Prism::WhileNode => :on_nester, Prism::UntilNode => :on_nester, Prism::ForNode => :on_nester, Prism::CaseNode => :on_nester, Prism::CaseMatchNode => :on_nester, Prism::BeginNode => :on_begin, Prism::AndNode => :on_boolean, Prism::OrNode => :on_boolean, Prism::BlockNode => :on_block, Prism::CallNode => :on_call }.freeze
- LABELS =
{ Prism::UnlessNode => "unless", Prism::WhileNode => "while", Prism::UntilNode => "until", Prism::ForNode => "for", Prism::CaseNode => "case", Prism::CaseMatchNode => "case" }.freeze
Instance Attribute Summary collapse
-
#calls ⇒ Object
readonly
Returns the value of attribute calls.
-
#increments ⇒ Object
readonly
Returns the value of attribute increments.
Instance Method Summary collapse
- #add(node, cost, label) ⇒ Object
-
#initialize(def_node) ⇒ CognitiveScore
constructor
A new instance of CognitiveScore.
- #total ⇒ Object
- #visit(node, nesting) ⇒ Object
Constructor Details
#initialize(def_node) ⇒ CognitiveScore
Returns a new instance of CognitiveScore.
29 30 31 32 33 |
# File 'lib/hashira/complexity/cognitive_score.rb', line 29 def initialize(def_node) @increments = [] @calls = 0 visit(def_node.body, 0) end |
Instance Attribute Details
#calls ⇒ Object (readonly)
Returns the value of attribute calls.
35 36 37 |
# File 'lib/hashira/complexity/cognitive_score.rb', line 35 def calls @calls end |
#increments ⇒ Object (readonly)
Returns the value of attribute increments.
35 36 37 |
# File 'lib/hashira/complexity/cognitive_score.rb', line 35 def increments @increments end |
Instance Method Details
#add(node, cost, label) ⇒ Object
45 46 47 |
# File 'lib/hashira/complexity/cognitive_score.rb', line 45 def add(node, cost, label) @increments << Increment.new(line: node.location.start_line, cost:, label:) end |
#total ⇒ Object
37 |
# File 'lib/hashira/complexity/cognitive_score.rb', line 37 def total = @increments.sum(&:cost) |
#visit(node, nesting) ⇒ Object
39 40 41 42 43 |
# File 'lib/hashira/complexity/cognitive_score.rb', line 39 def visit(node, nesting) return unless node send(HANDLERS.fetch(node.class, :descend), node, nesting) end |