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

Constructor Details

#initialize(config_type: :default, exclude_snippets: true) ⇒ UndefinedObject

Returns a new instance of UndefinedObject.



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

def initialize(config_type: :default, exclude_snippets: true)
  @config_type = config_type
  @exclude_snippets = exclude_snippets
  @files = {}
end

Instance Method Details

#on_assign(node) ⇒ Object



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

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

#on_capture(node) ⇒ Object



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

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

#on_document(node) ⇒ Object



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

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

#on_endObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/theme_check/checks/undefined_object.rb', line 108

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

  theme_app_extension_objects = ThemeCheck::ShopifyLiquid::Object.theme_app_extension_labels
  theme_app_extension_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 'templates/robots.txt' == name
      # NOTE: `robots` is the only object exposed object in
      #       the robots.txt template
      check_object(info, ['robots'])
    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)
    elsif config_type == :theme_app_extension
      check_object(info, all_global_objects + theme_app_extension_objects)
    else
      check_object(info, all_global_objects)
    end
  end
end

#on_for(node) ⇒ Object



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

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

#on_include(_node) ⇒ Object



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

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



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

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.theme_file.name].add_render(
    name: snippet_name,
    node: node,
  )
end

#on_variable_lookup(node) ⇒ Object



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

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