Class: PlatformosCheck::UndefinedObject

Inherits:
LiquidCheck show all
Defined in:
lib/platformos_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, #platformos_app

Class Method Summary collapse

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?, #to_s, #whole_platformos_app?

Methods included from JsonHelpers

#format_json_parse_error, #pretty_json

Constructor Details

#initialize(config_type: :default) ⇒ UndefinedObject

Returns a new instance of UndefinedObject.



65
66
67
68
# File 'lib/platformos_check/checks/undefined_object.rb', line 65

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

Class Method Details

.single_file(**_args) ⇒ Object



61
62
63
# File 'lib/platformos_check/checks/undefined_object.rb', line 61

def self.single_file(**_args)
  true
end

Instance Method Details

#on_assign(node) ⇒ Object



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

def on_assign(node)
  @files[node.app_file.name].all_assigns[node.value.to] = node
end

#on_background(node) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/platformos_check/checks/undefined_object.rb', line 119

def on_background(node)
  return unless node.value.partial_syntax

  @files[node.app_file.name].all_assigns[node.value.to] = node

  return unless node.value.partial_name.is_a?(String)

  @files[node.app_file.name].add_render(
    name: node.value.partial_name,
    node:
  )
end

#on_capture(node) ⇒ Object



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

def on_capture(node)
  @files[node.app_file.name].all_captures[node.value.instance_variable_get(:@to)] = node
end

#on_document(node) ⇒ Object



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

def on_document(node)
  @files[node.app_file.name] = TemplateInfo.new(app_file: node.app_file)
end

#on_endObject



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/platformos_check/checks/undefined_object.rb', line 151

def on_end
  all_global_objects = PlatformosCheck::PlatformosLiquid::Object.labels
  all_global_objects.freeze

  each_template do |(_name, info)|
    if info.app_file.notification?
      # NOTE: `data` comes from graphql for notifications
      check_object(info, all_global_objects + %w[data response form])
    elsif info.app_file.form?
      # NOTE: `data` comes from graphql for notifications
      check_object(info, all_global_objects + %w[form form_builder])
    else
      check_object(info, all_global_objects)
    end
  end
end

#on_for(node) ⇒ Object



86
87
88
# File 'lib/platformos_check/checks/undefined_object.rb', line 86

def on_for(node)
  @files[node.app_file.name].all_forloops[node.value.variable_name] = node
end

#on_function(node) ⇒ Object



104
105
106
107
108
109
110
111
112
113
# File 'lib/platformos_check/checks/undefined_object.rb', line 104

def on_function(node)
  @files[node.app_file.name].all_assigns[node.value.to] = node

  return unless node.value.from.is_a?(String)

  @files[node.app_file.name].add_render(
    name: node.value.from,
    node:
  )
end

#on_graphql(node) ⇒ Object



115
116
117
# File 'lib/platformos_check/checks/undefined_object.rb', line 115

def on_graphql(node)
  @files[node.app_file.name].all_assigns[node.value.to] = node
end

#on_include(_node) ⇒ Object



90
91
92
# File 'lib/platformos_check/checks/undefined_object.rb', line 90

def on_include(_node)
  # NOOP: we purposely do nothing on `include` since it is deprecated
end

#on_parse_json(node) ⇒ Object



82
83
84
# File 'lib/platformos_check/checks/undefined_object.rb', line 82

def on_parse_json(node)
  @files[node.app_file.name].all_captures[node.value.to] = node
end

#on_render(node) ⇒ Object



94
95
96
97
98
99
100
101
102
# File 'lib/platformos_check/checks/undefined_object.rb', line 94

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

  partial_name = node.value.template_name_expr
  @files[node.app_file.name].add_render(
    name: partial_name,
    node:
  )
end

#on_variable_lookup(node) ⇒ Object



132
133
134
135
136
137
# File 'lib/platformos_check/checks/undefined_object.rb', line 132

def on_variable_lookup(node)
  @files[node.app_file.name].add_variable_lookup(
    name: node.value.name,
    node:
  )
end

#single_file_end_dependencies(app_file) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
# File 'lib/platformos_check/checks/undefined_object.rb', line 139

def single_file_end_dependencies(app_file)
  @files[app_file.name].all_renders.keys.map do |partial_name|
    next if @files[partial_name]

    partial_file = @platformos_app.partials.detect { |p| p.name == partial_name } # NOTE: undefined partial

    next unless partial_file

    partial_file
  end.compact
end