Class: PlatformosCheck::UndefinedObject

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

Defined Under Namespace

Classes: TemplateInfo

Constant Summary collapse

NOTIFICATION_GLOBAL_OBJECTS =
%w[data response form].freeze
FORM_GLOBAL_OBJECTS =
%w[form form_builder].freeze

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

#initializeUndefinedObject

Returns a new instance of UndefinedObject.



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

def initialize
  @files = {}
end

Class Method Details

.single_file(**_args) ⇒ Object



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

def self.single_file(**_args)
  true
end

Instance Method Details

#on_assign(node) ⇒ Object



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

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

#on_background(node) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/platformos_check/checks/undefined_object.rb', line 128

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



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

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

#on_document(node) ⇒ Object



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

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

#on_endObject



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/platformos_check/checks/undefined_object.rb', line 160

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 + NOTIFICATION_GLOBAL_OBJECTS)
    elsif info.app_file.form?
      # NOTE: `data` comes from graphql for notifications
      check_object(info, all_global_objects + FORM_GLOBAL_OBJECTS)
    else
      check_object(info, all_global_objects)
    end
  end
end

#on_for(node) ⇒ Object



95
96
97
# File 'lib/platformos_check/checks/undefined_object.rb', line 95

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

#on_function(node) ⇒ Object



113
114
115
116
117
118
119
120
121
122
# File 'lib/platformos_check/checks/undefined_object.rb', line 113

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



124
125
126
# File 'lib/platformos_check/checks/undefined_object.rb', line 124

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

#on_include(_node) ⇒ Object



99
100
101
# File 'lib/platformos_check/checks/undefined_object.rb', line 99

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

#on_parse_json(node) ⇒ Object



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

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

#on_render(node) ⇒ Object



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

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



141
142
143
144
145
146
# File 'lib/platformos_check/checks/undefined_object.rb', line 141

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



148
149
150
151
152
153
154
155
156
157
158
# File 'lib/platformos_check/checks/undefined_object.rb', line 148

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