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.
[:line, :column, :cop_name, :message, :severity]
Instance Attribute Summary collapse
-
#cop_name ⇒ String
readonly
A cop class name without namespace.
-
#corrected ⇒ Boolean
(also: #corrected?)
readonly
Whether this offense is automatically corrected.
-
#disabled? ⇒ Boolean
readonly
Whether this offense was locally disabled 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
Instance Method Summary collapse
-
#<=>(other) ⇒ Integer
Returns
-1
,0
or+1
if this offense is less than, equal to, or greater thanother
. -
#==(other) ⇒ Boolean
(also: #eql?)
Returns
true
if two offenses contain same attributes. - #column ⇒ Object private
- #hash ⇒ Object
-
#initialize(severity, location, message, cop_name, status = :uncorrected) ⇒ Offense
constructor
private
A new instance of Offense.
- #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.
-
#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.
75 76 77 78 79 80 81 82 83 |
# File 'lib/rubocop/cop/offense.rb', line 75 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 namespace. i.e. type of the violation.
51 52 53 |
# File 'lib/rubocop/cop/offense.rb', line 51 def cop_name @cop_name end |
#corrected ⇒ Boolean (readonly) Also known as: corrected?
Returns whether this offense is automatically corrected.
59 60 61 |
# File 'lib/rubocop/cop/offense.rb', line 59 def corrected @status == :unsupported ? nil : @status == :corrected end |
#disabled? ⇒ Boolean (readonly)
Returns whether this offense was locally disabled where it occurred.
70 71 72 |
# File 'lib/rubocop/cop/offense.rb', line 70 def disabled? @status == :disabled 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 |
Instance Method Details
#<=>(other) ⇒ Integer
Returns -1
, 0
or +1
if this offense is less than, equal to, or greater than other
.
136 137 138 139 140 141 142 |
# File 'lib/rubocop/cop/offense.rb', line 136 def <=>(other) COMPARISON_ATTRIBUTES.each do |attribute| result = send(attribute) <=> other.send(attribute) return result unless result == 0 end 0 end |
#==(other) ⇒ Boolean Also known as: eql?
Returns true
if two offenses contain same attributes
115 116 117 118 119 |
# File 'lib/rubocop/cop/offense.rb', line 115 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.
98 99 100 |
# File 'lib/rubocop/cop/offense.rb', line 98 def column location.column end |
#hash ⇒ Object
123 124 125 126 127 |
# File 'lib/rubocop/cop/offense.rb', line 123 def hash COMPARISON_ATTRIBUTES.reduce(0) do |hash, attribute| hash ^ send(attribute).hash end 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.
93 94 95 |
# File 'lib/rubocop/cop/offense.rb', line 93 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.
107 108 109 |
# File 'lib/rubocop/cop/offense.rb', line 107 def real_column column + 1 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.
87 88 89 90 |
# File 'lib/rubocop/cop/offense.rb', line 87 def to_s format('%s:%3d:%3d: %s', severity.code, line, real_column, ) end |