Skip to content

Commit

Permalink
[FieldDefinitions] Support heredoc descriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
alex4787 committed Mar 22, 2022
1 parent e11b9fb commit e95a00a
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/rubocop-graphql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
require_relative "rubocop/graphql/version"
require_relative "rubocop/graphql/inject"
require_relative "rubocop/graphql/description_method"
require_relative "rubocop/graphql/heredoc"
require_relative "rubocop/graphql/node_pattern"
require_relative "rubocop/graphql/node_uniqueness"
require_relative "rubocop/graphql/sorbet"
Expand Down
3 changes: 2 additions & 1 deletion lib/rubocop/cop/graphql/field_definitions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class FieldDefinitions < Base # rubocop:disable Metrics/ClassLength
include RuboCop::GraphQL::NodePattern
include RuboCop::Cop::RangeHelp
include RuboCop::GraphQL::Sorbet
include RuboCop::GraphQL::Heredoc

def_node_matcher :field_kwargs, <<~PATTERN
(send nil? :field
Expand Down Expand Up @@ -188,7 +189,7 @@ def insert_new_resolver(corrector, field_definition, resolver_definition)
"\n#{signature_to_insert(resolver_definition)}\n" \
"#{indent(resolver_definition)}#{resolver_definition.source}\n"

field_definition_range = range_by_whole_lines(field_definition.loc.expression)
field_definition_range = range_including_heredoc(field_definition)
corrector.insert_after(field_definition_range, source_to_insert)
end

Expand Down
23 changes: 23 additions & 0 deletions lib/rubocop/graphql/heredoc.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

module RuboCop
module GraphQL
module Heredoc
def heredoc?(node)
(node.str_type? || node.dstr_type?) && node.heredoc?
end

def range_including_heredoc(node)
field = RuboCop::GraphQL::Field.new(node)
last_heredoc = field.kwargs.instance_variable_get(:@nodes).reverse.find do |kwarg|
heredoc?(kwarg.value)
end&.value

range = node.loc.expression
range = range.join(last_heredoc.loc.heredoc_end) if last_heredoc

range_by_whole_lines(range)
end
end
end
end
62 changes: 62 additions & 0 deletions spec/rubocop/cop/graphql/field_definitions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,30 @@ def last_name
end
end

context "when field has a heredoc description" do
it "not registers an offense" do
expect_no_offenses(<<~RUBY)
class UserType < BaseType
field :first_name, String, null: true, description: <<~DESC
First Name.
DESC
def first_name
object.contact_data.first_name
end
field :last_name, String, null: true, description: <<~DESC
Last Name.
DESC
def last_name
object.contact_data.last_name
end
end
RUBY
end
end

context "when class has single field" do
it "not registers an offense" do
expect_no_offenses(<<~RUBY)
Expand Down Expand Up @@ -454,6 +478,44 @@ def last_name
RUBY
end

it "registers offenses when field has a heredoc description" do
expect_offense(<<~RUBY)
class UserType < BaseType
field :first_name, String, null: true, description: <<~DESC
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Define resolver method after field definition.
First name.
DESC
field :last_name, String, null: true
def last_name
object.contact_data.last_name
end
def first_name
object.contact_data.first_name
end
end
RUBY

expect_correction(<<~RUBY)
class UserType < BaseType
field :first_name, String, null: true, description: <<~DESC
First name.
DESC
def first_name
object.contact_data.first_name
end
field :last_name, String, null: true
def last_name
object.contact_data.last_name
end
end
RUBY
end

it "registers offenses when type classes are nested within a module" do
expect_offense(<<~RUBY)
module Types
Expand Down

0 comments on commit e95a00a

Please sign in to comment.