Class: ThemeCheck::Offense
- Inherits:
-
Object
- Object
- ThemeCheck::Offense
- Defined in:
- lib/theme_check/offense.rb
Constant Summary collapse
- MAX_SOURCE_EXCERPT_SIZE =
120
Instance Attribute Summary collapse
-
#check ⇒ Object
readonly
Returns the value of attribute check.
-
#correction ⇒ Object
readonly
Returns the value of attribute correction.
-
#line_number ⇒ Object
readonly
Returns the value of attribute line_number.
-
#markup ⇒ Object
readonly
Returns the value of attribute markup.
-
#message ⇒ Object
readonly
Returns the value of attribute message.
-
#node ⇒ Object
readonly
Returns the value of attribute node.
-
#template ⇒ Object
readonly
Returns the value of attribute template.
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
- #check_name ⇒ Object
- #code_name ⇒ Object
- #correct ⇒ Object
- #correctable? ⇒ Boolean
- #doc ⇒ Object
- #end_column ⇒ Object
- #end_index ⇒ Object
- #end_line ⇒ Object
-
#initialize(check:, message: nil, template: nil, node: nil, markup: nil, line_number: nil, correction: nil) ⇒ Offense
constructor
A new instance of Offense.
- #location ⇒ Object
- #markup_start_in_excerpt ⇒ Object
- #severity ⇒ Object
- #single_file? ⇒ Boolean
- #source_excerpt ⇒ Object
- #start_column ⇒ Object
- #start_index ⇒ Object
- #start_line ⇒ Object
- #to_s ⇒ Object
- #whole_theme? ⇒ Boolean
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 39 40 41 42 43 |
# 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 = elsif defined?(check.class::MESSAGE) @message = check.class::MESSAGE else raise ArgumentError, "message required" end @node = node @template = nil 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 @position = Position.new(@markup, @template&.source, @line_number) 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 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
#==(other) ⇒ Object Also known as: eql?
125 126 127 128 129 130 131 132 |
# File 'lib/theme_check/offense.rb', line 125 def ==(other) other.is_a?(Offense) && code_name == other.code_name && == other. && location == other.location && start_index == other.start_index && end_index == other.end_index end |
#check_name ⇒ Object
93 94 95 |
# File 'lib/theme_check/offense.rb', line 93 def check_name StringHelpers.demodulize(check.class.name) end |
#code_name ⇒ Object
81 82 83 |
# File 'lib/theme_check/offense.rb', line 81 def code_name check.code_name end |
#correct ⇒ Object
110 111 112 113 114 115 |
# File 'lib/theme_check/offense.rb', line 110 def correct if correctable? corrector = Corrector.new(template: template) correction.call(corrector) end end |
#correctable? ⇒ Boolean
106 107 108 |
# File 'lib/theme_check/offense.rb', line 106 def correctable? line_number && correction end |
#doc ⇒ Object
97 98 99 |
# File 'lib/theme_check/offense.rb', line 97 def doc check.doc end |
#end_column ⇒ Object
77 78 79 |
# File 'lib/theme_check/offense.rb', line 77 def end_column @position.end_column end |
#end_index ⇒ Object
69 70 71 |
# File 'lib/theme_check/offense.rb', line 69 def end_index @position.end_index end |
#end_line ⇒ Object
73 74 75 |
# File 'lib/theme_check/offense.rb', line 73 def end_line @position.end_row end |
#location ⇒ Object
101 102 103 104 |
# File 'lib/theme_check/offense.rb', line 101 def location tokens = [template&.relative_path, line_number].compact tokens.join(":") if tokens.any? end |
#markup_start_in_excerpt ⇒ Object
85 86 87 |
# File 'lib/theme_check/offense.rb', line 85 def markup_start_in_excerpt source_excerpt.index(markup) if markup end |
#severity ⇒ Object
89 90 91 |
# File 'lib/theme_check/offense.rb', line 89 def severity check.severity end |
#single_file? ⇒ Boolean
121 122 123 |
# File 'lib/theme_check/offense.rb', line 121 def single_file? check.single_file? end |
#source_excerpt ⇒ Object
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_column ⇒ Object
65 66 67 |
# File 'lib/theme_check/offense.rb', line 65 def start_column @position.start_column end |
#start_index ⇒ Object
57 58 59 |
# File 'lib/theme_check/offense.rb', line 57 def start_index @position.start_index end |
#start_line ⇒ Object
61 62 63 |
# File 'lib/theme_check/offense.rb', line 61 def start_line @position.start_row end |
#to_s ⇒ Object
135 136 137 138 139 140 141 |
# File 'lib/theme_check/offense.rb', line 135 def to_s if template "#{} at #{location}" else end end |
#whole_theme? ⇒ Boolean
117 118 119 |
# File 'lib/theme_check/offense.rb', line 117 def whole_theme? check.whole_theme? end |