lib/code_keeper/metrics/class_length.rb
# NOTE: This doesn't exclude foldale sources like Array, Hash and Heredoc.
This doesn't exclude foldale sources like Array, Hash and Heredoc.
This doesn ' t exclude foldale sources like Array Hash and Heredoc .
def score
        @body.each_node(:class, :casgn, :module) do |node|
          if node.class_type? || node.module_type?
            @score_hash.store(build_namespace(node), calculate(node))
          elsif node.casgn_type?
            parent = node.parent
            if parent&.assignment?
              block_node = node.children[2]
              klass = node.loc.name.source
            elsif parent&.parent&.masgn_type?
              assigned = parent.loc.expression.source.split(',').first
              next unless node.loc.name.source == assigned
              block_node = parent.parent.children[1]
              klass = node.loc.name.source
            else
              _scope, klass, block_node = *node
              klass = klass.to_s
            end
            next unless block_node.respond_to?(:class_definition?) && block_node.class_definition?
            if klass
              @score_hash.store(klass, calculate(block_node)) if klass
            else
              @score_hash.store(build_namespace(block_node), calculate(block_node))
            end
          end
        end
        @score_hash
      end
def score @body . each_node ( :class , :casgn , :module ) do | node | if node . class_type? || node . module_type? @score_hash . store ( build_namespace ( node ) , calculate ( node ) ) elsif node . casgn_type? parent = node . parent if parent &. assignment? block_node = node . children [ 2 ] klass = node . loc . name . source elsif parent &. parent &. masgn_type? assigned = parent . loc . expression . source . split ( ',' ) . first next unless node . loc . name . source == assigned block_node = parent . parent . children [ 1 ] klass = node . loc . name . source else _scope , klass , block_node = * node klass = klass . to_s end next unless block_node . respond_to? ( :class_definition? ) && block_node . class_definition? if klass @score_hash . store ( klass , calculate ( block_node ) ) if klass else @score_hash . store ( build_namespace ( block_node ) , calculate ( block_node ) ) end end end @score_hash end
NOTE: This doesn't exclude foldale sources like Array, Hash and Heredoc.
NOTE : This doesn ' t exclude foldale sources like Array Hash and Heredoc .
# In the case where `A, B = Struct.new(:a, :b)`, # B is always nil. # This is not to raise error on dynamic assignments like `X = Y = Z = Class.new`. # the block node is as follows if node is X: # `s(:casgn, nil, :Y, s(:casgn, nil, :Z, s(:block, ...` # Similarly the block node is `:X` as follows if node is Y. # NOTE: klass doesn't have a namespace. # Only supports namepaces in `class A; end` case.