Class: ThemeCheck::Check

Inherits:
Object
  • Object
show all
Includes:
JsonHelpers
Defined in:
lib/theme_check/check.rb

Direct Known Subclasses

HtmlCheck, JsonCheck, LiquidCheck

Constant Summary collapse

SEVERITIES =

The order matters.

[
  :error,
  :suggestion,
  :style,
]
SEVERITY_VALUES =
severity: sym

> number

SEVERITIES
.map
.with_index { |sev, i| [sev, i] }
.to_h
CATEGORIES =
[
  :liquid,
  :translation,
  :html,
  :json,
  :performance,
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from JsonHelpers

#format_json_parse_error, #pretty_json

Instance Attribute Details

#ignored_patternsObject



134
135
136
# File 'lib/theme_check/check.rb', line 134

def ignored_patterns
  @ignored_patterns ||= []
end

#offensesObject



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

def offenses
  @offenses ||= []
end

#optionsObject

Returns the value of attribute options.



9
10
11
# File 'lib/theme_check/check.rb', line 9

def options
  @options
end

#themeObject

Returns the value of attribute theme.



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

def theme
  @theme
end

Class Method Details

.allObject



35
36
37
# File 'lib/theme_check/check.rb', line 35

def all
  @all ||= []
end

.can_disable(disableable = nil) ⇒ Object



76
77
78
79
80
81
# File 'lib/theme_check/check.rb', line 76

def can_disable(disableable = nil)
  unless disableable.nil?
    @can_disable = disableable
  end
  defined?(@can_disable) ? @can_disable : true
end

.categories(*categories) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/theme_check/check.rb', line 53

def categories(*categories)
  @categories ||= []
  if categories.any?
    unknown_categories = categories.select { |category| !CATEGORIES.include?(category) }
    if unknown_categories.any?
      raise ArgumentError,
        "unknown categories: #{unknown_categories.join(', ')}. Use: #{CATEGORIES.join(', ')}"
    end
    @categories = categories
  end
  @categories
end

.categoryObject



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/theme_check/check.rb', line 65

def categories(*categories)
  @categories ||= []
  if categories.any?
    unknown_categories = categories.select { |category| !CATEGORIES.include?(category) }
    if unknown_categories.any?
      raise ArgumentError,
        "unknown categories: #{unknown_categories.join(', ')}. Use: #{CATEGORIES.join(', ')}"
    end
    @categories = categories
  end
  @categories
end

.doc(doc = nil) ⇒ Object



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

def doc(doc = nil)
  @doc = doc if doc
  @doc if defined?(@doc)
end

.docs_url(path) ⇒ Object



72
73
74
# File 'lib/theme_check/check.rb', line 72

def docs_url(path)
  "https://github.com/Shopify/theme-check/blob/main/docs/checks/#{File.basename(path, '.rb')}.md"
end

.severity(severity = nil) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/theme_check/check.rb', line 39

def severity(severity = nil)
  if severity
    unless SEVERITIES.include?(severity)
      raise ArgumentError, "unknown severity. Use: #{SEVERITIES.join(', ')}"
    end
    @severity = severity
  end
  @severity if defined?(@severity)
end

.severity_value(severity) ⇒ Object



49
50
51
# File 'lib/theme_check/check.rb', line 49

def severity_value(severity)
  SEVERITY_VALUES[severity] || -1
end

.single_file(single_file = nil) ⇒ Object



83
84
85
86
87
88
# File 'lib/theme_check/check.rb', line 83

def single_file(single_file = nil)
  unless single_file.nil?
    @single_file = single_file
  end
  defined?(@single_file) ? @single_file : !method_defined?(:on_end)
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



150
151
152
# File 'lib/theme_check/check.rb', line 150

def ==(other)
  other.is_a?(Check) && code_name == other.code_name
end

#add_offense(message, node: nil, theme_file: node&.theme_file, markup: nil, line_number: nil, node_markup_offset: 0, &block) ⇒ Object



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

def add_offense(message, node: nil, theme_file: node&.theme_file, markup: nil, line_number: nil, node_markup_offset: 0, &block)
  offenses << Offense.new(check: self, message: message, theme_file: theme_file, node: node, markup: markup, line_number: line_number, node_markup_offset: node_markup_offset, correction: block)
end

#can_disable?Boolean

Returns:

  • (Boolean)


138
139
140
# File 'lib/theme_check/check.rb', line 138

def can_disable?
  self.class.can_disable
end

#categoriesObject



114
115
116
# File 'lib/theme_check/check.rb', line 114

def categories
  self.class.categories
end

#code_nameObject



122
123
124
# File 'lib/theme_check/check.rb', line 122

def code_name
  StringHelpers.demodulize(self.class.name)
end

#docObject



118
119
120
# File 'lib/theme_check/check.rb', line 118

def doc
  self.class.doc
end

#ignore!Object



126
127
128
# File 'lib/theme_check/check.rb', line 126

def ignore!
  @ignored = true
end

#ignored?Boolean

Returns:

  • (Boolean)


130
131
132
# File 'lib/theme_check/check.rb', line 130

def ignored?
  defined?(@ignored) && @ignored
end

#severityObject



99
100
101
# File 'lib/theme_check/check.rb', line 99

def severity
  @severity ||= self.class.severity
end

#severity=(severity) ⇒ Object



103
104
105
106
107
108
# File 'lib/theme_check/check.rb', line 103

def severity=(severity)
  unless SEVERITIES.include?(severity)
    raise ArgumentError, "unknown severity. Use: #{SEVERITIES.join(', ')}"
  end
  @severity = severity
end

#severity_valueObject



110
111
112
# File 'lib/theme_check/check.rb', line 110

def severity_value
  SEVERITY_VALUES[severity]
end

#single_file?Boolean

Returns:

  • (Boolean)


142
143
144
# File 'lib/theme_check/check.rb', line 142

def single_file?
  self.class.single_file
end

#to_sObject



155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/theme_check/check.rb', line 155

def to_s
  s = +"#{code_name}:\n"
  properties = {
    severity: severity,
    categories: categories,
    doc: doc,
    ignored_patterns: ignored_patterns,
  }.merge(options)
  properties.each_pair do |name, value|
    s << "  #{name}: #{value}\n" if value
  end
  s
end

#whole_theme?Boolean

Returns:

  • (Boolean)


146
147
148
# File 'lib/theme_check/check.rb', line 146

def whole_theme?
  !single_file?
end