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 FieldDefinitions autocorrection for fields with squished heredocs #164

Merged
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
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