Sha256: 14ce9c01983c0949f9b1fe77b781a865d04a4051f136d4172d3733cc6506ace2

Contents?: true

Size: 1.76 KB

Versions: 4

Compression:

Stored size: 1.76 KB

Contents

module Cycromatic
  class Calculator
    attr_reader :node

    def initialize(node:)
      @node = node
    end

    def each_complexity(&block)
      if block_given?
        calculate_toplevel node, &block
      else
        enum_for :each_complexity
      end
    end

    private

    COMPLEX_NODE_TYPES = Set.new([:if, :while, :while_post, :when, :resbody, :csend, :for,
                                  :and, :or,
                                  :optarg, :kwoptarg])

    def calculate_toplevel(node, &block)
      value = calculate_node(node, &block)
      yield Complexity.new(type: :toplevel, node: node, value: value + 1)
    end

    def calculate_def(node, &block)
      value = 0

      case node.type
      when :def
        args = node.children[1]
        body = node.children[2]
      when :defs
        args = node.children[2]
        body = node.children[3]
      end

      value += calculate_node(args, &block)
      value += calculate_node(body, &block) if body

      yield Complexity.new(type: :method, node: node, value: value + 1)
    end

    def calculate_node(node, &block)
      count = 0

      case node.type
      when :def
        calculate_def node, &block
        return 0
      when :defs
        calculate_def node, &block
        return calculate_node(node.children[0], &block)
      when :case
        if node.children.last
          count = 1
        end
      when :rescue
        if node.children.last
          count = 1
        end
      else
        if COMPLEX_NODE_TYPES.include?(node.type)
          count = 1
        end
      end

      count + node.children.flat_map do |child|
        if child.is_a? Parser::AST::Node
          [calculate_node(child, &block)]
        else
          []
        end
      end.inject(0) {|x, y| x + y }
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
cycromatic-0.1.3 lib/cycromatic/calculator.rb
cycromatic-0.1.2 lib/cycromatic/calculator.rb
cycromatic-0.1.1 lib/cycromatic/calculator.rb
cycromatic-0.1.0 lib/cycromatic/calculator.rb