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]

Instance Attribute Summary collapse

Instance Method Summary collapse

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.



63
64
65
66
67
68
69
70
# File 'lib/rubocop/cop/offense.rb', line 63

def initialize(severity, location, message, cop_name, corrected = false)
  @severity = RuboCop::Cop::Severity.new(severity)
  @location = location
  @message = message.freeze
  @cop_name = cop_name.freeze
  @corrected = corrected
  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.



51
52
53
# File 'lib/rubocop/cop/offense.rb', line 51

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.



59
60
61
# File 'lib/rubocop/cop/offense.rb', line 59

def corrected
  @corrected
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:



28
29
30
# File 'lib/rubocop/cop/offense.rb', line 28

def location
  @location
end

#messageString (readonly)

Returns human-readable message.

Examples:

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

Returns:

  • (String)

    human-readable message



39
40
41
# File 'lib/rubocop/cop/offense.rb', line 39

def message
  @message
end

#severityRuboCop::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.

Returns:

  • (Integer)

    comparison result



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

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

Returns:

  • (Boolean)

    returns true if two offenses contain same attributes



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

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.



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

def column
  location.column
end

#hashObject



110
111
112
113
114
# File 'lib/rubocop/cop/offense.rb', line 110

def hash
  COMPARISON_ATTRIBUTES.reduce(0) do |hash, attribute|
    hash ^ send(attribute).hash
  end
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.



80
81
82
# File 'lib/rubocop/cop/offense.rb', line 80

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.



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

def real_column
  column + 1
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.



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

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