Skip to content

Commit a5c3d63

Browse files
committed
Implement special array literals for ripper translation
1 parent a3156e6 commit a5c3d63

File tree

2 files changed

+60
-27
lines changed

2 files changed

+60
-27
lines changed

lib/prism/translation/ripper.rb

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,66 @@ def visit_and_node(node)
239239
# []
240240
# ^^
241241
def visit_array_node(node)
242-
elements = visit_arguments(node.elements) unless node.elements.empty?
242+
bounds(node.location)
243+
elements =
244+
case node.opening
245+
when /^%w/
246+
node.elements.inject(on_qwords_new) do |qwords, element|
247+
bounds(element.location)
248+
on_qwords_add(qwords, on_tstring_content(element.content))
249+
end
250+
when /^%i/
251+
node.elements.inject(on_qsymbols_new) do |qsymbols, element|
252+
bounds(element.location)
253+
on_qsymbols_add(qsymbols, on_tstring_content(element.value))
254+
end
255+
when /^%W/
256+
node.elements.inject(on_words_new) do |words, element|
257+
bounds(element.location)
258+
word =
259+
if element.is_a?(StringNode)
260+
on_word_add(on_word_new, on_tstring_content(element.content))
261+
else
262+
element.parts.inject(on_word_new) do |word, part|
263+
word_part =
264+
if part.is_a?(StringNode)
265+
bounds(part.location)
266+
on_tstring_content(part.content)
267+
else
268+
visit(part)
269+
end
270+
271+
on_word_add(word, word_part)
272+
end
273+
end
274+
275+
on_words_add(words, word)
276+
end
277+
when /^%I/
278+
node.elements.inject(on_symbols_new) do |symbols, element|
279+
bounds(element.location)
280+
symbol =
281+
if element.is_a?(SymbolNode)
282+
on_word_add(on_word_new, on_tstring_content(element.value))
283+
else
284+
element.parts.inject(on_word_new) do |word, part|
285+
word_part =
286+
if part.is_a?(StringNode)
287+
bounds(part.location)
288+
on_tstring_content(part.content)
289+
else
290+
visit(part)
291+
end
292+
293+
on_word_add(word, word_part)
294+
end
295+
end
296+
297+
on_symbols_add(symbols, symbol)
298+
end
299+
else
300+
visit_arguments(node.elements) unless node.elements.empty?
301+
end
243302

244303
bounds(node.location)
245304
on_array(elements)

test/prism/ripper_test.rb

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ class RipperTest < TestCase
4343
rescue.txt
4444
return.txt
4545
seattlerb/TestRubyParserShared.txt
46-
seattlerb/array_lits_trailing_calls.txt
4746
seattlerb/begin_rescue_else_ensure_bodies.txt
4847
seattlerb/begin_rescue_else_ensure_no_bodies.txt
4948
seattlerb/block_break.txt
@@ -115,8 +114,6 @@ class RipperTest < TestCase
115114
seattlerb/heredoc_with_only_carriage_returns.txt
116115
seattlerb/heredoc_with_only_carriage_returns_windows.txt
117116
seattlerb/if_elsif.txt
118-
seattlerb/interpolated_symbol_array_line_breaks.txt
119-
seattlerb/interpolated_word_array_line_breaks.txt
120117
seattlerb/lambda_do_vs_brace.txt
121118
seattlerb/lasgn_middle_splat.txt
122119
seattlerb/magic_encoding_comment.txt
@@ -141,8 +138,6 @@ class RipperTest < TestCase
141138
seattlerb/mlhs_mid_anonsplat.txt
142139
seattlerb/mlhs_mid_splat.txt
143140
seattlerb/module_comments.txt
144-
seattlerb/non_interpolated_symbol_array_line_breaks.txt
145-
seattlerb/non_interpolated_word_array_line_breaks.txt
146141
seattlerb/parse_if_not_canonical.txt
147142
seattlerb/parse_if_not_noncanonical.txt
148143
seattlerb/parse_line_defn_complex.txt
@@ -158,18 +153,10 @@ class RipperTest < TestCase
158153
seattlerb/parse_pattern_051.txt
159154
seattlerb/parse_pattern_058.txt
160155
seattlerb/parse_pattern_076.txt
161-
seattlerb/pctW_lineno.txt
162156
seattlerb/pct_nl.txt
163-
seattlerb/pct_w_heredoc_interp_nested.txt
164-
seattlerb/qWords_space.txt
165-
seattlerb/qsymbols.txt
166-
seattlerb/qsymbols_empty.txt
167-
seattlerb/qsymbols_empty_space.txt
168-
seattlerb/qsymbols_interp.txt
169157
seattlerb/quoted_symbol_hash_arg.txt
170158
seattlerb/quoted_symbol_keys.txt
171159
seattlerb/qw_escape_term.txt
172-
seattlerb/qwords_empty.txt
173160
seattlerb/read_escape_unicode_curlies.txt
174161
seattlerb/read_escape_unicode_h4.txt
175162
seattlerb/regexp_esc_C_slash.txt
@@ -189,13 +176,7 @@ class RipperTest < TestCase
189176
seattlerb/str_lit_concat_bad_encodings.txt
190177
seattlerb/str_newline_hash_line_number.txt
191178
seattlerb/str_single_double_escaped_newline.txt
192-
seattlerb/symbol_list.txt
193-
seattlerb/symbols.txt
194-
seattlerb/symbols_empty.txt
195-
seattlerb/symbols_empty_space.txt
196-
seattlerb/symbols_interp.txt
197179
seattlerb/thingy.txt
198-
seattlerb/words_interp.txt
199180
seattlerb/yield_call_assocs.txt
200181
seattlerb/yield_empty_parens.txt
201182
single_method_call_with_bang.txt
@@ -250,12 +231,6 @@ class RipperTest < TestCase
250231
whitequark/args_block_pass.txt
251232
whitequark/args_cmd.txt
252233
whitequark/args_star.txt
253-
whitequark/array_symbols.txt
254-
whitequark/array_symbols_empty.txt
255-
whitequark/array_symbols_interp.txt
256-
whitequark/array_words.txt
257-
whitequark/array_words_empty.txt
258-
whitequark/array_words_interp.txt
259234
whitequark/asgn_mrhs.txt
260235
whitequark/break_block.txt
261236
whitequark/bug_480.txt
@@ -283,7 +258,6 @@ class RipperTest < TestCase
283258
whitequark/hash_label_end.txt
284259
whitequark/if_else.txt
285260
whitequark/if_elsif.txt
286-
whitequark/interp_digit_var.txt
287261
whitequark/kwbegin_compstmt.txt
288262
whitequark/kwoptarg_with_kwrestarg_and_forwarded_args.txt
289263
whitequark/lvar_injecting_match.txt

0 commit comments

Comments
 (0)