forked from sds/overcommit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rubo_cop.rb
35 lines (29 loc) · 910 Bytes
/
rubo_cop.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# frozen_string_literal: true
module Overcommit::Hook::PreCommit
# Runs `rubocop` against any modified Ruby files.
#
# @see http://batsov.com/rubocop/
class RuboCop < Base
GENERIC_MESSAGE_TYPE_CATEGORIZER = lambda do |type|
type.match?(/^warn/) ? :warning : :error
end
COP_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?
generic_messages = extract_messages(
result.stderr.split("\n"),
/^(?<type>[a-z]+)/i,
GENERIC_MESSAGE_TYPE_CATEGORIZER,
)
cop_messages = extract_messages(
result.stdout.split("\n"),
/^(?<file>(?:\w:)?[^:]+):(?<line>\d+):[^ ]+ (?<type>[^ ]+)/,
COP_MESSAGE_TYPE_CATEGORIZER,
)
generic_messages + cop_messages
end
end
end