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::BeginNode => :on_begin, Prism::BlockNode => :on_block } .merge(Prism::AndNode => :on_boolean, Prism::OrNode => :on_boolean, Prism::CallNode => :on_call) .merge(Prism::UnlessNode => :on_nester, Prism::WhileNode => :on_nester, Prism::UntilNode => :on_nester) .merge(Prism::ForNode => :on_nester, Prism::CaseNode => :on_nester, Prism::CaseMatchNode => :on_nester) .freeze
- LABELS =
{ Prism::UnlessNode => "unless", Prism::WhileNode => "while", Prism::UntilNode => "until" } .merge(Prism::ForNode => "for", Prism::CaseNode => "case", Prism::CaseMatchNode => "case").freeze
Instance Method Summary collapse
- #add(node, cost, label) ⇒ Object
- #calls ⇒ Object
- #deeper ⇒ Object
- #increments ⇒ Object
-
#initialize(node) ⇒ CognitiveScore
constructor
A new instance of CognitiveScore.
- #nesting ⇒ Object
- #total ⇒ Object
- #visit(node) ⇒ Object
Constructor Details
#initialize(node) ⇒ CognitiveScore
Returns a new instance of CognitiveScore.
15 16 17 |
# File 'lib/hashira/complexity/cognitive_score.rb', line 15 def initialize(node) @node = node end |
Instance Method Details
#add(node, cost, label) ⇒ Object
41 42 43 |
# File 'lib/hashira/complexity/cognitive_score.rb', line 41 def add(node, cost, label) @_increments << Hashira::Complexity::Increment.new(line: node.location.start_line, cost:, label:) end |
#calls ⇒ Object
24 25 26 27 |
# File 'lib/hashira/complexity/cognitive_score.rb', line 24 def calls walked @_calls end |
#deeper ⇒ Object
45 46 47 48 49 |
# File 'lib/hashira/complexity/cognitive_score.rb', line 45 def deeper @_nesting += 1 yield @_nesting -= 1 end |
#increments ⇒ Object
19 20 21 22 |
# File 'lib/hashira/complexity/cognitive_score.rb', line 19 def increments walked @_increments end |
#nesting ⇒ Object
29 30 31 32 |
# File 'lib/hashira/complexity/cognitive_score.rb', line 29 def nesting walked @_nesting end |
#total ⇒ Object
34 |
# File 'lib/hashira/complexity/cognitive_score.rb', line 34 def total = increments.sum(&:cost) |
#visit(node) ⇒ Object
36 37 38 39 |
# File 'lib/hashira/complexity/cognitive_score.rb', line 36 def visit(node) return unless node __send__(HANDLERS.fetch(node.class, :descend), node) end |