Sha256: 0e1b3eb74333ae14db5a074a2098b30e6bee60c475dc408cc17e8d816f95bd1f

Contents?: true

Size: 1.72 KB

Versions: 5

Compression:

Stored size: 1.72 KB

Contents

require 'pronto'
require 'flay'

module Pronto
  class Flay < Runner
    def run
      if files.any?
        flay.analyze
        messages
      else
        []
      end
    end

    # The current Flay (2.8.0) takes care of filtering
    # files by looking at .flayignore.
    # Saving the returned Flay object at @flay so we
    # can inspect it and build the messages Array.
    def flay
      @flay ||= ::Flay.run(files)
    end

    def files
      @files ||= ruby_patches.map(&:new_file_full_path)
    end

    def messages
      nodes.map do |node|
        patch = patch_for_node(node)

        line = patch.added_lines.find do |added_line|
          added_line.new_lineno == node.line
        end

        new_message(line, node) if line
      end.flatten.compact
    end

    def patch_for_node(node)
      ruby_patches.find do |patch|
        patch.new_file_full_path == node.file
      end
    end

    def new_message(line, node)
      path = line.patch.delta.new_file[:path]
      hash = node.structural_hash
      Message.new(path, line, level(hash), message(hash), nil, self.class)
    end

    def level(hash)
      same?(hash) ? :error : :warning
    end

    def same?(hash)
      flay.identical[hash]
    end

    def message(hash)
      match = same?(hash) ? 'Identical' : 'Similar'
      location = nodes_for(hash).map do |node|
        "#{File.basename(node.file)}:#{node.line}"
      end

      "#{match} code found in #{location.join(', ')} (mass = #{masses[hash]})"
    end

    def nodes_for(hash)
      flay.hashes[hash]
    end

    def nodes
      result = []
      masses.keys.each do |hash|
        nodes_for(hash).each { |node| result << node }
      end
      result
    end

    def masses
      flay.masses
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
pronto-flay-0.11.0 lib/pronto/flay.rb
pronto-flay-0.10.0 lib/pronto/flay.rb
pronto-flay-0.9.0 lib/pronto/flay.rb
pronto-flay-0.8.0 lib/pronto/flay.rb
pronto-flay-0.7.0 lib/pronto/flay.rb