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

Fix NodePattern for fields without a name #20

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
6 changes: 3 additions & 3 deletions lib/rubocop/graphql/node_pattern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ module NodePattern
extend RuboCop::NodePattern::Macros

def_node_matcher :field_definition?, <<~PATTERN
(send nil? :field ...)
(send nil? :field (:sym _) ...)
PATTERN

def_node_matcher :field_definition_with_body?, <<~PATTERN
(block
(send nil? :field ...)
(send nil? :field (:sym _) ...)
...
)
PATTERN

def_node_matcher :argument?, <<~PATTERN
(send nil? :argument ...)
(send nil? :argument (:sym _) ...)
PATTERN

def field?(node)
Expand Down
22 changes: 22 additions & 0 deletions spec/rubocop/cop/graphql/field_name_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,28 @@ class UserType < BaseType
end
end

context "when field is called on an extension" do
it "not registers an offense" do
expect_no_offenses(<<~RUBY)
class MyExtension < GraphQL::Schema::FieldExtension
def apply
field.argument(:width, Integer, required: false)
end
end
RUBY
end
end

context "when field has no name" do
it "not registers an offense" do
expect_no_offenses(<<~RUBY)
class UserType < BaseType
field field: MyField
end
RUBY
end
end

context "when field name is in camel case" do
it "registers an offense" do
expect_offense(<<~RUBY)
Expand Down