From 3cd3cf45294ab08c34c478075cb5b5ba122479cc Mon Sep 17 00:00:00 2001 From: fatkodima Date: Fri, 3 May 2024 14:11:59 +0300 Subject: [PATCH] Optimize `OrderedArguments` and `FieldDefinitions` cops --- lib/rubocop/cop/graphql/ordered_arguments.rb | 33 ++++++++----------- lib/rubocop/graphql/sorbet.rb | 12 ++----- .../cop/graphql/field_definitions_spec.rb | 2 +- .../multiple_field_definitions_spec.rb | 4 +-- 4 files changed, 18 insertions(+), 33 deletions(-) diff --git a/lib/rubocop/cop/graphql/ordered_arguments.rb b/lib/rubocop/cop/graphql/ordered_arguments.rb index 1ee3f61..be75a20 100644 --- a/lib/rubocop/cop/graphql/ordered_arguments.rb +++ b/lib/rubocop/cop/graphql/ordered_arguments.rb @@ -58,16 +58,14 @@ class OrderedArguments < Base "Field `%s` should appear before `%s`." def on_class(node) - declarations_with_blocks = argument_declarations_with_blocks(node) - declarations_without_blocks = argument_declarations_without_blocks(node) - - argument_declarations = declarations_without_blocks.map do |node| - arg_name = argument_name(node) - same_arg_with_block_declaration = declarations_with_blocks.find do |dec| - argument_name(dec) == arg_name + # Do a single pass over descendants to get argument declarations + # with and without a block. + argument_declarations = argument_declaration(node).map do |declaration| + if argument_declaration_with_block?(declaration) + declaration.parent + else + declaration end - - same_arg_with_block_declaration || node end argument_declarations.each_cons(2) do |previous, current| @@ -80,6 +78,10 @@ def on_class(node) private + def argument_declaration_with_block?(node) + node.parent&.block_type? && node.parent.send_node == node + end + def register_offense(previous, current) message = format( self.class::MSG, @@ -102,19 +104,10 @@ def consecutive_lines(previous, current) previous.source_range.last_line == current.source_range.first_line - 1 end - # @!method argument_declarations_without_blocks(node) - def_node_search :argument_declarations_without_blocks, <<~PATTERN + # @!method argument_declaration(node) + def_node_search :argument_declaration, <<~PATTERN (send nil? :argument (:sym _) ...) PATTERN - - # @!method argument_declarations_with_blocks(node) - def_node_search :argument_declarations_with_blocks, <<~PATTERN - (block - (send nil? :argument - (:sym _) - ...) - ...) - PATTERN end end end diff --git a/lib/rubocop/graphql/sorbet.rb b/lib/rubocop/graphql/sorbet.rb index 766cfb2..a180698 100644 --- a/lib/rubocop/graphql/sorbet.rb +++ b/lib/rubocop/graphql/sorbet.rb @@ -15,16 +15,8 @@ def has_sorbet_signature?(node) end def sorbet_signature_for(node) - node.parent.each_descendant.find do |sibling| - siblings_in_sequence?(sibling, node) && - sorbet_signature(sibling) - end - end - - private - - def siblings_in_sequence?(first_node, second_node) - first_node.sibling_index - second_node.sibling_index == - 1 + sibling = node.left_sibling + sibling if sibling && sorbet_signature(sibling) end end end diff --git a/spec/rubocop/cop/graphql/field_definitions_spec.rb b/spec/rubocop/cop/graphql/field_definitions_spec.rb index a8d8c06..58cdc2e 100644 --- a/spec/rubocop/cop/graphql/field_definitions_spec.rb +++ b/spec/rubocop/cop/graphql/field_definitions_spec.rb @@ -971,7 +971,7 @@ def image_url RUBY end - it "register an offense when when resolver method is before the last field definition" do + it "registers an offense when when resolver method is before the last field definition" do expect_offense(<<~RUBY) class UserType < BaseType field :image_url, String, null: false do diff --git a/spec/rubocop/cop/graphql/multiple_field_definitions_spec.rb b/spec/rubocop/cop/graphql/multiple_field_definitions_spec.rb index 9d7f7fe..b844a8d 100644 --- a/spec/rubocop/cop/graphql/multiple_field_definitions_spec.rb +++ b/spec/rubocop/cop/graphql/multiple_field_definitions_spec.rb @@ -14,7 +14,7 @@ class UserType < BaseType RUBY end - it "register an offense when ungrouped" do + it "registers an offense when ungrouped" do expect_offense(<<~RUBY) class UserType < BaseType field :image_url, String, null: true @@ -54,7 +54,7 @@ class UserType < BaseType RUBY end - it "register an offense when ungrouped" do + it "registers an offense when ungrouped" do expect_offense(<<~RUBY) class UserType < BaseType field :image_url, String, null: false do