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
Instance Attribute Summary collapse
-
#cop_name ⇒ String
readonly
A cop class name without department.
-
#correctable? ⇒ Boolean
readonly
Whether this offense can be automatically corrected via autocorrect or a todo.
-
#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.
-
#disabled? ⇒ Boolean
readonly
Whether this offense was locally disabled with a disable or todo where it occurred.
-
#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 `+1` if this offense is less than, equal to, or greater than `other`.
-
#==(other) ⇒ Boolean
(also: #eql?)
Returns ‘true` if two offenses contain same attributes.
- #column ⇒ Object private
- #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) ⇒ Offense
constructor
private
A new instance of Offense.
- #last_column ⇒ Object private
- #last_line ⇒ Object private
- #line ⇒ Object private
-
#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.
- #source_line ⇒ Object private
-
#to_s ⇒ Object
private
This is just for debugging purpose.
Constructor Details
#initialize(severity, location, message, cop_name, status = :uncorrected) ⇒ 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.
58 59 60 61 62 63 64 65 66 |
# File 'lib/rubocop/cop/offense.rb', line 58 def initialize(severity, location, , cop_name, status = :uncorrected) @severity = RuboCop::Cop::Severity.new(severity) @location = location @message = .freeze @cop_name = cop_name.freeze @status = status freeze end |
Instance Attribute Details
#cop_name ⇒ String (readonly)
Returns a cop class name without department. i.e. type of the violation.
52 53 54 |
# File 'lib/rubocop/cop/offense.rb', line 52 def cop_name @cop_name end |
#correctable? ⇒ Boolean (readonly)
Returns whether this offense can be automatically corrected via autocorrect or a todo.
75 76 77 |
# File 'lib/rubocop/cop/offense.rb', line 75 def correctable? @status != :unsupported end |
#corrected? ⇒ Boolean (readonly)
Returns whether this offense is automatically corrected via autocorrect or a todo.
86 87 88 |
# File 'lib/rubocop/cop/offense.rb', line 86 def corrected? @status == :corrected || @status == :corrected_with_todo end |
#corrected_with_todo? ⇒ Boolean (readonly)
Returns whether this offense is automatically disabled via a todo.
96 97 98 |
# File 'lib/rubocop/cop/offense.rb', line 96 def corrected_with_todo? @status == :corrected_with_todo end |
#disabled? ⇒ Boolean (readonly)
Returns whether this offense was locally disabled with a disable or todo where it occurred.
107 108 109 |
# File 'lib/rubocop/cop/offense.rb', line 107 def disabled? @status == :disabled || @status == :todo end |
#location ⇒ Parser::Source::Range (readonly)
Returns the location where the violation is detected.
29 30 31 |
# File 'lib/rubocop/cop/offense.rb', line 29 def location @location end |
#message ⇒ String (readonly)
Returns human-readable message.
40 41 42 |
# File 'lib/rubocop/cop/offense.rb', line 40 def @message end |
#severity ⇒ RuboCop::Cop::Severity (readonly)
18 19 20 |
# File 'lib/rubocop/cop/offense.rb', line 18 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.
55 56 57 |
# File 'lib/rubocop/cop/offense.rb', line 55 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`.
207 208 209 210 211 212 213 |
# File 'lib/rubocop/cop/offense.rb', line 207 def <=>(other) COMPARISON_ATTRIBUTES.each do |attribute| result = send(attribute) <=> other.send(attribute) return result unless result.zero? end 0 end |
#==(other) ⇒ Boolean Also known as: eql?
Returns ‘true` if two offenses contain same attributes
186 187 188 189 190 |
# File 'lib/rubocop/cop/offense.rb', line 186 def ==(other) COMPARISON_ATTRIBUTES.all? do |attribute| send(attribute) == other.send(attribute) end end |
#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.
135 136 137 |
# File 'lib/rubocop/cop/offense.rb', line 135 def column location.column 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.
145 146 147 148 149 150 151 |
# File 'lib/rubocop/cop/offense.rb', line 145 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.
169 170 171 |
# File 'lib/rubocop/cop/offense.rb', line 169 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.
154 155 156 |
# File 'lib/rubocop/cop/offense.rb', line 154 def first_line location.first_line end |
#hash ⇒ Object
194 195 196 197 198 |
# File 'lib/rubocop/cop/offense.rb', line 194 def hash COMPARISON_ATTRIBUTES.reduce(0) do |hash, attribute| hash ^ send(attribute).hash end end |
#highlighted_area ⇒ Parser::Source::Range
Returns the range of the code that is highlighted.
115 116 117 118 119 |
# File 'lib/rubocop/cop/offense.rb', line 115 def highlighted_area Parser::Source::Range.new(source_line, 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.
164 165 166 |
# File 'lib/rubocop/cop/offense.rb', line 164 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.
159 160 161 |
# File 'lib/rubocop/cop/offense.rb', line 159 def last_line location.last_line end |
#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.
130 131 132 |
# File 'lib/rubocop/cop/offense.rb', line 130 def line location.line 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.
178 179 180 |
# File 'lib/rubocop/cop/offense.rb', line 178 def real_column column + 1 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.
140 141 142 |
# File 'lib/rubocop/cop/offense.rb', line 140 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.
123 124 125 126 127 |
# File 'lib/rubocop/cop/offense.rb', line 123 def to_s format('%<severity>s:%3<line>d:%3<column>d: %<message>s', severity: severity.code, line: line, column: real_column, message: ) end |