Skip to content

Commit

Permalink
[Fix #108] Handle heredocs when reordering code
Browse files Browse the repository at this point in the history
  • Loading branch information
Darhazer committed Jan 13, 2023
1 parent 0bb454a commit de57646
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## master

- [PR#107](https://github.com/DmitryTsepelev/rubocop-graphql/pull/107) Make ExtractInputType check nested folders ([@arenclissold][])
- [PR#109](https://github.com/DmitryTsepelev/rubocop-graphql/pull/109) [OrderedFields] [OrderedArguments] Handle heredocs when auto-correcting ([@Darhazer][])

## 0.18.0 (2022-10-30)

Expand Down
11 changes: 10 additions & 1 deletion lib/rubocop/graphql/swap_range.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,20 @@ def swap_range(corrector, current, previous)
def declaration(node)
buffer = processed_source.buffer
begin_pos = range_by_whole_lines(node.source_range).begin_pos
end_line = buffer.line_for_position(node.loc.expression.end_pos)
end_line = buffer.line_for_position(final_end_location(node).end_pos)
end_pos = range_by_whole_lines(buffer.line_range(end_line),
include_final_newline: true).end_pos
Parser::Source::Range.new(buffer, begin_pos, end_pos)
end

def final_end_location(start_node)
heredoc_endings =
start_node.each_node(:str, :dstr, :xstr)
.select(&:heredoc?)
.map { |node| node.loc.heredoc_end }

[start_node.source_range.end, *heredoc_endings].max_by(&:line)
end
end
end
end
25 changes: 25 additions & 0 deletions spec/rubocop/cop/graphql/ordered_arguments_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,31 @@ class UpdateProfile < BaseMutation
end
end

context "when an unordered argument declaration uses heredoc" do
it "registers an offense" do
expect_offense(<<~RUBY)
class UpdateProfile < BaseMutation
argument :uuid, ID, required: true
argument :email, String, required: false, description: <<~DESC
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Arguments should be sorted in an alphabetical order within their section. Field `email` should appear before `uuid`.
heredoc description
DESC
argument :name, String, required: false
end
RUBY

expect_correction(<<~RUBY)
class UpdateProfile < BaseMutation
argument :email, String, required: false, description: <<~DESC
heredoc description
DESC
argument :name, String, required: false
argument :uuid, ID, required: true
end
RUBY
end
end

context "when an unordered argument declaration contains a block" do
it "registers an offense" do
expect_offense(<<~RUBY)
Expand Down
23 changes: 23 additions & 0 deletions spec/rubocop/cop/graphql/ordered_fields_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,29 @@ class UserType < BaseType
end
end

context "when a unordered field declaration uses heredocs" do
it "registers an offense" do
expect_offense(<<~RUBY)
class UserType < BaseType
field :phone, String, null: true
field :name, String, null: true, description: <<~HEREDOC
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Fields should be sorted in an alphabetical order within their section. Field `name` should appear before `phone`.
heredoc_example
HEREDOC
end
RUBY

expect_correction(<<~RUBY)
class UserType < BaseType
field :name, String, null: true, description: <<~HEREDOC
heredoc_example
HEREDOC
field :phone, String, null: true
end
RUBY
end
end

context "with multiple offenses" do
it "orders all fields alphabetically" do
expect_offense(<<~RUBY)
Expand Down

0 comments on commit de57646

Please sign in to comment.