Sha256: 9cf154cd54d5859a2fccb0a993af6e68b5a59ae0abe12b5594cb8b2931bba525

Contents?: true

Size: 973 Bytes

Versions: 7

Compression:

Stored size: 973 Bytes

Contents

module Pelusa
  module Lint
    class ElseClauses
      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 are #{violations.length} else clauses in lines #{violations.to_a.join(', ')}"
        end
      end

      private

      def name
        "Doesn't use else clauses"
      end

      def iterate_lines!(klass)
        iterator = Iterator.new do |node|
          if node.is_a?(Rubinius::AST::If)
            has_body = node.body && !node.body.is_a?(Rubinius::AST::NilLiteral)
            has_else = node.else && !node.else.is_a?(Rubinius::AST::NilLiteral)

            if has_body && has_else
              @violations << node.else.line
            end
          end
        end
        Array(klass).each(&iterator)
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
pelusa-0.2.2 lib/pelusa/lint/else_clauses.rb
pelusa-0.2.1 lib/pelusa/lint/else_clauses.rb
pelusa-0.2.0 lib/pelusa/lint/else_clauses.rb
pelusa-0.1.1 lib/pelusa/lint/else_clauses.rb
pelusa-0.1.0 lib/pelusa/lint/else_clauses.rb
pelusa-0.0.2 lib/pelusa/lint/else_clauses.rb
pelusa-0.0.1 lib/pelusa/lint/else_clauses.rb