Class: Rubomop::Cop

Inherits:
Literal::Object
  • Object
show all
Defined in:
lib/rubomop/cop.rb

Constant Summary collapse

OFFENSE_COUNT_REGEX =
/\A# Offense count: (\d*)/
COP_NAME_REGEX =
/\A(\S.*):/
FILE_NAME_REGEX =
/- '(.*)'/
SAFE_AUTOCORRECT_REGEX =
/\A# This cop supports safe autocorrection/
UNSAFE_AUTOCORRECT_REGEX =
/\A# This cop supports unsafe autocorrection/
GENERAL_COMMENT_REGEX =
/\A#/
EXCLUDE_REGEX =
/Exclude:/
CONFIG_PARAM_REGEX =
/\A\s\s(.*)/

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create_and_parse(raw_lines) ⇒ Object



12
13
14
15
16
# File 'lib/rubomop/cop.rb', line 12

def self.create_and_parse(raw_lines)
  result = new(raw_lines:)
  result.parse
  result
end

Instance Method Details

#activateObject



104
105
106
# File 'lib/rubomop/cop.rb', line 104

def activate
  self.active = true
end

#active?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/rubomop/cop.rb', line 112

def active?
  active
end

#any_autocorrect?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/rubomop/cop.rb', line 70

def any_autocorrect?
  autocorrect != :none
end

#autocorrect_inquiryObject



74
75
76
# File 'lib/rubomop/cop.rb', line 74

def autocorrect_inquiry
  autocorrect.to_s.inquiry
end

#autocorrect_optionObject



87
88
89
90
91
92
93
94
# File 'lib/rubomop/cop.rb', line 87

def autocorrect_option
  case autocorrect
  when :safe then "a"
  when :unsafe then "A"
  else
    ""
  end
end

#autocorrect_verbiageObject



78
79
80
81
82
83
84
85
# File 'lib/rubomop/cop.rb', line 78

def autocorrect_verbiage
  case autocorrect
  when :safe then "safe autocorrect"
  when :unsafe then "unsafe autocorrect"
  else
    "no autocorrect"
  end
end

#deactivateObject



108
109
110
# File 'lib/rubomop/cop.rb', line 108

def deactivate
  self.active = false
end

#delete!(filename) ⇒ Object



96
97
98
# File 'lib/rubomop/cop.rb', line 96

def delete!(filename)
  files.delete(filename)
end

#outputObject



52
53
54
# File 'lib/rubomop/cop.rb', line 52

def output
  output_lines.join("\n")
end

#output_linesObject



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/rubomop/cop.rb', line 56

def output_lines
  result = ["# Offense count: #{offense_count}"]
  result << "# This cop supports safe autocorrection (--autocorrect)." if autocorrect == :safe
  result << "# This cop supports unsafe autocorrection (--autocorrect-all)." if autocorrect == :unsafe
  result += comments
  result << "#{name}:"
  result += config_params.map { "  #{_1}" }
  unless files.empty?
    result << "  Exclude:"
    result += files.map { "    - '#{_1}'" }
  end
  result
end

#parseObject



18
19
20
# File 'lib/rubomop/cop.rb', line 18

def parse
  raw_lines.each { parse_one_line(_1) }
end

#parse_one_line(line) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rubomop/cop.rb', line 31

def parse_one_line(line)
  case line
  when OFFENSE_COUNT_REGEX
    self.offense_count = line.match(OFFENSE_COUNT_REGEX)[1].to_i
  when SAFE_AUTOCORRECT_REGEX
    self.autocorrect = :safe
  when UNSAFE_AUTOCORRECT_REGEX
    self.autocorrect = :unsafe
  when GENERAL_COMMENT_REGEX
    comments << line.chomp
  when EXCLUDE_REGEX
    # no-op
  when COP_NAME_REGEX
    self.name = line.match(COP_NAME_REGEX)[1]
  when FILE_NAME_REGEX
    files << line.match(FILE_NAME_REGEX)[1]
  when CONFIG_PARAM_REGEX
    config_params << line.match(CONFIG_PARAM_REGEX)[1]
  end
end

#subtract!(offense_count) ⇒ Object



100
101
102
# File 'lib/rubomop/cop.rb', line 100

def subtract!(offense_count)
  self.offense_count -= offense_count
end