Sha256: 480ad4ecdbe08937765d043ee7e26438788a84d14c5955e75521cad1ee74973a

Contents?: true

Size: 1.94 KB

Versions: 72

Compression:

Stored size: 1.94 KB

Contents

# frozen_string_literal: true

module Danger
  class MessageGroup
    def initialize(file: nil, line: nil)
      @file = file
      @line = line
    end

    # Returns whether this `MessageGroup` is for the same line of code as
    #   `other`, taking which file they are in to account.
    # @param other [MessageGroup, Markdown, Violation]
    # @return [Boolean] whether this `MessageGroup` is for the same line of code
    def same_line?(other)
      other.file == file && other.line == line
    end

    # Merges two `MessageGroup`s that represent the same line of code
    # In future, perhaps `MessageGroup` will be able to represent a group of
    # messages for multiple lines.
    def merge(other)
      raise ArgumentError, "Cannot merge with MessageGroup for a different line" unless same_line?(other)

      @messages = (messages + other.messages).uniq
    end

    # Adds a message to the group.
    # @param message [Markdown, Violation] the message to add
    def <<(message)
      # TODO: insertion sort
      return nil unless same_line?(message)

      inserted = false
      messages.each.with_index do |other, idx|
        if (message <=> other) == -1
          inserted = true
          messages.insert(idx, message)
          break
        end
      end
      messages << message unless inserted
      messages
    end

    # The list of messages in this group. This list will be sorted in decreasing
    # order of severity (error, warning, message, markdown)
    def messages
      @messages ||= []
    end

    attr_reader :file, :line

    # @return a hash of statistics. Currently only :warnings_count and
    # :errors_count
    def stats
      stats = { warnings_count: 0, errors_count: 0 }
      messages.each do |msg|
        stats[:warnings_count] += 1 if msg.type == :warning
        stats[:errors_count] += 1 if msg.type == :error
      end
      stats
    end

    def markdowns
      messages.select { |x| x.type == :markdown }
    end
  end
end

Version data entries

72 entries across 72 versions & 2 rubygems

Version Path
danger-9.5.1 lib/danger/danger_core/message_group.rb
danger-9.5.0 lib/danger/danger_core/message_group.rb
danger-9.4.3 lib/danger/danger_core/message_group.rb
danger-9.4.2 lib/danger/danger_core/message_group.rb
danger-9.4.1 lib/danger/danger_core/message_group.rb
danger-9.4.0 lib/danger/danger_core/message_group.rb
danger-9.3.2 lib/danger/danger_core/message_group.rb
danger-additional-logging-0.0.36 lib/danger/danger_core/message_group.rb
danger-additional-logging-0.0.35 lib/danger/danger_core/message_group.rb
danger-additional-logging-0.0.34 lib/danger/danger_core/message_group.rb
danger-additional-logging-0.0.33 lib/danger/danger_core/message_group.rb
danger-additional-logging-0.0.32 lib/danger/danger_core/message_group.rb
danger-additional-logging-0.0.31 lib/danger/danger_core/message_group.rb
danger-additional-logging-0.0.30 lib/danger/danger_core/message_group.rb
danger-additional-logging-0.0.29 lib/danger/danger_core/message_group.rb
danger-additional-logging-0.0.28 lib/danger/danger_core/message_group.rb
danger-additional-logging-0.0.27 lib/danger/danger_core/message_group.rb
danger-additional-logging-0.0.26 lib/danger/danger_core/message_group.rb
danger-additional-logging-0.0.25 lib/danger/danger_core/message_group.rb
danger-additional-logging-0.0.24 lib/danger/danger_core/message_group.rb