Class: RuboCop::Cop::GraphQL::PrepareMethod
- Inherits:
-
Base
- Object
- Base
- RuboCop::Cop::GraphQL::PrepareMethod
- Extended by:
- AutoCorrector
- Defined in:
- lib/rubocop/cop/graphql/prepare_method.rb
Overview
Checks that GraphQL Argument definitions prepare arguments use String or constants instead of ‘prepare: CONST_REF` This allows better Sorbet typing.
Preference is given to an input object declaring a ‘def prepare(…); end` method
Constant Summary collapse
- GENERAL_MSG =
"Avoid using prepare lambdas, use prepare: :method_name or "\ "prepare: \"method_name\" instead."
- STRING_MSG =
"Avoid using prepare lambdas, use prepare: \"method_name\" instead."
- PREFER_STRING_MSG =
"Avoid using prepare symbols, use prepare: \"%s\" instead."
- SYMBOL_MSG =
"Avoid using prepare lambdas, use prepare: :method_name instead."
- PREFER_SYMBOL_MSG =
"Avoid using prepare string, use prepare: :%s instead."
- ARGUMENT_METHODS =
Set[:argument, :public_argument].freeze
Instance Method Summary collapse
- #graphql_argument_with_prepare_block?(node) ⇒ Object
- #on_send(node) ⇒ Object
- #prepare_method_string_name?(node) ⇒ Object
- #prepare_method_symbol_name?(node) ⇒ Object
Instance Method Details
#graphql_argument_with_prepare_block?(node) ⇒ Object
37 38 39 |
# File 'lib/rubocop/cop/graphql/prepare_method.rb', line 37 def_node_matcher :graphql_argument_with_prepare_block?, <<~PATTERN (send nil? ARGUMENT_METHODS ... (hash ... (pair (sym :prepare) $({ const | block } ...) ))) PATTERN |
#on_send(node) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/rubocop/cop/graphql/prepare_method.rb', line 51 def on_send(node) type = cop_config["EnforcedStyle"] = case type when "symbol" SYMBOL_MSG when "string" STRING_MSG else GENERAL_MSG end graphql_argument_with_prepare_block?(node) do |prepare_definition| add_offense(prepare_definition, message: ) end if type == "symbol" prepare_method_string_name?(node) do |prepare_definition| method_name = prepare_definition.value add_offense(prepare_definition, message: format(PREFER_SYMBOL_MSG, method_name)) do |corrector| autocorrect(corrector, node, "\"#{method_name}\"", ":#{method_name}") end end elsif type == "string" prepare_method_symbol_name?(node) do |prepare_definition| method_name = prepare_definition.value add_offense(prepare_definition, message: format(PREFER_STRING_MSG, method_name)) do |corrector| autocorrect(corrector, node, ":#{method_name}", "\"#{method_name}\"") end end end end |
#prepare_method_string_name?(node) ⇒ Object
42 43 44 |
# File 'lib/rubocop/cop/graphql/prepare_method.rb', line 42 def_node_matcher :prepare_method_string_name?, <<~PATTERN (send nil? ARGUMENT_METHODS ... (hash ... (pair (sym :prepare) $(str ...) ))) PATTERN |
#prepare_method_symbol_name?(node) ⇒ Object
47 48 49 |
# File 'lib/rubocop/cop/graphql/prepare_method.rb', line 47 def_node_matcher :prepare_method_symbol_name?, <<~PATTERN (send nil? ARGUMENT_METHODS ... (hash ... (pair (sym :prepare) $(sym ...) ))) PATTERN |