Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,13 @@ PreCommit:
required_executable: 'grep'
flags: ['-IHn', "\t"]

Hlint:
enabled: false
description: 'Analyzing with hlint'
required_executable: 'hlint'
install_command: 'cabal install hlint'
include: '**/*.hs'

HtmlHint:
enabled: false
description: 'Analyzing with HTMLHint'
Expand Down
32 changes: 32 additions & 0 deletions lib/overcommit/hook/pre_commit/hlint.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module Overcommit::Hook::PreCommit
# Runs `hlint` against any modified Haskell files.
#
# @see https://github.com/ndmitchell/hlint
class Hlint < Base
MESSAGE_REGEX = /
^(?<file>(?:\w:)?[^:]+)
:(?<line>\d+)
:\d+
:\s*(?<type>\w+)
/x

MESSAGE_TYPE_CATEGORIZER = lambda do |type|
type.include?('W') ? :warning : :error
end

def run
result = execute(command, args: applicable_files)
return :pass if result.success?

raw_messages = result.stdout.split("\n").grep(MESSAGE_REGEX)

# example message:
# path/to/file.hs:1:0: Error: message
extract_messages(
raw_messages,
MESSAGE_REGEX,
MESSAGE_TYPE_CATEGORIZER
)
end
end
end
58 changes: 58 additions & 0 deletions spec/overcommit/hook/pre_commit/hlint_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
require 'spec_helper'

describe Overcommit::Hook::PreCommit::Hlint do
let(:config) { Overcommit::ConfigurationLoader.default_configuration }
let(:context) { double('context') }
subject { described_class.new(config, context) }

let(:result) { double('result') }

before do
subject.stub(:applicable_files).and_return(%w[file1.hs file2.hs])
subject.stub(:execute).and_return(result)
end

context 'when hlint exits successfully' do
before do
result.stub(success?: true, stdout: '')
end

it { should pass }
end

context 'when hlint exits unsucessfully' do
let(:result) { double('result') }

before do
result.stub(:success?).and_return(false)
end

context 'and it reports a warning' do
before do
result.stub(:stdout).and_return(normalize_indent(<<-OUT))
file1.hs:22:16: Warning: Use const
Found:
\\ _ -> False
Why not:
const False
OUT
end

it { should warn }
end

context 'and it reports an error' do
before do
result.stub(:stdout).and_return(normalize_indent(<<-OUT))
file1.hs:22:5: Error: Redundant lambda
Found:
nameHack = \\ _ -> Nothing
Why not:
nameHack _ = Nothing
OUT
end

it { should fail_hook }
end
end
end