Class: RuboCop::Cop::Offense

Inherits:
Object
  • Object
show all
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].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

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.



59
60
61
62
63
64
65
66
67
# File 'lib/rubocop/cop/offense.rb', line 59

def initialize(severity, location, message, cop_name,
               status = :uncorrected)
  @severity = RuboCop::Cop::Severity.new(severity)
  @location = location
  @message = message.freeze
  @cop_name = cop_name.freeze
  @status = status
  freeze
end

Instance Attribute Details

#cop_nameString (readonly)

Returns a cop class name without namespace. i.e. type of the violation.

Examples:

'LineLength'

Returns:

  • (String)

    a cop class name without namespace. i.e. type of the violation.



53
54
55
# File 'lib/rubocop/cop/offense.rb', line 53

def cop_name
  @cop_name
end

#correctedBoolean (readonly) Also known as: corrected?

Returns whether this offense is automatically corrected.

Returns:

  • (Boolean)

    whether this offense is automatically corrected.



75
76
77
# File 'lib/rubocop/cop/offense.rb', line 75

def corrected
  @status == :unsupported ? nil : @status == :corrected
end

#disabled?Boolean (readonly)

Returns whether this offense was locally disabled where it occurred.

Returns:

  • (Boolean)

    whether this offense was locally disabled where it occurred



86
87
88
# File 'lib/rubocop/cop/offense.rb', line 86

def disabled?
  @status == :disabled
end

#locationParser::Source::Range (readonly)

Returns the location where the violation is detected.

Returns:

  • (Parser::Source::Range)

    the location where the violation is detected.

See Also:



30
31
32
# File 'lib/rubocop/cop/offense.rb', line 30

def location
  @location
end

#messageString (readonly)

Returns human-readable message.

Examples:

'Line is too long. [90/80]'

Returns:

  • (String)

    human-readable message



41
42
43
# File 'lib/rubocop/cop/offense.rb', line 41

def message
  @message
end

#severityRuboCop::Cop::Severity (readonly)



19
20
21
# File 'lib/rubocop/cop/offense.rb', line 19

def severity
  @severity
end

#statusObject (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.



56
57
58
# File 'lib/rubocop/cop/offense.rb', line 56

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`.

Returns:

  • (Integer)

    comparison result



180
181
182
183
184
185
186
# File 'lib/rubocop/cop/offense.rb', line 180

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

Returns:

  • (Boolean)

    returns ‘true` if two offenses contain same attributes



159
160
161
162
163
# File 'lib/rubocop/cop/offense.rb', line 159

def ==(other)
  COMPARISON_ATTRIBUTES.all? do |attribute|
    send(attribute) == other.send(attribute)
  end
end

#columnObject

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.



113
114
115
# File 'lib/rubocop/cop/offense.rb', line 113

def column
  location.column
end

#column_lengthObject

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.



123
124
125
126
127
128
129
# File 'lib/rubocop/cop/offense.rb', line 123

def column_length
  if first_line == last_line
    column_range.count
  else
    source_line.length - column
  end
end

#column_rangeObject

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.



142
143
144
# File 'lib/rubocop/cop/offense.rb', line 142

def column_range
  location.column_range
end

#first_lineObject

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.



132
133
134
# File 'lib/rubocop/cop/offense.rb', line 132

def first_line
  location.first_line
end

#hashObject



167
168
169
170
171
# File 'lib/rubocop/cop/offense.rb', line 167

def hash
  COMPARISON_ATTRIBUTES.reduce(0) do |hash, attribute|
    hash ^ send(attribute).hash
  end
end

#highlighted_areaParser::Source::Range

Returns the range of the code that is highlighted.

Returns:

  • (Parser::Source::Range)

    the range of the code that is highlighted



94
95
96
97
98
# File 'lib/rubocop/cop/offense.rb', line 94

def highlighted_area
  Parser::Source::Range.new(source_line,
                            column,
                            column + column_length)
end

#last_lineObject

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.



137
138
139
# File 'lib/rubocop/cop/offense.rb', line 137

def last_line
  location.last_line
end

#lineObject

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.



108
109
110
# File 'lib/rubocop/cop/offense.rb', line 108

def line
  location.line
end

#real_columnObject

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.



151
152
153
# File 'lib/rubocop/cop/offense.rb', line 151

def real_column
  column + 1
end

#source_lineObject

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.



118
119
120
# File 'lib/rubocop/cop/offense.rb', line 118

def source_line
  location.source_line
end

#to_sObject

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.



102
103
104
105
# File 'lib/rubocop/cop/offense.rb', line 102

def to_s
  format('%s:%3d:%3d: %s',
         severity.code, line, real_column, message)
end