Sha256: 7a7ceef185947dbf64db675085163d4beec7b525a054a71600caa853c0959829

Contents?: true

Size: 1.19 KB

Versions: 1

Compression:

Stored size: 1.19 KB

Contents

# frozen_string_literal: true

module Danger
  # Lint haskell files against ormolu formatting
  #
  # @example Ensure the files are correctly formatted
  #
  #          ormolu.check files
  #
  # @see  blackheaven/danger-ormolu
  # @tags haskell, formatting, ormolu
  #
  class DangerOrmolu < Plugin
    # Check that the files are correctly formatted
    # @param files [Array<String>]
    # @return [void]
    #
    def check(files)
      files
        .each do |file|
          result = `ormolu --mode stdout --check-idempotence "#{file}" | diff "#{file}" -`
          next if result.empty?

          extract_diffs(result.lines)
            .each do |diff|
              inconsistence(file, diff[:line], diff[:diff])
            end
        end
    end

    private

    def inconsistence(file, line, diff)
      message = "Style error, fix it through \n\n```haskell\n#{diff}\n``` \n"
      warn(message, file: file, line: line)
    end

    def extract_diffs(lines)
      lines
        .reverse
        .slice_when { |l| l =~ /^\d.*/ }
        .to_a
        .map(&:reverse)
        .reverse
        .map do |chunk|
          { line: chunk.first[/^\d+/].to_i, diff: chunk.flatten }
        end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
danger-ormolu-0.0.1 lib/ormolu/plugin.rb