Sha256: 09916fd36fd683170a9b7577725c9830a9b4bc40bc571afd51675846384537ce

Contents?: true

Size: 1.38 KB

Versions: 7

Compression:

Stored size: 1.38 KB

Contents

module Pelusa
  module Lint
    class CollectionWrappers
      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|
          "Using an instance variable apart from the array ivar in lines #{violations.to_a.join(', ')}"
        end
      end

      private

      def name
        "Doesn't mix array instance variables with others"
      end

      def iterate_lines!(klass)
        array_assignment = nil

        get_array_assignment = Iterator.new do |node|
          if node.is_a?(Rubinius::AST::InstanceVariableAssignment) &&
            (node.value.is_a?(Rubinius::AST::ArrayLiteral) ||
            node.value.is_a?(Rubinius::AST::EmptyArray))
              array_assignment = node
          end
        end

        iterator = Iterator.new do |node|
          next if node == array_assignment || !array_assignment

          if node.is_a?(Rubinius::AST::InstanceVariableAssignment)
            @violations << node.line
          elsif node.is_a?(Rubinius::AST::InstanceVariableAccess) && node.name != array_assignment.name
            @violations << node.line
          end
        end
        Array(klass).each(&get_array_assignment)
        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/collection_wrappers.rb
pelusa-0.2.1 lib/pelusa/lint/collection_wrappers.rb
pelusa-0.2.0 lib/pelusa/lint/collection_wrappers.rb
pelusa-0.1.1 lib/pelusa/lint/collection_wrappers.rb
pelusa-0.1.0 lib/pelusa/lint/collection_wrappers.rb
pelusa-0.0.2 lib/pelusa/lint/collection_wrappers.rb
pelusa-0.0.1 lib/pelusa/lint/collection_wrappers.rb