Skip to content

Commit

Permalink
Make FieldDefinitions work with modules (#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
DmitryTsepelev authored Feb 11, 2023
1 parent 0fd1590 commit 7a54d1f
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## master

- [PR#111](https://github.com/DmitryTsepelev/rubocop-graphql/pull/111) Make FieldDefinitions work with modules ([@DmitryTsepelev][])

## 0.19.0 (2023-01-15)

- [PR#107](https://github.com/DmitryTsepelev/rubocop-graphql/pull/107) Make ExtractInputType check nested folders ([@arenclissold][])
Expand Down
11 changes: 10 additions & 1 deletion lib/rubocop/cop/graphql/field_definitions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,16 @@ def on_class(node)
end
end

def on_module(node)
return if style != :group_definitions

schema_member = RuboCop::GraphQL::SchemaMember.new(node)

if (body = schema_member.body)
check_grouped_field_declarations(body)
end
end

private

GROUP_DEFS_MSG = "Group all field definitions together."
Expand All @@ -100,7 +110,6 @@ def check_grouped_field_declarations(body)

def group_field_declarations(corrector, node)
field = RuboCop::GraphQL::Field.new(node)

first_field = field.schema_member.body.find do |node|
field_definition?(node) || field_definition_with_body?(node)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/graphql/field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def root_node
end

def root_node?(node)
node.parent.nil? || node.parent.module_type? || root_with_siblings?(node.parent)
node.parent.nil? || node.class_type? || root_with_siblings?(node.parent)
end

def root_with_siblings?(node)
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/graphql/schema_member.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class SchemaMember

# @!method class_contents(node)
def_node_matcher :class_contents, <<~PATTERN
(class _ _ $_)
{(class _ _ $_) | (module _ $_)}
PATTERN

attr_reader :node
Expand Down
38 changes: 38 additions & 0 deletions spec/rubocop/cop/graphql/field_definitions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,44 @@ def last_name
RUBY
end

context "when inside a module" do
it "registers an offense" do
expect_offense(<<~RUBY)
module UserType
field :first_name, String, null: true
def first_name
object.contact_data.first_name
end
field :last_name, String, null: true
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Group all field definitions together.
def last_name
object.contact_data.last_name
end
end
RUBY

expect_correction(<<~RUBY)
module UserType
field :first_name, String, null: true
field :last_name, String, null: true
def first_name
object.contact_data.first_name
end
def last_name
object.contact_data.last_name
end
end
RUBY
end
end

context "when resolver methods have Sorbet signatures" do
it "registers an offense" do
expect_offense(<<~RUBY)
Expand Down

0 comments on commit 7a54d1f

Please sign in to comment.