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.
-
#nesting ⇒ Object
readonly
Returns the value of attribute nesting.
Instance Method Summary collapse
- #add(node, cost, label) ⇒ Object
- #deeper ⇒ Object
-
#initialize(def_node) ⇒ CognitiveScore
constructor
A new instance of CognitiveScore.
- #total ⇒ Object
- #visit(node) ⇒ Object
Constructor Details
#initialize(def_node) ⇒ CognitiveScore
Returns a new instance of CognitiveScore.
27 28 29 30 31 32 |
# File 'lib/hashira/complexity/cognitive_score.rb', line 27 def initialize(def_node) @increments = [] @calls = 0 @nesting = 0 visit(def_node.body) end |
Instance Attribute Details
#calls ⇒ Object (readonly)
Returns the value of attribute calls.
34 35 36 |
# File 'lib/hashira/complexity/cognitive_score.rb', line 34 def calls @calls end |
#increments ⇒ Object (readonly)
Returns the value of attribute increments.
34 35 36 |
# File 'lib/hashira/complexity/cognitive_score.rb', line 34 def increments @increments end |
#nesting ⇒ Object (readonly)
Returns the value of attribute nesting.
34 35 36 |
# File 'lib/hashira/complexity/cognitive_score.rb', line 34 def nesting @nesting end |
Instance Method Details
#add(node, cost, label) ⇒ Object
43 44 45 |
# File 'lib/hashira/complexity/cognitive_score.rb', line 43 def add(node, cost, label) @increments << Hashira::Complexity::Increment.new(line: node.location.start_line, cost:, label:) end |
#deeper ⇒ Object
47 48 49 50 51 |
# File 'lib/hashira/complexity/cognitive_score.rb', line 47 def deeper @nesting += 1 yield @nesting -= 1 end |
#total ⇒ Object
36 |
# File 'lib/hashira/complexity/cognitive_score.rb', line 36 def total = @increments.sum(&:cost) |
#visit(node) ⇒ Object
38 39 40 41 |
# File 'lib/hashira/complexity/cognitive_score.rb', line 38 def visit(node) return unless node __send__(HANDLERS.fetch(node.class, :descend), node) end |