Skip to content

Commit

Permalink
Fix FieldDefinitions autocorrection for fields with squished heredocs (
Browse files Browse the repository at this point in the history
  • Loading branch information
fatkodima authored Jun 19, 2024
1 parent a4b8924 commit 3b8016f
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 4 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#164](https://github.com/DmitryTsepelev/rubocop-graphql/pull/164) Fix FieldDefinitions autocorrection for fields with squished heredocs ([@fatkodima][])

## 1.5.2 (2024-06-01)

- [PR#157](https://github.com/DmitryTsepelev/rubocop-graphql/pull/157) Optimize OrderedArguments and FieldDefinitions cops ([@fatkodima][])
Expand Down
15 changes: 11 additions & 4 deletions lib/rubocop/graphql/heredoc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,23 @@ def heredoc?(node)
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
last_heredoc = find_last_heredoc(node)

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

range_by_whole_lines(range)
end

private

def find_last_heredoc(node)
# Do a cheap check first.
return nil unless node.source.include?("<<")

heredocs = node.descendants.select { |descendant| heredoc?(descendant) }
heredocs.max_by { |heredoc| heredoc.loc.heredoc_end }
end
end
end
end
80 changes: 80 additions & 0 deletions spec/rubocop/cop/graphql/field_definitions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,86 @@ def last_name
RUBY
end

it "registers offense when field has multiple heredocs" do
expect_offense(<<~RUBY)
class UserType < BaseType
field :first_name, String, null: true, description: <<~DESC, deprecation_reason: <<~DEP
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Define resolver method after field definition.
First name.
DESC
Deprecated.
DEP
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, deprecation_reason: <<~DEP
First name.
DESC
Deprecated.
DEP
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 field has a squished heredoc description" do
expect_offense(<<~RUBY)
class UserType < BaseType
field :first_name, String, null: true, description: <<~DESC.squish
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 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.squish
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

context "within module" do
it "registers offenses inside class" do
expect_offense(<<~RUBY)
Expand Down

0 comments on commit 3b8016f

Please sign in to comment.