Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize OrderedArguments and FieldDefinitions cops #157

Merged
merged 1 commit into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 13 additions & 20 deletions lib/rubocop/cop/graphql/ordered_arguments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,14 @@ class OrderedArguments < Base
"Field `%<current>s` should appear before `%<previous>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|
Expand All @@ -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,
Expand All @@ -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
Expand Down
12 changes: 2 additions & 10 deletions lib/rubocop/graphql/sorbet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/rubocop/cop/graphql/field_definitions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions spec/rubocop/cop/graphql/multiple_field_definitions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading