class Danger::DangerRegexp

Constants

ChangedLine
MAX_COMMENT_COUNT

Public Instance Methods

lint(&block) click to toggle source
# File lib/danger_plugin.rb, line 9
    def lint(&block)
      @map = {}

      instance_eval(&block)

      @map.each do |regexp, message|
        count = 0

        target_lines = changed_lines.select do |line|
          line.content.match?(regexp)
        end

        target_lines.first(MAX_COMMENT_COUNT).each do |line|
          markdown(message, file: line.file, line: line.number)
        end

        if target_lines.size > MAX_COMMENT_COUNT
          warn <<~MSG
            Regexp `#{regexp.inspect}` matched too many lines (#{target_lines.size} lines). Only first #{MAX_COMMENT_COUNT} comments are posted.
          MSG
        end
      end
    end

Private Instance Methods

changed_lines() click to toggle source
# File lib/danger_plugin.rb, line 41
def changed_lines
  @changed_lines ||= git.diff.flat_map do |diff|
    GitDiffParser::Patch.new(diff.patch).changed_lines.map do |line|
      ChangedLine.new(file: diff.path, number: line.number, content: line.content)
    end
  end
end
match(*regexps, message) click to toggle source
# File lib/danger_plugin.rb, line 35
def match(*regexps, message)
  regexps.each do |regexp|
    @map[regexp] = message
  end
end