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

Instance Attribute Details

#ignored_patternsObject

Returns the value of attribute ignored_patterns.



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

def ignored_patterns
  @ignored_patterns
end

#offensesObject



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

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



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

def all
  @all ||= []
end

.can_disable(disableable = nil) ⇒ Object



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

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

.categories(*categories) ⇒ Object



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

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



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

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



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

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

.docs_url(path) ⇒ Object



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

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

.severity(severity = nil) ⇒ Object



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

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



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

def severity_value(severity)
  SEVERITY_VALUES[severity]
end

.single_file(single_file = nil) ⇒ Object



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

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?



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

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

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



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

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

#can_disable?Boolean

Returns:

  • (Boolean)


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

def can_disable?
  self.class.can_disable
end

#categoriesObject



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

def categories
  self.class.categories
end

#code_nameObject



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

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

#docObject



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

def doc
  self.class.doc
end

#ignore!Object



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

def ignore!
  @ignored = true
end

#ignored?Boolean

Returns:

  • (Boolean)


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

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

#severityObject



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

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

#severity=(severity) ⇒ Object



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

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

#severity_valueObject



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

def severity_value
  SEVERITY_VALUES[severity]
end

#single_file?Boolean

Returns:

  • (Boolean)


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

def single_file?
  self.class.single_file
end

#to_sObject



150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/theme_check/check.rb', line 150

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)


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

def whole_theme?
  !single_file?
end