Class: Rubocop::Cop::Offence
- Inherits:
-
Object
- Object
- Rubocop::Cop::Offence
- Includes:
- Comparable
- Defined in:
- lib/rubocop/cop/offence.rb
Overview
An Offence represents a style violation detected by RuboCop.
Constant Summary collapse
- SEVERITIES =
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.
[:refactor, :convention, :warning, :error, :fatal]
Instance Attribute Summary collapse
- #column ⇒ Object readonly private
-
#cop_name ⇒ String
readonly
A cop class name without namespace.
- #line ⇒ Object readonly private
-
#location ⇒ Parser::Source::Range
readonly
The location where the violation is detected.
-
#message ⇒ String
readonly
Human-readable message.
-
#severity ⇒ Symbol
readonly
Severity.
Class Method Summary collapse
Instance Method Summary collapse
-
#<=>(other) ⇒ Integer
Returns
-1
,0
or+1
if this offence is less than, equal to, or greater thanother
. -
#==(other) ⇒ Boolean
Returns
true
if two offences contain same attributes. - #clang_severity ⇒ Object private
- #encode_severity ⇒ Object private
-
#initialize(severity, location, message, cop_name) ⇒ Offence
constructor
private
A new instance of Offence.
-
#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.
- #severity_level ⇒ Object private
- #to_s ⇒ Object private
Constructor Details
#initialize(severity, location, message, cop_name) ⇒ Offence
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 Offence.
71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/rubocop/cop/offence.rb', line 71 def initialize(severity, location, , cop_name) unless SEVERITIES.include?(severity) fail ArgumentError, "Unknown severity: #{severity}" end @severity = severity.freeze @location = location.freeze @line = location.line.freeze @column = location.column.freeze @message = .freeze @cop_name = cop_name.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.
68 69 70 |
# File 'lib/rubocop/cop/offence.rb', line 68 def column @column end |
#cop_name ⇒ String (readonly)
Returns a cop class name without namespace. i.e. type of the violation.
62 63 64 |
# File 'lib/rubocop/cop/offence.rb', line 62 def cop_name @cop_name 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.
65 66 67 |
# File 'lib/rubocop/cop/offence.rb', line 65 def line @line end |
#location ⇒ Parser::Source::Range (readonly)
Returns the location where the violation is detected.
39 40 41 |
# File 'lib/rubocop/cop/offence.rb', line 39 def location @location end |
#message ⇒ String (readonly)
Returns human-readable message.
50 51 52 |
# File 'lib/rubocop/cop/offence.rb', line 50 def @message end |
#severity ⇒ Symbol (readonly)
Returns severity.
any of :refactor
, :convention
, :warning
, :error
or :fatal
.
28 29 30 |
# File 'lib/rubocop/cop/offence.rb', line 28 def severity @severity end |
Class Method Details
.from_diagnostic(diagnostic) ⇒ Object
12 13 14 15 16 17 18 19 |
# File 'lib/rubocop/cop/offence.rb', line 12 def self.from_diagnostic(diagnostic) new( diagnostic.level, diagnostic.location, diagnostic., 'Syntax' ) end |
Instance Method Details
#<=>(other) ⇒ Integer
Returns -1
, 0
or +1
if this offence is less than, equal to, or greater than other
.
136 137 138 139 140 141 142 |
# File 'lib/rubocop/cop/offence.rb', line 136 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 offences contain same attributes
123 124 125 126 127 |
# File 'lib/rubocop/cop/offence.rb', line 123 def ==(other) severity == other.severity && line == other.line && column == other.column && == other. && cop_name == other.cop_name end |
#clang_severity ⇒ 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.
96 97 98 99 100 101 102 103 |
# File 'lib/rubocop/cop/offence.rb', line 96 def clang_severity case @severity when :fatal then 'F'.color(:red) when :error then 'E'.color(:red) when :warning then 'W'.color(:magenta) when :convention then 'C'.color(:yellow) end end |
#encode_severity ⇒ 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.
91 92 93 |
# File 'lib/rubocop/cop/offence.rb', line 91 def encode_severity @severity.to_s[0].upcase 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.
115 116 117 |
# File 'lib/rubocop/cop/offence.rb', line 115 def real_column column + 1 end |
#severity_level ⇒ 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.
106 107 108 |
# File 'lib/rubocop/cop/offence.rb', line 106 def severity_level SEVERITIES.index(severity) + 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.
85 86 87 88 |
# File 'lib/rubocop/cop/offence.rb', line 85 def to_s sprintf("#{encode_severity}:%3d:%3d: %s", line, real_column, ) end |