Skip to content

Commit e332952

Browse files
authored
Merge pull request #3398 from Earlopain/parser-translator-%-array-space
Fix parser translator tSPACE tokens for percent arrays
2 parents 6cd258d + 1019625 commit e332952

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

lib/prism/translation/parser/lexer.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,15 @@ def to_a
457457
location = range(token.location.start_offset, token.location.start_offset + 1)
458458
end
459459

460-
quote_stack.pop
460+
if percent_array?(quote_stack.pop)
461+
prev_token = lexed[index - 2][0] if index - 2 >= 0
462+
empty = %i[PERCENT_LOWER_I PERCENT_LOWER_W PERCENT_UPPER_I PERCENT_UPPER_W].include?(prev_token&.type)
463+
ends_with_whitespace = prev_token&.type == :WORDS_SEP
464+
# parser always emits a space token after content in a percent array, even if no actual whitespace is present.
465+
if !empty && !ends_with_whitespace
466+
tokens << [:tSPACE, [nil, range(token.location.start_offset, token.location.start_offset)]]
467+
end
468+
end
461469
when :tSYMBEG
462470
if (next_token = lexed[index][0]) && next_token.type != :STRING_CONTENT && next_token.type != :EMBEXPR_BEGIN && next_token.type != :EMBVAR && next_token.type != :STRING_END
463471
next_location = token.location.join(next_token.location)

test/prism/ruby/parser_test.rb

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -219,13 +219,6 @@ def assert_equal_tokens(expected_tokens, actual_tokens)
219219
expected_index += 1
220220
actual_index += 1
221221

222-
# The parser gem always has a space before a string end in list
223-
# literals, but we don't. So we'll skip over the space.
224-
if expected_token[0] == :tSPACE && actual_token[0] == :tSTRING_END
225-
expected_index += 1
226-
next
227-
end
228-
229222
# There are a lot of tokens that have very specific meaning according
230223
# to the context of the parser. We don't expose that information in
231224
# prism, so we need to normalize these tokens a bit.

0 commit comments

Comments
 (0)