Skip to content

Commit

Permalink
Merge pull request #4704 from fpacanowski/master
Browse files Browse the repository at this point in the history
Fix `BlockString.trim_whitespace` to conform to spec.
  • Loading branch information
rmosolgo authored Nov 24, 2023
2 parents 565b62f + e3fe8a6 commit 109c26c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/graphql/language/block_string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ def self.trim_whitespace(str)
end

# Remove leading & trailing blank lines
while lines.size > 0 && lines[0].empty?
while lines.size > 0 && contains_only_whitespace?(lines.first)
lines.shift
end
while lines.size > 0 && lines[-1].empty?
while lines.size > 0 && contains_only_whitespace?(lines.last)
lines.pop
end

Expand Down Expand Up @@ -106,6 +106,10 @@ def self.break_line(line, length)

nil
end

def self.contains_only_whitespace?(line)
line.match?(/^\s*$/)
end
end
end
end
12 changes: 12 additions & 0 deletions spec/graphql/language/block_string_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,18 @@ def trim_whitespace(str)
# Doesn't crash when the string is only a newline
"\n",
""
],
[
# Removes long blank lines
" \n \n
Hello,
World!
Yours,
GraphQL.
\n \n",
"Hello,\n World!\n\nYours,\n GraphQL."
]
]

Expand Down

0 comments on commit 109c26c

Please sign in to comment.