Class: RuboCop::Cop::Offense
| Relationships & Source Files | |
| Namespace Children | |
|
Classes:
| |
| Super Chains via Extension / Inclusion / Inheritance | |
|
Instance Chain:
self,
Comparable
|
|
| Inherits: | Object |
| Defined in: | lib/rubocop/cop/offense.rb |
Overview
An offense represents a style violation detected by RuboCop.
Constant Summary
-
COMPARISON_ATTRIBUTES =
# File 'lib/rubocop/cop/offense.rb', line 10%i[line column cop_name message severity].freeze
-
EMPTY_CORRECTIONS =
private
# File 'lib/rubocop/cop/offense.rb', line 89[].freeze -
NO_LOCATION =
# File 'lib/rubocop/cop/offense.rb', line 87PseudoSourceRange.new(1, 0, '', 0, 0).freeze
Class Method Summary
Instance Attribute Summary
- #column readonly Internal use only
- #cop_name ⇒ String readonly
- #correctable? ⇒ Boolean readonly
- #corrected? ⇒ Boolean readonly
- #corrected_with_todo? ⇒ Boolean readonly
- #correction_safe ⇒ Boolean readonly
- #corrections ⇒ Array<Correction> readonly
- #corrector ⇒ Corrector | nil readonly
- #disabled? ⇒ Boolean readonly
- #justification ⇒ String? readonly
- #line readonly Internal use only
- #location ⇒ Parser::Source::Range readonly
- #message ⇒ String readonly
- #severity ⇒ RuboCop::Cop::Severity readonly
- #status readonly Internal use only
Instance Method Summary
-
#<=>(other) ⇒ Integer
Returns
-1,0, or+1if this offense is less than, equal to, or greater thanother. - #==(other) ⇒ Boolean (also: #eql?)
- #column_length Internal use only
- #column_range Internal use only
-
#eql?(other)
Alias for #==.
- #first_line Internal use only
- #hash
- #highlighted_area ⇒ Parser::Source::Range
- #last_column Internal use only
- #last_line Internal use only
- #marshal_dump
- #marshal_load(array)
-
#real_column
Internal use only
Internally we use column number that start at 0, but when outputting column numbers, we want them to start at 1.
-
#real_last_column
Internal use only
The minimum value of #real_column is 1, so the minimum value of #real_last_column is also 1.
- #source_line Internal use only
-
#to_s
Internal use only
This is just for debugging purpose.
-
#corrections_from(corrector)
private
Extracted eagerly rather than on demand because the offense is frozen, and because a corrector cannot be serialized - the cache and parallel workers would otherwise hand back offenses that have lost their edits.
Constructor Details
.new(severity, location, message, cop_name, status = :uncorrected, corrector = nil, justification: nil, corrections: nil, correction_safe: true) ⇒ Offense
# File 'lib/rubocop/cop/offense.rb', line 136
def initialize(severity, location, , cop_name, # rubocop:disable Metrics/ParameterLists status = :uncorrected, corrector = nil, justification: nil, corrections: nil, correction_safe: true) @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 @corrections = (corrections || corrections_from(corrector)).freeze @correction_safe = correction_safe freeze end
Instance Attribute Details
#column (readonly)
[ GitHub ]# File 'lib/rubocop/cop/offense.rb', line 237
attr_reader :column
#cop_name ⇒ String (readonly)
# File 'lib/rubocop/cop/offense.rb', line 50
attr_reader :cop_name
#correctable? ⇒ Boolean (readonly)
# File 'lib/rubocop/cop/offense.rb', line 180
def correctable? @status != :unsupported && @status != :disabled end
#corrected? ⇒ Boolean (readonly)
# File 'lib/rubocop/cop/offense.rb', line 191
def corrected? @status == :corrected || @status == :corrected_with_todo end
#corrected_with_todo? ⇒ Boolean (readonly)
# File 'lib/rubocop/cop/offense.rb', line 201
def corrected_with_todo? @status == :corrected_with_todo end
#correction_safe ⇒ Boolean (readonly)
# File 'lib/rubocop/cop/offense.rb', line 122
attr_reader :correction_safe
#corrections ⇒ Array<Correction> (readonly)
# File 'lib/rubocop/cop/offense.rb', line 111
attr_reader :corrections
#corrector ⇒ Corrector | nil (readonly)
# File 'lib/rubocop/cop/offense.rb', line 61
attr_reader :corrector
#disabled? ⇒ Boolean (readonly)
# File 'lib/rubocop/cop/offense.rb', line 212
def disabled? @status == :disabled || @status == :todo end
#justification ⇒ String? (readonly)
# File 'lib/rubocop/cop/offense.rb', line 100
attr_reader :justification
#line (readonly)
[ GitHub ]# File 'lib/rubocop/cop/offense.rb', line 234
attr_reader :line
#location ⇒ Parser::Source::Range (readonly)
# File 'lib/rubocop/cop/offense.rb', line 28
attr_reader :location
#message ⇒ String (readonly)
# File 'lib/rubocop/cop/offense.rb', line 39
attr_reader :
#severity ⇒ RuboCop::Cop::Severity (readonly)
# File 'lib/rubocop/cop/offense.rb', line 17
attr_reader :severity
#status (readonly)
[ GitHub ]# File 'lib/rubocop/cop/offense.rb', line 53
attr_reader :status
Instance Method Details
#<=>(other) ⇒ Integer
Returns -1, 0, or +1
if this offense is less than, equal to, or greater than other.
# File 'lib/rubocop/cop/offense.rb', line 315
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?
# File 'lib/rubocop/cop/offense.rb', line 296
def ==(other) COMPARISON_ATTRIBUTES.all? do |attribute| public_send(attribute) == other.public_send(attribute) end end
#column_length
[ GitHub ]# File 'lib/rubocop/cop/offense.rb', line 245
def column_length if first_line == last_line column_range.count else source_line.length - column end end
#column_range
[ GitHub ]# File 'lib/rubocop/cop/offense.rb', line 269
def column_range location.column_range end
#corrections_from(corrector) (private)
Extracted eagerly rather than on demand because the offense is frozen, and because a corrector cannot be serialized - the cache and parallel workers would otherwise hand back offenses that have lost their edits.
# File 'lib/rubocop/cop/offense.rb', line 328
def corrections_from(corrector) return EMPTY_CORRECTIONS unless corrector corrector.as_replacements.map do |range, replacement| Correction.new(range.begin_pos, range.end_pos, replacement.freeze).freeze end end
#eql?(other)
Alias for #==.
# File 'lib/rubocop/cop/offense.rb', line 302
alias eql? ==
#first_line
[ GitHub ]# File 'lib/rubocop/cop/offense.rb', line 254
def first_line location.first_line end
#hash
[ GitHub ]# File 'lib/rubocop/cop/offense.rb', line 304
def hash COMPARISON_ATTRIBUTES.map { |attribute| public_send(attribute) }.hash end
#highlighted_area ⇒ Parser::Source::Range
# File 'lib/rubocop/cop/offense.rb', line 220
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
[ GitHub ]# File 'lib/rubocop/cop/offense.rb', line 264
def last_column location.last_column end
#last_line
[ GitHub ]# File 'lib/rubocop/cop/offense.rb', line 259
def last_line location.last_line end
#marshal_dump
[ GitHub ]# File 'lib/rubocop/cop/offense.rb', line 157
def marshal_dump [@severity, @location, @message, @cop_name, @status, @justification, @corrections, @correction_safe] end
#marshal_load(array)
[ GitHub ]# File 'lib/rubocop/cop/offense.rb', line 162
def marshal_load(array) @severity, @location, @message, @cop_name, @status, @justification, @corrections, @correction_safe = array @corrections ||= EMPTY_CORRECTIONS @correction_safe = true if @correction_safe.nil? @line = @location.line @column = @location.column end
#real_column
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.
# File 'lib/rubocop/cop/offense.rb', line 278
def real_column column + 1 end
#real_last_column
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.
# File 'lib/rubocop/cop/offense.rb', line 288
def real_last_column last_column.zero? ? 1 : last_column end
#source_line
[ GitHub ]# File 'lib/rubocop/cop/offense.rb', line 240
def source_line location.source_line end
#to_s
This is just for debugging purpose.
# File 'lib/rubocop/cop/offense.rb', line 227
def to_s format('%<severity>s:%3<line>d:%3<column>d: %<message>s', severity: severity.code, line: line, column: real_column, message: ) end