Class: RuboCop::Cop::Offense
- Inherits:
-
Object
- Object
- RuboCop::Cop::Offense
- 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
- #column ⇒ Object readonly private
-
#cop_name ⇒ String
readonly
The cop name as a String for which this offense is for.
-
#correctable? ⇒ Boolean
readonly
Whether this offense can be automatically corrected via autocorrect.
-
#corrected? ⇒ Boolean
readonly
Whether this offense is automatically corrected via autocorrect or a todo.
-
#corrected_with_todo? ⇒ Boolean
readonly
Whether this offense is automatically disabled via a todo.
-
#corrector ⇒ Corrector | nil
readonly
The autocorrection for this offense, or
nilwhen not available. -
#disabled? ⇒ Boolean
readonly
Whether this offense was locally disabled with a disable or todo where it occurred.
-
#justification ⇒ String?
readonly
The reason given on the directive that suppressed this offense (the text after
--), ornilwhen the offense is not suppressed or the directive carries no reason. - #line ⇒ Object readonly private
-
#location ⇒ Parser::Source::Range
readonly
The location where the violation is detected.
-
#message ⇒ String
readonly
Human-readable message.
- #severity ⇒ RuboCop::Cop::Severity readonly
- #status ⇒ Object readonly private
Instance Method Summary collapse
-
#<=>(other) ⇒ Integer
Returns
-1,0, or+1if this offense is less than, equal to, or greater thanother. -
#==(other) ⇒ Boolean
(also: #eql?)
Returns
trueif two offenses contain same attributes. - #column_length ⇒ Object private
- #column_range ⇒ Object private
- #first_line ⇒ Object private
- #hash ⇒ Object
-
#highlighted_area ⇒ Parser::Source::Range
The range of the code that is highlighted.
-
#initialize(severity, location, message, cop_name, status = :uncorrected, corrector = nil, justification: nil) ⇒ Offense
constructor
private
A new instance of Offense.
- #last_column ⇒ Object private
- #last_line ⇒ Object private
- #marshal_dump ⇒ Object
- #marshal_load(array) ⇒ Object
-
#real_column ⇒ Object
private
Internally we use column number that start at 0, but when outputting column numbers, we want them to start at 1.
-
#real_last_column ⇒ Object
private
The minimum value of
real_columnis 1, so the minimum value ofreal_last_columnis also 1. - #source_line ⇒ Object private
-
#to_s ⇒ Object
private
This is just for debugging purpose.
Constructor Details
#initialize(severity, location, message, cop_name, status = :uncorrected, corrector = nil, justification: 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.
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/rubocop/cop/offense.rb', line 100 def initialize(severity, location, , cop_name, # rubocop:disable Metrics/ParameterLists status = :uncorrected, corrector = nil, justification: nil) @severity = RuboCop::Cop::Severity.new(severity) @location = location # Pre-compute the position eagerly because the offense is frozen and # `location.line` / `location.column` are expensive to compute; sorting # many offenses calls them repeatedly through `#<=>`. @line = location.line @column = location.column @message = .freeze @cop_name = cop_name.freeze @status = status @corrector = corrector @justification = justification.freeze freeze end |
Instance Attribute Details
#column ⇒ Object (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.
194 195 196 |
# File 'lib/rubocop/cop/offense.rb', line 194 def column @column end |
#cop_name ⇒ String (readonly)
Returns the cop name as a String for which this offense is for.
50 51 52 |
# File 'lib/rubocop/cop/offense.rb', line 50 def cop_name @cop_name end |
#correctable? ⇒ Boolean (readonly)
Returns whether this offense can be automatically corrected via autocorrect.
This includes todo comments, for example when requested with --disable-uncorrectable.
An offense suppressed by a directive comment is not correctable -
autocorrect will not touch it.
137 138 139 |
# File 'lib/rubocop/cop/offense.rb', line 137 def correctable? @status != :unsupported && @status != :disabled end |
#corrected? ⇒ Boolean (readonly)
Returns whether this offense is automatically corrected via autocorrect or a todo.
148 149 150 |
# File 'lib/rubocop/cop/offense.rb', line 148 def corrected? @status == :corrected || @status == :corrected_with_todo end |
#corrected_with_todo? ⇒ Boolean (readonly)
Returns whether this offense is automatically disabled via a todo.
158 159 160 |
# File 'lib/rubocop/cop/offense.rb', line 158 def corrected_with_todo? @status == :corrected_with_todo end |
#corrector ⇒ Corrector | nil (readonly)
Returns the autocorrection for this offense, or nil when not available.
61 62 63 |
# File 'lib/rubocop/cop/offense.rb', line 61 def corrector @corrector end |
#disabled? ⇒ Boolean (readonly)
Returns whether this offense was locally disabled with a disable or todo where it occurred.
169 170 171 |
# File 'lib/rubocop/cop/offense.rb', line 169 def disabled? @status == :disabled || @status == :todo end |
#justification ⇒ String? (readonly)
Returns the reason given on the directive that suppressed this offense
(the text after --), or nil when the offense is not suppressed
or the directive carries no reason.
97 98 99 |
# File 'lib/rubocop/cop/offense.rb', line 97 def justification @justification end |
#line ⇒ Object (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.
191 192 193 |
# File 'lib/rubocop/cop/offense.rb', line 191 def line @line end |
#location ⇒ Parser::Source::Range (readonly)
Returns the location where the violation is detected.
28 29 30 |
# File 'lib/rubocop/cop/offense.rb', line 28 def location @location end |
#message ⇒ String (readonly)
Returns human-readable message.
39 40 41 |
# File 'lib/rubocop/cop/offense.rb', line 39 def @message end |
#severity ⇒ RuboCop::Cop::Severity (readonly)
17 18 19 |
# File 'lib/rubocop/cop/offense.rb', line 17 def severity @severity end |
#status ⇒ Object (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.
53 54 55 |
# File 'lib/rubocop/cop/offense.rb', line 53 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.
272 273 274 275 276 277 278 |
# File 'lib/rubocop/cop/offense.rb', line 272 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
253 254 255 256 257 |
# File 'lib/rubocop/cop/offense.rb', line 253 def ==(other) COMPARISON_ATTRIBUTES.all? do |attribute| public_send(attribute) == other.public_send(attribute) end end |
#column_length ⇒ Object
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.
202 203 204 205 206 207 208 |
# File 'lib/rubocop/cop/offense.rb', line 202 def column_length if first_line == last_line column_range.count else source_line.length - column end end |
#column_range ⇒ Object
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.
226 227 228 |
# File 'lib/rubocop/cop/offense.rb', line 226 def column_range location.column_range end |
#first_line ⇒ Object
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.
211 212 213 |
# File 'lib/rubocop/cop/offense.rb', line 211 def first_line location.first_line end |
#hash ⇒ Object
261 262 263 |
# File 'lib/rubocop/cop/offense.rb', line 261 def hash COMPARISON_ATTRIBUTES.map { |attribute| public_send(attribute) }.hash end |
#highlighted_area ⇒ Parser::Source::Range
Returns the range of the code that is highlighted.
177 178 179 180 |
# File 'lib/rubocop/cop/offense.rb', line 177 def highlighted_area source_buffer = Parser::Source::Buffer.new(location.source_buffer.name, source: source_line) Parser::Source::Range.new(source_buffer, column, column + column_length) end |
#last_column ⇒ Object
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.
221 222 223 |
# File 'lib/rubocop/cop/offense.rb', line 221 def last_column location.last_column end |
#last_line ⇒ Object
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.
216 217 218 |
# File 'lib/rubocop/cop/offense.rb', line 216 def last_line location.last_line end |
#marshal_dump ⇒ Object
118 119 120 |
# File 'lib/rubocop/cop/offense.rb', line 118 def marshal_dump [@severity, @location, @message, @cop_name, @status, @justification] end |
#marshal_load(array) ⇒ Object
122 123 124 125 126 |
# File 'lib/rubocop/cop/offense.rb', line 122 def marshal_load(array) @severity, @location, @message, @cop_name, @status, @justification = array @line = @location.line @column = @location.column end |
#real_column ⇒ Object
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.
235 236 237 |
# File 'lib/rubocop/cop/offense.rb', line 235 def real_column column + 1 end |
#real_last_column ⇒ Object
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.
The minimum value of real_column is 1, so the minimum value of
real_last_column is also 1. A non-zero last_column is used as is,
because the 0-based end-exclusive column is the same as the 1-based
column of the last character.
245 246 247 |
# File 'lib/rubocop/cop/offense.rb', line 245 def real_last_column last_column.zero? ? 1 : last_column end |
#source_line ⇒ Object
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.
197 198 199 |
# File 'lib/rubocop/cop/offense.rb', line 197 def source_line location.source_line end |
#to_s ⇒ Object
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.
184 185 186 187 188 |
# File 'lib/rubocop/cop/offense.rb', line 184 def to_s format('%<severity>s:%3<line>d:%3<column>d: %<message>s', severity: severity.code, line: line, column: real_column, message: ) end |