Class: RuboCop::Cop::Offense

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/rubocop/cop/offense.rb

Overview

An offense represents a style violation detected by RuboCop.

Constant Summary collapse

COMPARISON_ATTRIBUTES =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

%i[line column cop_name message severity].freeze
NO_LOCATION =
PseudoSourceRange.new(1, 0, '', 0, 0).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(severity, location, message, cop_name, status = :uncorrected, corrector = nil) ⇒ Offense

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Offense.


84
85
86
87
88
89
90
91
92
93
# File 'lib/rubocop/cop/offense.rb', line 84

def initialize(severity, location, message, cop_name, # rubocop:disable Metrics/ParameterLists
               status = :uncorrected, corrector = nil)
  @severity = RuboCop::Cop::Severity.new(severity)
  @location = location
  @message = message.freeze
  @cop_name = cop_name.freeze
  @status = status
  @corrector = corrector
  freeze
end

Instance Attribute Details

#cop_nameString (readonly)

Returns a cop class name without department. i.e. type of the violation.

Examples:

'LineLength'

Returns:

  • (String)

    a cop class name without department. i.e. type of the violation.


51
52
53
# File 'lib/rubocop/cop/offense.rb', line 51

def cop_name
  @cop_name
end

#correctable?Boolean (readonly)

Returns whether this offense can be automatically corrected via autocorrect or a todo.

Returns:

  • (Boolean)

    whether this offense can be automatically corrected via autocorrect or a todo.


102
103
104
# File 'lib/rubocop/cop/offense.rb', line 102

def correctable?
  @status != :unsupported
end

#corrected?Boolean (readonly)

Returns whether this offense is automatically corrected via autocorrect or a todo.

Returns:

  • (Boolean)

    whether this offense is automatically corrected via autocorrect or a todo.


113
114
115
# File 'lib/rubocop/cop/offense.rb', line 113

def corrected?
  @status == :corrected || @status == :corrected_with_todo
end

#corrected_with_todo?Boolean (readonly)

Returns whether this offense is automatically disabled via a todo.

Returns:

  • (Boolean)

    whether this offense is automatically disabled via a todo.


123
124
125
# File 'lib/rubocop/cop/offense.rb', line 123

def corrected_with_todo?
  @status == :corrected_with_todo
end

#correctorCorrector | nil (readonly)

Returns the autocorrection for this offense, or `nil` when not available.

Returns:

  • (Corrector | nil)

    the autocorrection for this offense, or `nil` when not available


62
63
64
# File 'lib/rubocop/cop/offense.rb', line 62

def corrector
  @corrector
end

#disabled?Boolean (readonly)

Returns whether this offense was locally disabled with a disable or todo where it occurred.

Returns:

  • (Boolean)

    whether this offense was locally disabled with a disable or todo where it occurred.


134
135
136
# File 'lib/rubocop/cop/offense.rb', line 134

def disabled?
  @status == :disabled || @status == :todo
end

#locationParser::Source::Range (readonly)

Returns the location where the violation is detected.

Returns:

  • (Parser::Source::Range)

    the location where the violation is detected.

See Also:


28
29
30
# File 'lib/rubocop/cop/offense.rb', line 28

def location
  @location
end

#messageString (readonly)

Returns human-readable message.

Examples:

'Line is too long. [90/80]'

Returns:

  • (String)

    human-readable message


39
40
41
# File 'lib/rubocop/cop/offense.rb', line 39

def message
  @message
end

#severityRuboCop::Cop::Severity (readonly)


17
18
19
# File 'lib/rubocop/cop/offense.rb', line 17

def severity
  @severity
end

#statusObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


54
55
56
# File 'lib/rubocop/cop/offense.rb', line 54

def status
  @status
end

Instance Method Details

#<=>(other) ⇒ Integer

Returns `-1`, `0`, or `+1` if this offense is less than, equal to, or greater than `other`.

Returns:

  • (Integer)

    comparison result


230
231
232
233
234
235
236
# File 'lib/rubocop/cop/offense.rb', line 230

def <=>(other)
  COMPARISON_ATTRIBUTES.each do |attribute|
    result = public_send(attribute) <=> other.public_send(attribute)
    return result unless result.zero?
  end
  0
end

#==(other) ⇒ Boolean Also known as: eql?

Returns `true` if two offenses contain same attributes

Returns:

  • (Boolean)

    returns `true` if two offenses contain same attributes


211
212
213
214
215
# File 'lib/rubocop/cop/offense.rb', line 211

def ==(other)
  COMPARISON_ATTRIBUTES.all? do |attribute|
    public_send(attribute) == other.public_send(attribute)
  end
end

#columnObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


160
161
162
# File 'lib/rubocop/cop/offense.rb', line 160

def column
  location.column
end

#column_lengthObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


170
171
172
173
174
175
176
# File 'lib/rubocop/cop/offense.rb', line 170

def column_length
  if first_line == last_line
    column_range.count
  else
    source_line.length - column
  end
end

#column_rangeObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


194
195
196
# File 'lib/rubocop/cop/offense.rb', line 194

def column_range
  location.column_range
end

#first_lineObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


179
180
181
# File 'lib/rubocop/cop/offense.rb', line 179

def first_line
  location.first_line
end

#hashObject


219
220
221
# File 'lib/rubocop/cop/offense.rb', line 219

def hash
  COMPARISON_ATTRIBUTES.reduce(0) { |hash, attribute| hash ^ public_send(attribute).hash }
end

#highlighted_areaParser::Source::Range

Returns the range of the code that is highlighted.

Returns:

  • (Parser::Source::Range)

    the range of the code that is highlighted


142
143
144
# File 'lib/rubocop/cop/offense.rb', line 142

def highlighted_area
  Parser::Source::Range.new(source_line, column, column + column_length)
end

#last_columnObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


189
190
191
# File 'lib/rubocop/cop/offense.rb', line 189

def last_column
  location.last_column
end

#last_lineObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


184
185
186
# File 'lib/rubocop/cop/offense.rb', line 184

def last_line
  location.last_line
end

#lineObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


155
156
157
# File 'lib/rubocop/cop/offense.rb', line 155

def line
  location.line
end

#real_columnObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Internally we use column number that start at 0, but when outputting column numbers, we want them to start at 1. One reason is that editors, such as Emacs, expect this.


203
204
205
# File 'lib/rubocop/cop/offense.rb', line 203

def real_column
  column + 1
end

#source_lineObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


165
166
167
# File 'lib/rubocop/cop/offense.rb', line 165

def source_line
  location.source_line
end

#to_sObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This is just for debugging purpose.


148
149
150
151
152
# File 'lib/rubocop/cop/offense.rb', line 148

def to_s
  format('%<severity>s:%3<line>d:%3<column>d: %<message>s',
         severity: severity.code, line: line,
         column: real_column, message: message)
end