Skip to content

Commit

Permalink
Implement special array literals for ripper translation
Browse files Browse the repository at this point in the history
  • Loading branch information
kddnewton committed Mar 6, 2024
1 parent a3156e6 commit a5c3d63
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 27 deletions.
61 changes: 60 additions & 1 deletion lib/prism/translation/ripper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,66 @@ def visit_and_node(node)
# []
# ^^
def visit_array_node(node)
elements = visit_arguments(node.elements) unless node.elements.empty?
bounds(node.location)
elements =
case node.opening
when /^%w/
node.elements.inject(on_qwords_new) do |qwords, element|
bounds(element.location)
on_qwords_add(qwords, on_tstring_content(element.content))
end
when /^%i/
node.elements.inject(on_qsymbols_new) do |qsymbols, element|
bounds(element.location)
on_qsymbols_add(qsymbols, on_tstring_content(element.value))
end
when /^%W/
node.elements.inject(on_words_new) do |words, element|
bounds(element.location)
word =
if element.is_a?(StringNode)
on_word_add(on_word_new, on_tstring_content(element.content))
else
element.parts.inject(on_word_new) do |word, part|
word_part =
if part.is_a?(StringNode)
bounds(part.location)
on_tstring_content(part.content)
else
visit(part)
end

on_word_add(word, word_part)
end
end

on_words_add(words, word)
end
when /^%I/
node.elements.inject(on_symbols_new) do |symbols, element|
bounds(element.location)
symbol =
if element.is_a?(SymbolNode)
on_word_add(on_word_new, on_tstring_content(element.value))
else
element.parts.inject(on_word_new) do |word, part|
word_part =
if part.is_a?(StringNode)
bounds(part.location)
on_tstring_content(part.content)
else
visit(part)
end

on_word_add(word, word_part)
end
end

on_symbols_add(symbols, symbol)
end
else
visit_arguments(node.elements) unless node.elements.empty?
end

bounds(node.location)
on_array(elements)
Expand Down
26 changes: 0 additions & 26 deletions test/prism/ripper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ class RipperTest < TestCase
rescue.txt
return.txt
seattlerb/TestRubyParserShared.txt
seattlerb/array_lits_trailing_calls.txt
seattlerb/begin_rescue_else_ensure_bodies.txt
seattlerb/begin_rescue_else_ensure_no_bodies.txt
seattlerb/block_break.txt
Expand Down Expand Up @@ -115,8 +114,6 @@ class RipperTest < TestCase
seattlerb/heredoc_with_only_carriage_returns.txt
seattlerb/heredoc_with_only_carriage_returns_windows.txt
seattlerb/if_elsif.txt
seattlerb/interpolated_symbol_array_line_breaks.txt
seattlerb/interpolated_word_array_line_breaks.txt
seattlerb/lambda_do_vs_brace.txt
seattlerb/lasgn_middle_splat.txt
seattlerb/magic_encoding_comment.txt
Expand All @@ -141,8 +138,6 @@ class RipperTest < TestCase
seattlerb/mlhs_mid_anonsplat.txt
seattlerb/mlhs_mid_splat.txt
seattlerb/module_comments.txt
seattlerb/non_interpolated_symbol_array_line_breaks.txt
seattlerb/non_interpolated_word_array_line_breaks.txt
seattlerb/parse_if_not_canonical.txt
seattlerb/parse_if_not_noncanonical.txt
seattlerb/parse_line_defn_complex.txt
Expand All @@ -158,18 +153,10 @@ class RipperTest < TestCase
seattlerb/parse_pattern_051.txt
seattlerb/parse_pattern_058.txt
seattlerb/parse_pattern_076.txt
seattlerb/pctW_lineno.txt
seattlerb/pct_nl.txt
seattlerb/pct_w_heredoc_interp_nested.txt
seattlerb/qWords_space.txt
seattlerb/qsymbols.txt
seattlerb/qsymbols_empty.txt
seattlerb/qsymbols_empty_space.txt
seattlerb/qsymbols_interp.txt
seattlerb/quoted_symbol_hash_arg.txt
seattlerb/quoted_symbol_keys.txt
seattlerb/qw_escape_term.txt
seattlerb/qwords_empty.txt
seattlerb/read_escape_unicode_curlies.txt
seattlerb/read_escape_unicode_h4.txt
seattlerb/regexp_esc_C_slash.txt
Expand All @@ -189,13 +176,7 @@ class RipperTest < TestCase
seattlerb/str_lit_concat_bad_encodings.txt
seattlerb/str_newline_hash_line_number.txt
seattlerb/str_single_double_escaped_newline.txt
seattlerb/symbol_list.txt
seattlerb/symbols.txt
seattlerb/symbols_empty.txt
seattlerb/symbols_empty_space.txt
seattlerb/symbols_interp.txt
seattlerb/thingy.txt
seattlerb/words_interp.txt
seattlerb/yield_call_assocs.txt
seattlerb/yield_empty_parens.txt
single_method_call_with_bang.txt
Expand Down Expand Up @@ -250,12 +231,6 @@ class RipperTest < TestCase
whitequark/args_block_pass.txt
whitequark/args_cmd.txt
whitequark/args_star.txt
whitequark/array_symbols.txt
whitequark/array_symbols_empty.txt
whitequark/array_symbols_interp.txt
whitequark/array_words.txt
whitequark/array_words_empty.txt
whitequark/array_words_interp.txt
whitequark/asgn_mrhs.txt
whitequark/break_block.txt
whitequark/bug_480.txt
Expand Down Expand Up @@ -283,7 +258,6 @@ class RipperTest < TestCase
whitequark/hash_label_end.txt
whitequark/if_else.txt
whitequark/if_elsif.txt
whitequark/interp_digit_var.txt
whitequark/kwbegin_compstmt.txt
whitequark/kwoptarg_with_kwrestarg_and_forwarded_args.txt
whitequark/lvar_injecting_match.txt
Expand Down

0 comments on commit a5c3d63

Please sign in to comment.