/
rule.rb
55 lines (45 loc) · 1.36 KB
/
rule.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# frozen_string_literal: true
module RubocopChallenger
module Rubocop
# Parse rubocop rule which loaded from Rubocop::TodoReader class
class Rule
include Comparable
attr_reader :title, :offense_count, :contents
def initialize(contents)
@contents = contents.dup
@title = extract_title
@offense_count = extract_offense_count
end
# Compare to other rule
#
# @param other [Rule] Target of comparison
# @return [Boolean] Return true in case of coincidence
def ==(other)
title == other.title
end
def <=>(other)
offense_count <=> other.offense_count
end
def auto_correctable?
contents.include?('# Cop supports --auto-correct') || # for rubocop < v1.26.0
contents.match?(/# This cop supports (un)?safe auto-correction/) # for rubocop >= v1.26.0
end
def rubydoc_url
if title.start_with?('RSpec')
"https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/#{title}"
else
"https://www.rubydoc.info/gems/rubocop/RuboCop/Cop/#{title}"
end
end
private
def extract_title
contents =~ /^([^# ].+):$/
Regexp.last_match(1)
end
def extract_offense_count
contents =~ /# Offense count: (\d+)/
Regexp.last_match(1).to_i
end
end
end
end