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.
27 28 29 30 31 |
# File 'lib/hashira/complexity/cognitive_score.rb', line 27 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.
33 34 35 |
# File 'lib/hashira/complexity/cognitive_score.rb', line 33 def calls @calls end |
#increments ⇒ Object (readonly)
Returns the value of attribute increments.
33 34 35 |
# File 'lib/hashira/complexity/cognitive_score.rb', line 33 def increments @increments end |
Instance Method Details
#add(node, cost, label) ⇒ Object
42 43 44 |
# File 'lib/hashira/complexity/cognitive_score.rb', line 42 def add(node, cost, label) @increments << Hashira::Complexity::Increment.new(line: node.location.start_line, cost:, label:) end |
#total ⇒ Object
35 |
# File 'lib/hashira/complexity/cognitive_score.rb', line 35 def total = @increments.sum(&:cost) |
#visit(node, nesting) ⇒ Object
37 38 39 40 |
# File 'lib/hashira/complexity/cognitive_score.rb', line 37 def visit(node, nesting) return unless node __send__(HANDLERS.fetch(node.class, :descend), node, nesting) end |