lib/wcc/utils.rb in danger-wcc-0.0.6 vs lib/wcc/utils.rb in danger-wcc-0.1.0

- old
+ new

@@ -20,20 +20,27 @@ end # All the diffs in the PR parsed into GitDiff objects def parsed_diffs @parsed_diffs ||= - plugin.git.diff.map do |d| + plugin.git.diff&.map do |d| begin GitDiff.from_string(d.patch) rescue StandardError logger.fatal "Error parsing patch:\n#{d.patch}" raise end end end + def find_file_in_diff(filename) + each_file_in_diff do |file, _diff| + return file if file.b_path == filename + end + nil + end + # Finds lines in the overall diff matching the given regex, and # executes a block for each matched line. # The results of the yield block are returned as an array. def find_in_diff(regex) each_file_in_diff do |file, diff| @@ -50,20 +57,25 @@ end end def each_file_in_diff(passed_diff = nil) diffs = passed_diff ? [passed_diff] : parsed_diffs - diffs.flat_map do |diff| + diffs&.flat_map do |diff| diff.files.flat_map do |file| yield(file, diff) end end end - def each_addition_in_diff(passed_diff = nil) + def each_addition_in_diff(passed_diff = nil, &block) + each_line_in_diff(passed_diff, type: :addition, &block) + end + + def each_line_in_diff(passed_diff = nil, type: nil) each_file_in_diff(passed_diff) do |file, diff| file.hunks.flat_map do |hunk| - lines = hunk.lines.select(&:addition?) + lines = hunk.lines + lines = lines.select { |l| l.public_send("#{type}?") } if type lines = lines.map { |l| yield(l, hunk, file, diff) } if block_given? lines end end end