Class: PlatformosCheck::InvalidArgs

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

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

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_platformos_app?

Methods included from JsonHelpers

#format_json_parse_error, #pretty_json

Instance Method Details

#add_duplicated_key_offense(node) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/platformos_check/checks/invalid_args.rb', line 43

def add_duplicated_key_offense(node)
  node.value.duplicated_attrs.each do |duplicated_arg|
    add_offense("Duplicated argument `#{duplicated_arg}`", node:) do |corrector|
      match = node.markup.match(/(?<attribute>,?\s*#{duplicated_arg}\s*:\s*#{Liquid::QuotedFragment})\s*/)
      corrector.replace(node, node.markup.sub(match[:attribute], ''), node.start_index...node.end_index)
    end
  end
end

#on_function(node) ⇒ Object



13
14
15
# File 'lib/platformos_check/checks/invalid_args.rb', line 13

def on_function(node)
  add_duplicated_key_offense(node)
end

#on_graphql(node) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/platformos_check/checks/invalid_args.rb', line 17

def on_graphql(node)
  add_duplicated_key_offense(node)

  return if node.value.inline_query

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

  graphql_file = platformos_app.grouped_files[GraphqlFile][graphql_partial]
  return unless graphql_file

  provided_arguments = node.value.attributes

  return if provided_arguments.include?('args')

  (provided_arguments - graphql_file.defined_arguments).each do |name|
    add_offense("Undefined argument `#{name}` provided to `#{graphql_file.relative_path}`", node:)
  end

  (graphql_file.required_arguments - provided_arguments).each do |name|
    add_offense("Required argument `#{name}` not provided to `#{graphql_file.relative_path}`", node:)
  end
rescue GraphQL::ParseError => e
  add_offense("GraphQL Parse error triggered by `#{graphql_file.relative_path}`: #{e.message}", node:)
end

#on_render(node) ⇒ Object



9
10
11
# File 'lib/platformos_check/checks/invalid_args.rb', line 9

def on_render(node)
  add_duplicated_key_offense(node)
end