Class: Rubocop::Cop::Offence

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

Instance Method Summary collapse

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.



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/rubocop/cop/offence.rb', line 62

def initialize(severity, location, message, 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 = message.freeze
  @cop_name = cop_name.freeze
  freeze
end

Instance Attribute Details

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



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

def column
  @column
end

#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/offence.rb', line 53

def cop_name
  @cop_name
end

#lineObject (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/offence.rb', line 56

def line
  @line
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/offence.rb', line 30

def location
  @location
end

#messageString (readonly)

Returns human-readable message.

Examples:

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

Returns:

  • (String)

    human-readable message



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

def message
  @message
end

#severitySymbol (readonly)

Returns severity. any of :refactor, :convention, :warning, :error or :fatal.

Returns:

  • (Symbol)

    severity. any of :refactor, :convention, :warning, :error or :fatal.



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

def severity
  @severity
end

Instance Method Details

#<=>(other) ⇒ Integer

Returns -1, 0 or +1 if this offence is less than, equal to, or greater than other.

Returns:

  • (Integer)

    comparison result



127
128
129
130
131
132
133
# File 'lib/rubocop/cop/offence.rb', line 127

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

Returns:

  • (Boolean)

    returns true if two offences contain same attributes



114
115
116
117
118
# File 'lib/rubocop/cop/offence.rb', line 114

def ==(other)
  severity == other.severity && line == other.line &&
    column == other.column && message == other.message &&
    cop_name == other.cop_name
end

#clang_severityObject

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.



87
88
89
90
91
92
93
94
# File 'lib/rubocop/cop/offence.rb', line 87

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_severityObject

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.



82
83
84
# File 'lib/rubocop/cop/offence.rb', line 82

def encode_severity
  @severity.to_s[0].upcase
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.



106
107
108
# File 'lib/rubocop/cop/offence.rb', line 106

def real_column
  column + 1
end

#severity_levelObject

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.



97
98
99
# File 'lib/rubocop/cop/offence.rb', line 97

def severity_level
  SEVERITIES.index(severity) + 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.



76
77
78
79
# File 'lib/rubocop/cop/offence.rb', line 76

def to_s
  sprintf("#{encode_severity}:%3d:%3d: %s",
          line, real_column, message)
end