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.
Instance Attribute Summary collapse
- #column ⇒ Object readonly private
-
#cop_name ⇒ String
readonly
A cop class name without namespace.
-
#corrected ⇒ Boolean
(also: #corrected?)
readonly
Whether this offense is automatically corrected.
- #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
Instance Method Summary collapse
-
#<=>(other) ⇒ Integer
Returns
-1
,0
or+1
if this offense is less than, equal to, or greater thanother
. -
#==(other) ⇒ Boolean
Returns
true
if two offenses contain same attributes. -
#initialize(severity, location, message, cop_name, corrected = false) ⇒ Offense
constructor
private
A new instance of Offense.
-
#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, corrected = false) ⇒ 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.
66 67 68 69 70 71 72 73 74 75 |
# File 'lib/rubocop/cop/offense.rb', line 66 def initialize(severity, location, , cop_name, corrected = false) @severity = Rubocop::Cop::Severity.new(severity) @location = location.freeze @line = location.line.freeze @column = location.column.freeze @message = .freeze @cop_name = cop_name.freeze @corrected = corrected.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.
63 64 65 |
# File 'lib/rubocop/cop/offense.rb', line 63 def column @column end |
#cop_name ⇒ String (readonly)
Returns a cop class name without namespace. i.e. type of the violation.
48 49 50 |
# File 'lib/rubocop/cop/offense.rb', line 48 def cop_name @cop_name end |
#corrected ⇒ Boolean (readonly) Also known as: corrected?
Returns whether this offense is automatically corrected.
56 57 58 |
# File 'lib/rubocop/cop/offense.rb', line 56 def corrected @corrected 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.
60 61 62 |
# File 'lib/rubocop/cop/offense.rb', line 60 def line @line end |
#location ⇒ Parser::Source::Range (readonly)
Returns the location where the violation is detected.
25 26 27 |
# File 'lib/rubocop/cop/offense.rb', line 25 def location @location end |
#message ⇒ String (readonly)
Returns human-readable message.
36 37 38 |
# File 'lib/rubocop/cop/offense.rb', line 36 def @message end |
#severity ⇒ Rubocop::Cop::Severity (readonly)
14 15 16 |
# File 'lib/rubocop/cop/offense.rb', line 14 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
.
110 111 112 113 114 115 116 |
# File 'lib/rubocop/cop/offense.rb', line 110 def <=>(other) [:line, :column, :cop_name, :message].each do |attribute| result = send(attribute) <=> other.send(attribute) return result unless result == 0 end 0 end |
#==(other) ⇒ Boolean
Returns true
if two offenses contain same attributes
97 98 99 100 101 |
# File 'lib/rubocop/cop/offense.rb', line 97 def ==(other) severity == other.severity && line == other.line && column == other.column && == other. && cop_name == other.cop_name 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.
89 90 91 |
# File 'lib/rubocop/cop/offense.rb', line 89 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.
79 80 81 82 |
# File 'lib/rubocop/cop/offense.rb', line 79 def to_s format('%s:%3d:%3d: %s', severity.code, line, real_column, ) end |