Class: ThemeCheck::Offense

Inherits:
Object
  • Object
show all
Defined in:
lib/theme_check/offense.rb

Constant Summary collapse

MAX_SOURCE_EXCERPT_SIZE =
120

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(check:, message: nil, template: nil, node: nil, markup: nil, line_number: nil, correction: nil) ⇒ Offense

Returns a new instance of Offense.

Raises:

  • (ArgumentError)


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/theme_check/offense.rb', line 10

def initialize(check:, message: nil, template: nil, node: nil, markup: nil, line_number: nil, correction: nil)
  @check = check
  @correction = correction

  if message
    @message = message
  elsif defined?(check.class::MESSAGE)
    @message = check.class::MESSAGE
  else
    raise ArgumentError, "message required"
  end

  @node = node
  if node
    @template = node.template
  elsif template
    @template = template
  end

  @markup = if markup
    markup
  else
    node&.markup
  end

  raise ArgumentError, "Offense markup cannot be an empty string" if @markup.is_a?(String) && @markup.empty?

  @line_number = if line_number
    line_number
  elsif @node
    @node.line_number
  end

  @start_position = nil
  @end_position = nil
end

Instance Attribute Details

#check ⇒ Object (readonly)

Returns the value of attribute check.



8
9
10
# File 'lib/theme_check/offense.rb', line 8

def check
  @check
end

#correction ⇒ Object (readonly)

Returns the value of attribute correction.



8
9
10
# File 'lib/theme_check/offense.rb', line 8

def correction
  @correction
end

#line_number ⇒ Object (readonly)

Returns the value of attribute line_number.



8
9
10
# File 'lib/theme_check/offense.rb', line 8

def line_number
  @line_number
end

#markup ⇒ Object (readonly)

Returns the value of attribute markup.



8
9
10
# File 'lib/theme_check/offense.rb', line 8

def markup
  @markup
end

#message ⇒ Object (readonly)

Returns the value of attribute message.



8
9
10
# File 'lib/theme_check/offense.rb', line 8

def message
  @message
end

#node ⇒ Object (readonly)

Returns the value of attribute node.



8
9
10
# File 'lib/theme_check/offense.rb', line 8

def node
  @node
end

#template ⇒ Object (readonly)

Returns the value of attribute template.



8
9
10
# File 'lib/theme_check/offense.rb', line 8

def template
  @template
end

Instance Method Details

#check_name ⇒ Object



87
88
89
# File 'lib/theme_check/offense.rb', line 87

def check_name
  StringHelpers.demodulize(check.class.name)
end

#code_name ⇒ Object



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

def code_name
  check.code_name
end

#correct ⇒ Object



104
105
106
107
108
109
# File 'lib/theme_check/offense.rb', line 104

def correct
  if correctable?
    corrector = Corrector.new(template: template)
    correction.call(corrector)
  end
end

#correctable? ⇒ Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/theme_check/offense.rb', line 100

def correctable?
  line_number && correction
end

#doc ⇒ Object



91
92
93
# File 'lib/theme_check/offense.rb', line 91

def doc
  check.doc
end

#end_column ⇒ Object



71
72
73
# File 'lib/theme_check/offense.rb', line 71

def end_column
  end_position.column
end

#end_line ⇒ Object



67
68
69
# File 'lib/theme_check/offense.rb', line 67

def end_line
  end_position.line
end

#location ⇒ Object



95
96
97
98
# File 'lib/theme_check/offense.rb', line 95

def location
  tokens = [template&.relative_path, line_number].compact
  tokens.join(":") if tokens.any?
end

#markup_start_in_excerpt ⇒ Object



79
80
81
# File 'lib/theme_check/offense.rb', line 79

def markup_start_in_excerpt
  source_excerpt.index(markup) if markup
end

#severity ⇒ Object



83
84
85
# File 'lib/theme_check/offense.rb', line 83

def severity
  check.severity
end

#source_excerpt ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/theme_check/offense.rb', line 47

def source_excerpt
  return unless line_number
  @source_excerpt ||= begin
    excerpt = template.source_excerpt(line_number)
    if excerpt.size > MAX_SOURCE_EXCERPT_SIZE
      excerpt[0, MAX_SOURCE_EXCERPT_SIZE - 3] + '...'
    else
      excerpt
    end
  end
end

#start_column ⇒ Object



63
64
65
# File 'lib/theme_check/offense.rb', line 63

def start_column
  start_position.column
end

#start_line ⇒ Object



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

def start_line
  start_position.line
end

#to_s ⇒ Object



111
112
113
114
115
116
117
# File 'lib/theme_check/offense.rb', line 111

def to_s
  if template
    "#{message} at #{location}"
  else
    message
  end
end