Class: ThemeCheck::UndefinedObject

Inherits:
LiquidCheck show all
Defined in:
lib/theme_check/checks/undefined_object.rb

Defined Under Namespace

Classes: TemplateInfo

Constant Summary

Constants inherited from Check

Check::CATEGORIES, Check::SEVERITIES, Check::SEVERITY_VALUES

Instance Attribute Summary

Attributes inherited from Check

#ignored_patterns, #offenses, #options, #theme

Instance Method Summary collapse

Methods included from ChecksTracking

#inherited

Methods included from ParsingHelpers

#outside_of_strings

Methods inherited from Check

#==, #add_offense, all, can_disable, #can_disable?, categories, #categories, category, #code_name, doc, #doc, docs_url, #ignore!, #ignored?, #severity, severity, #severity=, #severity_value, severity_value, single_file, #single_file?, #to_s, #whole_theme?

Methods included from JsonHelpers

#format_json_parse_error

Constructor Details

#initialize(exclude_snippets: false) ⇒ UndefinedObject

Returns a new instance of UndefinedObject.



58
59
60
61
# File 'lib/theme_check/checks/undefined_object.rb', line 58

def initialize(exclude_snippets: false)
  @exclude_snippets = exclude_snippets
  @files = {}
end

Instance Method Details

#on_assign(node) ⇒ Object



68
69
70
71
# File 'lib/theme_check/checks/undefined_object.rb', line 68

def on_assign(node)
  return if ignore?(node)
  @files[node.template.name].all_assigns[node.value.to] = node
end

#on_capture(node) ⇒ Object



73
74
75
76
# File 'lib/theme_check/checks/undefined_object.rb', line 73

def on_capture(node)
  return if ignore?(node)
  @files[node.template.name].all_captures[node.value.instance_variable_get('@to')] = node
end

#on_document(node) ⇒ Object



63
64
65
66
# File 'lib/theme_check/checks/undefined_object.rb', line 63

def on_document(node)
  return if ignore?(node)
  @files[node.template.name] = TemplateInfo.new
end

#on_endObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/theme_check/checks/undefined_object.rb', line 107

def on_end
  all_global_objects = ThemeCheck::ShopifyLiquid::Object.labels
  all_global_objects.freeze

  shopify_plus_objects = ThemeCheck::ShopifyLiquid::Object.plus_labels
  shopify_plus_objects.freeze

  each_template do |(name, info)|
    if 'templates/customers/reset_password' == name
      # NOTE: `email` is exceptionally exposed as a theme object in
      #       the customers' reset password template
      check_object(info, all_global_objects + ['email'])
    elsif 'layout/checkout' == name
      # NOTE: Shopify Plus has exceptionally exposed objects in
      #       the checkout template
      # https://shopify.dev/docs/themes/theme-templates/checkout-liquid#optional-objects
      check_object(info, all_global_objects + shopify_plus_objects)
    else
      check_object(info, all_global_objects)
    end
  end
end

#on_for(node) ⇒ Object



78
79
80
81
# File 'lib/theme_check/checks/undefined_object.rb', line 78

def on_for(node)
  return if ignore?(node)
  @files[node.template.name].all_forloops[node.value.variable_name] = node
end

#on_include(_node) ⇒ Object



83
84
85
86
# File 'lib/theme_check/checks/undefined_object.rb', line 83

def on_include(_node)
  # NOOP: we purposely do nothing on `include` since it is deprecated
  #   https://shopify.dev/docs/themes/liquid/reference/tags/deprecated-tags#include
end

#on_render(node) ⇒ Object



88
89
90
91
92
93
94
95
96
97
# File 'lib/theme_check/checks/undefined_object.rb', line 88

def on_render(node)
  return if ignore?(node)
  return unless node.value.template_name_expr.is_a?(String)

  snippet_name = "snippets/#{node.value.template_name_expr}"
  @files[node.template.name].add_render(
    name: snippet_name,
    node: node,
  )
end

#on_variable_lookup(node) ⇒ Object



99
100
101
102
103
104
105
# File 'lib/theme_check/checks/undefined_object.rb', line 99

def on_variable_lookup(node)
  return if ignore?(node)
  @files[node.template.name].add_variable_lookup(
    name: node.value.name,
    node: node,
  )
end