Sha256: 7163813c254da9e6384e82387c66986466ebcc7dbdbf9313feb8bb5f08e7fba5

Contents?: true

Size: 1.42 KB

Versions: 1

Compression:

Stored size: 1.42 KB

Contents

module Pelusa
  module Lint
    class IndentationLevel
      def initialize
        @violations = Set.new
      end

      def check(klass)
        initialize
        iterate_lines!(klass)

        return SuccessfulAnalysis.new(name) if @violations.empty?

        FailedAnalysis.new(name, @violations) do |violations|
          "There's too much indentation in lines #{violations.to_a.join(', ')}."
        end
      end

      private

      def name
        "Doesn't use more than one indentation level inside methods"
      end

      def iterate_lines!(klass)
        iterator = Iterator.new do |node|
          if node.is_a?(Rubinius::AST::Define)
            _iterate = Iterator.new do |node|
              __iterate = Iterator.new do |node|
                if body = get_body_from_node[node]
                  if node.line != [body].flatten.first.line
                    @violations.merge Array(body).map(&:line)
                  end
                end
              end

              Array(get_body_from_node[node]).each(&__iterate)
            end
            node.body.array.each(&_iterate)
          end
        end

        Array(klass).each(&iterator)
      end

      def get_body_from_node
        lambda do |node|
          if node.respond_to?(:body) && !node.body.is_a?(Rubinius::AST::NilLiteral)
             node.body
          elsif node.respond_to?(:else)
             node.else
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pelusa-0.2.2 lib/pelusa/lint/indentation_level.rb