Skip to content

Commit

Permalink
Fix NodePattern for fields without a name (#20)
Browse files Browse the repository at this point in the history
`field` can be called without a position name argument which causes
errors right now. This also happens when FieldExtension's are used;
methods are called on `field` instead of passing arguments.

The simplest fix was to ensure the node pattern matching for field
definitions required a symbol positional argument (the field name).
  • Loading branch information
swalkinshaw authored Jul 21, 2020
1 parent 9513407 commit e4f2915
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
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

0 comments on commit e4f2915

Please sign in to comment.