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.



8
9
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
# File 'lib/theme_check/offense.rb', line 8

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

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

Instance Attribute Details

#check ⇒ Object (readonly)

Returns the value of attribute check.



6
7
8
# File 'lib/theme_check/offense.rb', line 6

def check
  @check
end

#correction ⇒ Object (readonly)

Returns the value of attribute correction.



6
7
8
# File 'lib/theme_check/offense.rb', line 6

def correction
  @correction
end

#line_number ⇒ Object (readonly)

Returns the value of attribute line_number.



6
7
8
# File 'lib/theme_check/offense.rb', line 6

def line_number
  @line_number
end

#markup ⇒ Object (readonly)

Returns the value of attribute markup.



6
7
8
# File 'lib/theme_check/offense.rb', line 6

def markup
  @markup
end

#message ⇒ Object (readonly)

Returns the value of attribute message.



6
7
8
# File 'lib/theme_check/offense.rb', line 6

def message
  @message
end

#node ⇒ Object (readonly)

Returns the value of attribute node.



6
7
8
# File 'lib/theme_check/offense.rb', line 6

def node
  @node
end

#template ⇒ Object (readonly)

Returns the value of attribute template.



6
7
8
# File 'lib/theme_check/offense.rb', line 6

def template
  @template
end

Instance Method Details

#check_name ⇒ Object



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

def check_name
  check.class.name.demodulize
end

#code_name ⇒ Object



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

def code_name
  check.code_name
end

#correct ⇒ Object



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

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

#correctable? ⇒ Boolean

Returns:

  • (Boolean)


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

def correctable?
  line_number && correction
end

#doc ⇒ Object



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

def doc
  check.doc
end

#end_column ⇒ Object



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

def end_column
  return 0 unless line_number && markup
  markup_end = markup.split("\n").last
  template.full_line(end_line + 1).index(markup_end) + markup_end.size
end

#end_line ⇒ Object



57
58
59
60
61
62
63
# File 'lib/theme_check/offense.rb', line 57

def end_line
  if markup
    start_line + markup.count("\n")
  else
    start_line
  end
end

#location ⇒ Object



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

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

#markup_start_in_excerpt ⇒ Object



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

def markup_start_in_excerpt
  source_excerpt.index(markup) if markup
end

#severity ⇒ Object



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

def severity
  check.severity
end

#source_excerpt ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/theme_check/offense.rb', line 40

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



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

def start_column
  return 0 unless line_number && markup
  template.full_line(start_line + 1).index(markup.split("\n", 2).first)
end

#start_line ⇒ Object



52
53
54
55
# File 'lib/theme_check/offense.rb', line 52

def start_line
  return 0 unless line_number
  line_number - 1
end

#to_s ⇒ Object



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

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