File: context_is_passed_cop.rb

package info (click to toggle)
ruby-graphql 2.2.17-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 9,584 kB
  • sloc: ruby: 67,505; ansic: 1,753; yacc: 831; javascript: 331; makefile: 6
file content (44 lines) | stat: -rw-r--r-- 1,567 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
42
43
44
# frozen_string_literal: true
require 'rubocop'

module Cop
  module Development
    class ContextIsPassedCop < RuboCop::Cop::Cop
      MSG = <<-MSG
This method also accepts `context` as an argument. Pass it so that the returned value will reflect the current query, or use another method that isn't context-dependent.
MSG

      # These are already context-aware or else not query-related
      def_node_matcher :likely_query_specific_receiver?, "
        {
          (send _ {:ast_node :query :context :warden :ctx :query_ctx :query_context})
          (lvar {:ast_node :query :context :warden :ctx :query_ctx :query_context})
          (ivar {:@query :@context :@warden})
          (send _ {:introspection_system})
        }
      "

      def_node_matcher :method_doesnt_receive_second_context_argument?, <<-MATCHER
        (send _ {:get_field :get_argument :get_type} _)
      MATCHER

      def_node_matcher :method_doesnt_receive_first_context_argument?, <<-MATCHER
        (send _ {:fields :arguments :types :enum_values})
      MATCHER

      def_node_matcher :is_enum_values_call_without_arguments?, "
        (send (send _ {:enum :enum_type (ivar {:@enum :@enum_type})}) {:values})
      "

      def on_send(node)
        if (
            method_doesnt_receive_second_context_argument?(node) ||
              method_doesnt_receive_first_context_argument?(node) ||
              is_enum_values_call_without_arguments?(node)
            ) && !likely_query_specific_receiver?(node.to_a[0])
          add_offense(node)
        end
      end
    end
  end
end