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.



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
# 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

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

  @start_position = nil
  @end_position = nil
end

Instance Attribute Details

#checkObject (readonly)

Returns the value of attribute check.



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

def check
  @check
end

#correctionObject (readonly)

Returns the value of attribute correction.



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

def correction
  @correction
end

#line_numberObject (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

#markupObject (readonly)

Returns the value of attribute markup.



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

def markup
  @markup
end

#messageObject (readonly)

Returns the value of attribute message.



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

def message
  @message
end

#nodeObject (readonly)

Returns the value of attribute node.



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

def node
  @node
end

#templateObject (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_nameObject



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

def check_name
  check.class.name.demodulize
end

#code_nameObject



73
74
75
# File 'lib/theme_check/offense.rb', line 73

def code_name
  check.code_name
end

#correctObject



102
103
104
105
106
107
# File 'lib/theme_check/offense.rb', line 102

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

#correctable?Boolean

Returns:

  • (Boolean)


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

def correctable?
  line_number && correction
end

#docObject



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

def doc
  check.doc
end

#end_columnObject



69
70
71
# File 'lib/theme_check/offense.rb', line 69

def end_column
  end_position.column
end

#end_lineObject



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

def end_line
  end_position.line
end

#locationObject



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

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

#markup_start_in_excerptObject



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

def markup_start_in_excerpt
  source_excerpt.index(markup) if markup
end

#severityObject



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

def severity
  check.severity
end

#source_excerptObject



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

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_columnObject



61
62
63
# File 'lib/theme_check/offense.rb', line 61

def start_column
  start_position.column
end

#start_lineObject



57
58
59
# File 'lib/theme_check/offense.rb', line 57

def start_line
  start_position.line
end

#to_sObject



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

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