refract 1.2.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +30 -1
- data/lib/refract/basic_visitor.rb +3 -1
- data/lib/refract/converter.rb +37 -31
- data/lib/refract/formatter.rb +665 -312
- data/lib/refract/mutation_visitor.rb +8 -3
- data/lib/refract/nodes/alias_global_variable_node.rb +1 -1
- data/lib/refract/nodes/alias_method_node.rb +1 -1
- data/lib/refract/nodes/alternation_pattern_node.rb +1 -1
- data/lib/refract/nodes/and_node.rb +1 -1
- data/lib/refract/nodes/arguments_node.rb +1 -1
- data/lib/refract/nodes/array_node.rb +1 -1
- data/lib/refract/nodes/array_pattern_node.rb +1 -1
- data/lib/refract/nodes/assoc_node.rb +1 -1
- data/lib/refract/nodes/assoc_splat_node.rb +1 -1
- data/lib/refract/nodes/back_reference_read_node.rb +1 -1
- data/lib/refract/nodes/begin_node.rb +1 -1
- data/lib/refract/nodes/block_argument_node.rb +1 -1
- data/lib/refract/nodes/block_local_variable_node.rb +1 -1
- data/lib/refract/nodes/block_node.rb +1 -1
- data/lib/refract/nodes/block_parameter_node.rb +1 -1
- data/lib/refract/nodes/block_parameters_node.rb +1 -1
- data/lib/refract/nodes/break_node.rb +1 -1
- data/lib/refract/nodes/call_and_write_node.rb +1 -1
- data/lib/refract/nodes/call_node.rb +1 -1
- data/lib/refract/nodes/call_operator_write_node.rb +1 -1
- data/lib/refract/nodes/call_or_write_node.rb +1 -1
- data/lib/refract/nodes/call_target_node.rb +3 -2
- data/lib/refract/nodes/capture_pattern_node.rb +1 -1
- data/lib/refract/nodes/case_match_node.rb +1 -1
- data/lib/refract/nodes/case_node.rb +1 -1
- data/lib/refract/nodes/class_node.rb +1 -1
- data/lib/refract/nodes/class_variable_and_write_node.rb +1 -1
- data/lib/refract/nodes/class_variable_operator_write_node.rb +1 -1
- data/lib/refract/nodes/class_variable_or_write_node.rb +1 -1
- data/lib/refract/nodes/class_variable_read_node.rb +1 -1
- data/lib/refract/nodes/class_variable_target_node.rb +1 -1
- data/lib/refract/nodes/class_variable_write_node.rb +1 -1
- data/lib/refract/nodes/constant_and_write_node.rb +1 -1
- data/lib/refract/nodes/constant_operator_write_node.rb +1 -1
- data/lib/refract/nodes/constant_or_write_node.rb +1 -1
- data/lib/refract/nodes/constant_path_and_write_node.rb +1 -1
- data/lib/refract/nodes/constant_path_node.rb +1 -1
- data/lib/refract/nodes/constant_path_operator_write_node.rb +1 -1
- data/lib/refract/nodes/constant_path_or_write_node.rb +1 -1
- data/lib/refract/nodes/constant_path_target_node.rb +1 -1
- data/lib/refract/nodes/constant_path_write_node.rb +1 -1
- data/lib/refract/nodes/constant_read_node.rb +1 -1
- data/lib/refract/nodes/constant_target_node.rb +1 -1
- data/lib/refract/nodes/constant_write_node.rb +1 -1
- data/lib/refract/nodes/def_node.rb +1 -1
- data/lib/refract/nodes/defined_node.rb +1 -1
- data/lib/refract/nodes/else_node.rb +1 -1
- data/lib/refract/nodes/embedded_statements_node.rb +1 -1
- data/lib/refract/nodes/embedded_variable_node.rb +1 -1
- data/lib/refract/nodes/ensure_node.rb +1 -1
- data/lib/refract/nodes/find_pattern_node.rb +1 -1
- data/lib/refract/nodes/flip_flop_node.rb +1 -1
- data/lib/refract/nodes/float_node.rb +1 -1
- data/lib/refract/nodes/for_node.rb +1 -1
- data/lib/refract/nodes/forwarding_super_node.rb +1 -1
- data/lib/refract/nodes/global_variable_and_write_node.rb +1 -1
- data/lib/refract/nodes/global_variable_operator_write_node.rb +1 -1
- data/lib/refract/nodes/global_variable_or_write_node.rb +1 -1
- data/lib/refract/nodes/global_variable_read_node.rb +1 -1
- data/lib/refract/nodes/global_variable_target_node.rb +1 -1
- data/lib/refract/nodes/global_variable_write_node.rb +1 -1
- data/lib/refract/nodes/hash_node.rb +1 -1
- data/lib/refract/nodes/hash_pattern_node.rb +1 -1
- data/lib/refract/nodes/if_node.rb +1 -1
- data/lib/refract/nodes/imaginary_node.rb +1 -1
- data/lib/refract/nodes/implicit_node.rb +1 -1
- data/lib/refract/nodes/in_node.rb +1 -1
- data/lib/refract/nodes/index_and_write_node.rb +1 -1
- data/lib/refract/nodes/index_operator_write_node.rb +1 -1
- data/lib/refract/nodes/index_or_write_node.rb +1 -1
- data/lib/refract/nodes/index_target_node.rb +1 -1
- data/lib/refract/nodes/instance_variable_and_write_node.rb +1 -1
- data/lib/refract/nodes/instance_variable_operator_write_node.rb +1 -1
- data/lib/refract/nodes/instance_variable_or_write_node.rb +1 -1
- data/lib/refract/nodes/instance_variable_read_node.rb +1 -1
- data/lib/refract/nodes/instance_variable_target_node.rb +1 -1
- data/lib/refract/nodes/instance_variable_write_node.rb +1 -1
- data/lib/refract/nodes/integer_node.rb +1 -1
- data/lib/refract/nodes/interpolated_match_last_line_node.rb +1 -1
- data/lib/refract/nodes/interpolated_regular_expression_node.rb +1 -1
- data/lib/refract/nodes/interpolated_string_node.rb +1 -1
- data/lib/refract/nodes/interpolated_symbol_node.rb +1 -1
- data/lib/refract/nodes/interpolated_x_string_node.rb +1 -1
- data/lib/refract/nodes/keyword_hash_node.rb +1 -1
- data/lib/refract/nodes/keyword_rest_parameter_node.rb +1 -1
- data/lib/refract/nodes/lambda_node.rb +1 -1
- data/lib/refract/nodes/local_variable_and_write_node.rb +1 -1
- data/lib/refract/nodes/local_variable_operator_write_node.rb +1 -1
- data/lib/refract/nodes/local_variable_or_write_node.rb +1 -1
- data/lib/refract/nodes/local_variable_read_node.rb +3 -2
- data/lib/refract/nodes/local_variable_target_node.rb +3 -2
- data/lib/refract/nodes/local_variable_write_node.rb +3 -2
- data/lib/refract/nodes/match_last_line_node.rb +1 -1
- data/lib/refract/nodes/match_predicate_node.rb +1 -1
- data/lib/refract/nodes/match_required_node.rb +1 -1
- data/lib/refract/nodes/match_write_node.rb +1 -1
- data/lib/refract/nodes/module_node.rb +1 -1
- data/lib/refract/nodes/multi_target_node.rb +1 -1
- data/lib/refract/nodes/multi_write_node.rb +1 -1
- data/lib/refract/nodes/next_node.rb +1 -1
- data/lib/refract/nodes/numbered_reference_read_node.rb +1 -1
- data/lib/refract/nodes/optional_keyword_parameter_node.rb +1 -1
- data/lib/refract/nodes/optional_parameter_node.rb +1 -1
- data/lib/refract/nodes/or_node.rb +1 -1
- data/lib/refract/nodes/parameters_node.rb +1 -1
- data/lib/refract/nodes/parentheses_node.rb +1 -1
- data/lib/refract/nodes/pinned_expression_node.rb +1 -1
- data/lib/refract/nodes/pinned_variable_node.rb +1 -1
- data/lib/refract/nodes/post_execution_node.rb +1 -1
- data/lib/refract/nodes/pre_execution_node.rb +1 -1
- data/lib/refract/nodes/program_node.rb +1 -1
- data/lib/refract/nodes/range_node.rb +1 -1
- data/lib/refract/nodes/rational_node.rb +1 -1
- data/lib/refract/nodes/regular_expression_node.rb +1 -1
- data/lib/refract/nodes/required_keyword_parameter_node.rb +1 -1
- data/lib/refract/nodes/required_parameter_node.rb +1 -1
- data/lib/refract/nodes/rescue_modifier_node.rb +1 -1
- data/lib/refract/nodes/rescue_node.rb +1 -1
- data/lib/refract/nodes/rest_parameter_node.rb +1 -1
- data/lib/refract/nodes/return_node.rb +1 -1
- data/lib/refract/nodes/shareable_constant_node.rb +1 -1
- data/lib/refract/nodes/singleton_class_node.rb +1 -1
- data/lib/refract/nodes/splat_node.rb +1 -1
- data/lib/refract/nodes/statements_node.rb +1 -1
- data/lib/refract/nodes/string_node.rb +1 -1
- data/lib/refract/nodes/super_node.rb +1 -1
- data/lib/refract/nodes/symbol_node.rb +1 -1
- data/lib/refract/nodes/undef_node.rb +1 -1
- data/lib/refract/nodes/unless_node.rb +1 -1
- data/lib/refract/nodes/until_node.rb +1 -1
- data/lib/refract/nodes/when_node.rb +1 -1
- data/lib/refract/nodes/while_node.rb +1 -1
- data/lib/refract/nodes/x_string_node.rb +1 -1
- data/lib/refract/nodes/yield_node.rb +1 -1
- data/lib/refract/version.rb +1 -1
- data/lib/refract/visitor.rb +2 -2
- data/lib/refract.rb +49 -30
- data/lib/rubocop/cop/refract/ordered_visit_methods.rb +12 -84
- metadata +4 -4
data/lib/refract/formatter.rb
CHANGED
|
@@ -30,11 +30,12 @@ module Refract
|
|
|
30
30
|
|
|
31
31
|
# In a regexp, `#` only needs escaping where it would start interpolation.
|
|
32
32
|
# The end of the source counts, since the closing `/` can form `$/`.
|
|
33
|
-
REGEXP_INTERPOLATION = %r{#(?=\{|@@?(?:[A-Za-z_]|[^\x00-\x7F])|\$(?:[A-Za-z_~*$?!@/\\;,.=:<>"&`'+\d]
|
|
33
|
+
REGEXP_INTERPOLATION = %r{#(?=\{|@@?(?:[A-Za-z_]|[^\x00-\x7F])|\$(?:[A-Za-z_~*$?!@/\\;,.=:<>"&`'+\d]|-(?:\w|[^\x00-\x7F])|[^\x00-\x7F]|\z))}
|
|
34
34
|
|
|
35
35
|
ESCAPE_PATTERNS = {
|
|
36
36
|
'"' => /[\\"]|#(?=[{@$])|[^[:print:]]/,
|
|
37
37
|
"`" => /[\\`]|#(?=[{@$])|[^[:print:]]/,
|
|
38
|
+
"]" => /[\\\[\]\s]|#(?=[{@$])|[^[:print:]]/,
|
|
38
39
|
}.freeze
|
|
39
40
|
|
|
40
41
|
NON_ASCII = /[^\x00-\x7F]/
|
|
@@ -48,40 +49,36 @@ module Refract
|
|
|
48
49
|
|
|
49
50
|
def initialize(starting_line: 1)
|
|
50
51
|
super()
|
|
51
|
-
@
|
|
52
|
-
@source_map = []
|
|
53
|
-
@inferred_lines = Set.new
|
|
54
|
-
@current_line = starting_line
|
|
55
|
-
@indent = 0
|
|
56
|
-
@shareable_constant_value = :none
|
|
52
|
+
@starting_line = starting_line
|
|
57
53
|
end
|
|
58
54
|
|
|
59
55
|
def around_visit(node, &)
|
|
60
|
-
value =
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
56
|
+
if (value = shareable_constant_value_for(node))
|
|
57
|
+
multiline_parens(@stack.length > 1 && !statement?(node)) do
|
|
58
|
+
shareable_constant_value(value) { map_source_and_visit(node, &) }
|
|
59
|
+
end
|
|
64
60
|
else
|
|
65
61
|
map_source_and_visit(node, &)
|
|
66
62
|
end
|
|
67
63
|
end
|
|
68
64
|
|
|
69
|
-
def visit_each(nodes)
|
|
70
|
-
nodes
|
|
71
|
-
last = nodes.length - 1
|
|
72
|
-
|
|
73
|
-
nodes.each_with_index do |node, index|
|
|
74
|
-
length = @buffer.length
|
|
75
|
-
visit node
|
|
76
|
-
yield if block_given? && index < last && @buffer.length > length
|
|
77
|
-
end
|
|
65
|
+
def visit_each(nodes, &separator)
|
|
66
|
+
separate(nodes, separator) { |node| visit node }
|
|
78
67
|
end
|
|
79
68
|
|
|
80
69
|
def format_node(node)
|
|
70
|
+
@buffer = []
|
|
71
|
+
@source_map = []
|
|
72
|
+
@inferred_lines = Set.new
|
|
73
|
+
@current_line = @starting_line
|
|
74
|
+
@indent = 0
|
|
75
|
+
@shareable_constant_value = :none
|
|
76
|
+
@encoding = node&.prism_node&.slice&.encoding || Encoding::UTF_8
|
|
77
|
+
|
|
81
78
|
visit(node)
|
|
82
79
|
|
|
83
80
|
Result.new(
|
|
84
|
-
source: @buffer
|
|
81
|
+
source: join(@buffer),
|
|
85
82
|
source_map: @source_map,
|
|
86
83
|
)
|
|
87
84
|
end
|
|
@@ -109,23 +106,31 @@ module Refract
|
|
|
109
106
|
end
|
|
110
107
|
|
|
111
108
|
visit AndNode do |node|
|
|
112
|
-
|
|
109
|
+
operand node.left
|
|
113
110
|
space
|
|
114
111
|
push node.operator
|
|
115
112
|
space
|
|
116
|
-
|
|
113
|
+
operand node.right
|
|
117
114
|
end
|
|
118
115
|
|
|
119
116
|
visit ArgumentsNode do |node|
|
|
120
|
-
|
|
117
|
+
visit_operands(node.arguments, after_keyword: (@stack[-2] in ReturnNode | BreakNode | NextNode)) { push ", " }
|
|
121
118
|
end
|
|
122
119
|
|
|
123
120
|
visit ArrayNode do |node|
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
121
|
+
elements = Array(node.elements)
|
|
122
|
+
|
|
123
|
+
if pattern? && (prefix = word_list_prefix(elements))
|
|
124
|
+
word_list(prefix, elements)
|
|
125
|
+
else
|
|
126
|
+
pinned(pattern?) do
|
|
127
|
+
brackets do
|
|
128
|
+
indent do
|
|
129
|
+
visit_operands(elements) { push ","; new_line }
|
|
130
|
+
end
|
|
131
|
+
new_line
|
|
132
|
+
end
|
|
127
133
|
end
|
|
128
|
-
new_line
|
|
129
134
|
end
|
|
130
135
|
end
|
|
131
136
|
|
|
@@ -137,28 +142,32 @@ module Refract
|
|
|
137
142
|
end
|
|
138
143
|
|
|
139
144
|
visit AssocNode do |node|
|
|
145
|
+
pattern = HashPatternNode === @stack[-2]
|
|
146
|
+
|
|
140
147
|
case node.key
|
|
141
|
-
|
|
148
|
+
in SymbolNode unless leading_jump_argument?
|
|
142
149
|
bare = !node.key.quoted && bare?(node.key.unescaped, LABEL)
|
|
143
150
|
push(bare ? node.key.unescaped : quote(node.key.unescaped))
|
|
144
151
|
push ":"
|
|
145
152
|
|
|
146
|
-
if ImplicitNode === node.value && bare && shorthand?(node.key, node.value.value)
|
|
147
|
-
visit node.value
|
|
148
|
-
else
|
|
149
|
-
space
|
|
150
|
-
visit((ImplicitNode === node.value) ? node.value.value : node.value)
|
|
153
|
+
if ImplicitNode === node.value && (bare || pattern) && shorthand?(node.key, node.value.value)
|
|
154
|
+
return visit node.value
|
|
151
155
|
end
|
|
156
|
+
|
|
157
|
+
space
|
|
158
|
+
value = (ImplicitNode === node.value) ? node.value.value : node.value
|
|
152
159
|
else
|
|
153
|
-
|
|
160
|
+
operand node.key
|
|
154
161
|
push " => "
|
|
155
|
-
|
|
162
|
+
value = (ImplicitNode === node.value) ? node.value.value : node.value
|
|
156
163
|
end
|
|
164
|
+
|
|
165
|
+
pattern ? visit(value) : operand(value)
|
|
157
166
|
end
|
|
158
167
|
|
|
159
168
|
visit AssocSplatNode do |node|
|
|
160
169
|
push "**"
|
|
161
|
-
|
|
170
|
+
operand node.value
|
|
162
171
|
end
|
|
163
172
|
|
|
164
173
|
visit BackReferenceReadNode do |node|
|
|
@@ -194,7 +203,7 @@ module Refract
|
|
|
194
203
|
|
|
195
204
|
visit BlockArgumentNode do |node|
|
|
196
205
|
push "&"
|
|
197
|
-
|
|
206
|
+
operand node.expression
|
|
198
207
|
end
|
|
199
208
|
|
|
200
209
|
visit BlockLocalVariableNode do |node|
|
|
@@ -204,9 +213,9 @@ module Refract
|
|
|
204
213
|
visit BlockNode do |node|
|
|
205
214
|
braces do
|
|
206
215
|
case node.parameters
|
|
207
|
-
|
|
216
|
+
in nil
|
|
208
217
|
nil
|
|
209
|
-
|
|
218
|
+
in ItParametersNode | NumberedParametersNode
|
|
210
219
|
visit node.parameters
|
|
211
220
|
else
|
|
212
221
|
space
|
|
@@ -249,41 +258,45 @@ module Refract
|
|
|
249
258
|
call_receiver(node)
|
|
250
259
|
push node.read_name
|
|
251
260
|
push " &&= "
|
|
252
|
-
|
|
261
|
+
operand node.value
|
|
253
262
|
end
|
|
254
263
|
|
|
255
264
|
visit CallNode do |node|
|
|
256
|
-
|
|
265
|
+
if not?(node)
|
|
266
|
+
push "!"
|
|
267
|
+
return operand node.receiver
|
|
268
|
+
end
|
|
257
269
|
|
|
258
|
-
if node
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
parenthesize(low_precedence?(node.receiver)) { visit node.receiver }
|
|
262
|
-
brackets { visit_each(index) { push ", " } }
|
|
263
|
-
push " = "
|
|
264
|
-
visit value
|
|
265
|
-
return
|
|
266
|
-
elsif node.name.match?(SETTER) && arguments.length == 1
|
|
270
|
+
if setter?(node)
|
|
271
|
+
case [node.receiver, node.arguments&.arguments, node.block]
|
|
272
|
+
in [Node, [Node => value], nil] unless value in SplatNode | KeywordHashNode | BlockArgumentNode
|
|
267
273
|
call_receiver(node)
|
|
268
274
|
push node.name.name.delete_suffix("=")
|
|
269
275
|
push " = "
|
|
270
|
-
|
|
271
|
-
|
|
276
|
+
return operand value
|
|
277
|
+
else
|
|
278
|
+
raise ArgumentError, "A call to #{node.name} needs a receiver, one positional argument and no block."
|
|
272
279
|
end
|
|
280
|
+
elsif index_write?(node)
|
|
281
|
+
*index, value = node.arguments.arguments
|
|
282
|
+
receiver node.receiver
|
|
283
|
+
brackets { visit_operands(index) { push ", " } }
|
|
284
|
+
push " = "
|
|
285
|
+
return operand value
|
|
273
286
|
end
|
|
274
287
|
|
|
275
288
|
call_receiver(node)
|
|
276
289
|
push node.name
|
|
277
290
|
|
|
278
291
|
case node.block
|
|
279
|
-
|
|
292
|
+
in BlockNode
|
|
280
293
|
if node.arguments
|
|
281
294
|
parens { visit node.arguments }
|
|
282
295
|
end
|
|
283
296
|
|
|
284
297
|
space
|
|
285
298
|
visit node.block
|
|
286
|
-
|
|
299
|
+
in BlockArgumentNode
|
|
287
300
|
parens do
|
|
288
301
|
if node.arguments
|
|
289
302
|
visit node.arguments
|
|
@@ -307,23 +320,20 @@ module Refract
|
|
|
307
320
|
space
|
|
308
321
|
push node.binary_operator
|
|
309
322
|
push "= "
|
|
310
|
-
|
|
323
|
+
operand node.value
|
|
311
324
|
end
|
|
312
325
|
|
|
313
326
|
visit CallOrWriteNode do |node|
|
|
314
327
|
call_receiver(node)
|
|
315
328
|
push node.read_name
|
|
316
329
|
push " ||= "
|
|
317
|
-
|
|
330
|
+
operand node.value
|
|
318
331
|
end
|
|
319
332
|
|
|
320
333
|
visit CallTargetNode do |node|
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
end
|
|
325
|
-
|
|
326
|
-
push node.name[0..-2]
|
|
334
|
+
receiver node.receiver
|
|
335
|
+
push(node.safe_navigation ? "&." : ".")
|
|
336
|
+
push node.name.name.delete_suffix("=")
|
|
327
337
|
end
|
|
328
338
|
|
|
329
339
|
visit CapturePatternNode do |node|
|
|
@@ -334,7 +344,7 @@ module Refract
|
|
|
334
344
|
|
|
335
345
|
visit CaseMatchNode do |node|
|
|
336
346
|
push "case "
|
|
337
|
-
|
|
347
|
+
operand node.predicate
|
|
338
348
|
new_line
|
|
339
349
|
visit_each(node.conditions) { new_line }
|
|
340
350
|
new_line
|
|
@@ -347,7 +357,7 @@ module Refract
|
|
|
347
357
|
|
|
348
358
|
visit CaseNode do |node|
|
|
349
359
|
push "case "
|
|
350
|
-
|
|
360
|
+
operand node.predicate
|
|
351
361
|
new_line
|
|
352
362
|
visit_each(node.conditions) { new_line }
|
|
353
363
|
new_line
|
|
@@ -364,7 +374,7 @@ module Refract
|
|
|
364
374
|
visit node.constant_path
|
|
365
375
|
if node.superclass
|
|
366
376
|
push " < "
|
|
367
|
-
|
|
377
|
+
operand node.superclass
|
|
368
378
|
end
|
|
369
379
|
if node.body
|
|
370
380
|
indent do
|
|
@@ -378,19 +388,19 @@ module Refract
|
|
|
378
388
|
visit ClassVariableAndWriteNode do |node|
|
|
379
389
|
push node.name
|
|
380
390
|
push " &&= "
|
|
381
|
-
|
|
391
|
+
operand node.value
|
|
382
392
|
end
|
|
383
393
|
|
|
384
394
|
visit ClassVariableOperatorWriteNode do |node|
|
|
385
395
|
push node.name
|
|
386
396
|
push " #{node.binary_operator}= "
|
|
387
|
-
|
|
397
|
+
operand node.value
|
|
388
398
|
end
|
|
389
399
|
|
|
390
400
|
visit ClassVariableOrWriteNode do |node|
|
|
391
401
|
push node.name
|
|
392
402
|
push " ||= "
|
|
393
|
-
|
|
403
|
+
operand node.value
|
|
394
404
|
end
|
|
395
405
|
|
|
396
406
|
visit ClassVariableReadNode do |node|
|
|
@@ -404,35 +414,35 @@ module Refract
|
|
|
404
414
|
visit ClassVariableWriteNode do |node|
|
|
405
415
|
push node.name
|
|
406
416
|
push " = "
|
|
407
|
-
|
|
417
|
+
operand node.value
|
|
408
418
|
end
|
|
409
419
|
|
|
410
420
|
visit ConstantAndWriteNode do |node|
|
|
411
421
|
push node.name
|
|
412
422
|
push " &&= "
|
|
413
|
-
|
|
423
|
+
operand node.value
|
|
414
424
|
end
|
|
415
425
|
|
|
416
426
|
visit ConstantOperatorWriteNode do |node|
|
|
417
427
|
push node.name
|
|
418
428
|
push " #{node.binary_operator}= "
|
|
419
|
-
|
|
429
|
+
operand node.value
|
|
420
430
|
end
|
|
421
431
|
|
|
422
432
|
visit ConstantOrWriteNode do |node|
|
|
423
433
|
push node.name
|
|
424
434
|
push " ||= "
|
|
425
|
-
|
|
435
|
+
operand node.value
|
|
426
436
|
end
|
|
427
437
|
|
|
428
438
|
visit ConstantPathAndWriteNode do |node|
|
|
429
439
|
visit node.target
|
|
430
440
|
push " &&= "
|
|
431
|
-
|
|
441
|
+
operand node.value
|
|
432
442
|
end
|
|
433
443
|
|
|
434
444
|
visit ConstantPathNode do |node|
|
|
435
|
-
|
|
445
|
+
receiver node.parent
|
|
436
446
|
push "::"
|
|
437
447
|
push node.name
|
|
438
448
|
end
|
|
@@ -442,25 +452,25 @@ module Refract
|
|
|
442
452
|
space
|
|
443
453
|
push node.binary_operator
|
|
444
454
|
push "= "
|
|
445
|
-
|
|
455
|
+
operand node.value
|
|
446
456
|
end
|
|
447
457
|
|
|
448
458
|
visit ConstantPathOrWriteNode do |node|
|
|
449
459
|
visit node.target
|
|
450
460
|
push " ||= "
|
|
451
|
-
|
|
461
|
+
operand node.value
|
|
452
462
|
end
|
|
453
463
|
|
|
454
464
|
visit ConstantPathTargetNode do |node|
|
|
455
|
-
|
|
456
|
-
push "::"
|
|
465
|
+
receiver node.parent
|
|
466
|
+
push "::"
|
|
457
467
|
push node.name
|
|
458
468
|
end
|
|
459
469
|
|
|
460
470
|
visit ConstantPathWriteNode do |node|
|
|
461
471
|
visit node.target
|
|
462
472
|
push " = "
|
|
463
|
-
|
|
473
|
+
operand node.value
|
|
464
474
|
end
|
|
465
475
|
|
|
466
476
|
visit ConstantReadNode do |node|
|
|
@@ -474,7 +484,7 @@ module Refract
|
|
|
474
484
|
visit ConstantWriteNode do |node|
|
|
475
485
|
push node.name
|
|
476
486
|
push " = "
|
|
477
|
-
|
|
487
|
+
operand node.value
|
|
478
488
|
end
|
|
479
489
|
|
|
480
490
|
visit DefNode do |node|
|
|
@@ -482,7 +492,9 @@ module Refract
|
|
|
482
492
|
space
|
|
483
493
|
|
|
484
494
|
if node.receiver
|
|
485
|
-
|
|
495
|
+
parenthesize(!(node.receiver in SelfNode | LocalVariableReadNode | InstanceVariableReadNode | ClassVariableReadNode | GlobalVariableReadNode | ConstantReadNode | ParenthesesNode | CallNode[variable_call: true])) do
|
|
496
|
+
operand node.receiver
|
|
497
|
+
end
|
|
486
498
|
push "."
|
|
487
499
|
end
|
|
488
500
|
|
|
@@ -508,7 +520,7 @@ module Refract
|
|
|
508
520
|
visit DefinedNode do |node|
|
|
509
521
|
push "defined?"
|
|
510
522
|
parens do
|
|
511
|
-
|
|
523
|
+
operand node.value
|
|
512
524
|
end
|
|
513
525
|
end
|
|
514
526
|
|
|
@@ -551,13 +563,26 @@ module Refract
|
|
|
551
563
|
end
|
|
552
564
|
|
|
553
565
|
visit FlipFlopNode do |node|
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
566
|
+
if condition
|
|
567
|
+
range(node)
|
|
568
|
+
else
|
|
569
|
+
push "(("
|
|
570
|
+
range(node)
|
|
571
|
+
push ") ? true : false)"
|
|
572
|
+
end
|
|
557
573
|
end
|
|
558
574
|
|
|
559
575
|
visit FloatNode do |node|
|
|
560
|
-
|
|
576
|
+
case node.value
|
|
577
|
+
in String => value
|
|
578
|
+
push value
|
|
579
|
+
in Float => value if value.nan?
|
|
580
|
+
push "::Float::NAN"
|
|
581
|
+
in Float => value if value.infinite?
|
|
582
|
+
value.positive? ? push("::Float::INFINITY") : expression { push "-::Float::INFINITY" }
|
|
583
|
+
in Float => value
|
|
584
|
+
push value.to_s
|
|
585
|
+
end
|
|
561
586
|
end
|
|
562
587
|
|
|
563
588
|
visit ForNode do |node|
|
|
@@ -565,7 +590,7 @@ module Refract
|
|
|
565
590
|
space
|
|
566
591
|
visit node.index
|
|
567
592
|
push " in "
|
|
568
|
-
|
|
593
|
+
operand node.collection
|
|
569
594
|
indent do
|
|
570
595
|
visit node.statements
|
|
571
596
|
end
|
|
@@ -589,7 +614,7 @@ module Refract
|
|
|
589
614
|
visit GlobalVariableAndWriteNode do |node|
|
|
590
615
|
push node.name
|
|
591
616
|
push " &&= "
|
|
592
|
-
|
|
617
|
+
operand node.value
|
|
593
618
|
end
|
|
594
619
|
|
|
595
620
|
visit GlobalVariableOperatorWriteNode do |node|
|
|
@@ -597,13 +622,13 @@ module Refract
|
|
|
597
622
|
space
|
|
598
623
|
push node.binary_operator
|
|
599
624
|
push "= "
|
|
600
|
-
|
|
625
|
+
operand node.value
|
|
601
626
|
end
|
|
602
627
|
|
|
603
628
|
visit GlobalVariableOrWriteNode do |node|
|
|
604
629
|
push node.name
|
|
605
630
|
push " ||= "
|
|
606
|
-
|
|
631
|
+
operand node.value
|
|
607
632
|
end
|
|
608
633
|
|
|
609
634
|
visit GlobalVariableReadNode do |node|
|
|
@@ -617,16 +642,18 @@ module Refract
|
|
|
617
642
|
visit GlobalVariableWriteNode do |node|
|
|
618
643
|
push node.name
|
|
619
644
|
push " = "
|
|
620
|
-
|
|
645
|
+
operand node.value
|
|
621
646
|
end
|
|
622
647
|
|
|
623
648
|
visit HashNode do |node|
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
649
|
+
pinned(pattern?) do
|
|
650
|
+
braces do
|
|
651
|
+
indent do
|
|
652
|
+
visit_each(node.elements) { push ","; new_line }
|
|
653
|
+
end
|
|
628
654
|
|
|
629
|
-
|
|
655
|
+
new_line
|
|
656
|
+
end
|
|
630
657
|
end
|
|
631
658
|
end
|
|
632
659
|
|
|
@@ -646,13 +673,9 @@ module Refract
|
|
|
646
673
|
end
|
|
647
674
|
|
|
648
675
|
visit IfNode do |node|
|
|
649
|
-
if node
|
|
650
|
-
visit node.statements
|
|
651
|
-
push " if "
|
|
652
|
-
visit node.predicate
|
|
653
|
-
elsif (IfNode === @stack[-2]) && @stack[-2].subsequent.equal?(node)
|
|
676
|
+
if (IfNode === @stack[-2]) && @stack[-2].subsequent.equal?(node)
|
|
654
677
|
push "elsif "
|
|
655
|
-
|
|
678
|
+
operand node.predicate
|
|
656
679
|
if node.statements
|
|
657
680
|
indent do
|
|
658
681
|
visit node.statements
|
|
@@ -662,28 +685,39 @@ module Refract
|
|
|
662
685
|
new_line
|
|
663
686
|
visit node.subsequent
|
|
664
687
|
end
|
|
688
|
+
elsif modifier?(node)
|
|
689
|
+
modifier(node, "if")
|
|
665
690
|
else
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
visit node.statements
|
|
672
|
-
end
|
|
673
|
-
end
|
|
674
|
-
if node.subsequent
|
|
675
|
-
new_line
|
|
676
|
-
visit node.subsequent
|
|
691
|
+
push "if "
|
|
692
|
+
operand node.predicate
|
|
693
|
+
if node.statements
|
|
694
|
+
indent do
|
|
695
|
+
visit node.statements
|
|
677
696
|
end
|
|
697
|
+
end
|
|
698
|
+
if node.subsequent
|
|
678
699
|
new_line
|
|
679
|
-
|
|
700
|
+
visit node.subsequent
|
|
680
701
|
end
|
|
702
|
+
new_line
|
|
703
|
+
push "end"
|
|
681
704
|
end
|
|
682
705
|
end
|
|
683
706
|
|
|
684
707
|
visit ImaginaryNode do |node|
|
|
685
|
-
|
|
686
|
-
|
|
708
|
+
case node.numeric
|
|
709
|
+
in RationalNode
|
|
710
|
+
map_source_and_visit(node.numeric) { rational(node.numeric, "ri") }
|
|
711
|
+
in FloatNode[value: Float => value] unless value.finite?
|
|
712
|
+
expression do
|
|
713
|
+
push "::Complex.rectangular(0, "
|
|
714
|
+
visit node.numeric
|
|
715
|
+
push ")"
|
|
716
|
+
end
|
|
717
|
+
else
|
|
718
|
+
visit node.numeric
|
|
719
|
+
push "i"
|
|
720
|
+
end
|
|
687
721
|
end
|
|
688
722
|
|
|
689
723
|
visit ImplicitNode do |node|
|
|
@@ -697,6 +731,7 @@ module Refract
|
|
|
697
731
|
visit InNode do |node|
|
|
698
732
|
push "in "
|
|
699
733
|
visit node.pattern
|
|
734
|
+
push " then" if endless?(node.pattern)
|
|
700
735
|
if node.statements
|
|
701
736
|
indent do
|
|
702
737
|
visit node.statements
|
|
@@ -705,33 +740,33 @@ module Refract
|
|
|
705
740
|
end
|
|
706
741
|
|
|
707
742
|
visit IndexAndWriteNode do |node|
|
|
708
|
-
|
|
743
|
+
receiver node.receiver
|
|
709
744
|
push "["
|
|
710
745
|
visit_each([node.arguments, node.block]) { push ", " }
|
|
711
746
|
push "] &&= "
|
|
712
|
-
|
|
747
|
+
operand node.value
|
|
713
748
|
end
|
|
714
749
|
|
|
715
750
|
visit IndexOperatorWriteNode do |node|
|
|
716
|
-
|
|
751
|
+
receiver node.receiver
|
|
717
752
|
push "["
|
|
718
753
|
visit_each([node.arguments, node.block]) { push ", " }
|
|
719
754
|
push "] "
|
|
720
755
|
push node.binary_operator
|
|
721
756
|
push "= "
|
|
722
|
-
|
|
757
|
+
operand node.value
|
|
723
758
|
end
|
|
724
759
|
|
|
725
760
|
visit IndexOrWriteNode do |node|
|
|
726
|
-
|
|
761
|
+
receiver node.receiver
|
|
727
762
|
push "["
|
|
728
763
|
visit_each([node.arguments, node.block]) { push ", " }
|
|
729
764
|
push "] ||= "
|
|
730
|
-
|
|
765
|
+
operand node.value
|
|
731
766
|
end
|
|
732
767
|
|
|
733
768
|
visit IndexTargetNode do |node|
|
|
734
|
-
|
|
769
|
+
receiver node.receiver
|
|
735
770
|
push "["
|
|
736
771
|
visit_each([node.arguments, node.block]) { push ", " }
|
|
737
772
|
push "]"
|
|
@@ -740,7 +775,7 @@ module Refract
|
|
|
740
775
|
visit InstanceVariableAndWriteNode do |node|
|
|
741
776
|
push node.name
|
|
742
777
|
push " &&= "
|
|
743
|
-
|
|
778
|
+
operand node.value
|
|
744
779
|
end
|
|
745
780
|
|
|
746
781
|
visit InstanceVariableOperatorWriteNode do |node|
|
|
@@ -748,13 +783,13 @@ module Refract
|
|
|
748
783
|
space
|
|
749
784
|
push node.binary_operator
|
|
750
785
|
push "= "
|
|
751
|
-
|
|
786
|
+
operand node.value
|
|
752
787
|
end
|
|
753
788
|
|
|
754
789
|
visit InstanceVariableOrWriteNode do |node|
|
|
755
790
|
push node.name
|
|
756
791
|
push " ||= "
|
|
757
|
-
|
|
792
|
+
operand node.value
|
|
758
793
|
end
|
|
759
794
|
|
|
760
795
|
visit InstanceVariableReadNode do |node|
|
|
@@ -768,7 +803,7 @@ module Refract
|
|
|
768
803
|
visit InstanceVariableWriteNode do |node|
|
|
769
804
|
push node.name
|
|
770
805
|
push " = "
|
|
771
|
-
|
|
806
|
+
operand node.value
|
|
772
807
|
end
|
|
773
808
|
|
|
774
809
|
visit IntegerNode do |node|
|
|
@@ -776,35 +811,32 @@ module Refract
|
|
|
776
811
|
end
|
|
777
812
|
|
|
778
813
|
visit InterpolatedMatchLastLineNode do |node|
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
push "/"
|
|
782
|
-
regexp_flags(node)
|
|
814
|
+
regexp(node) { visit_parts(node.parts) { |string| escape_regexp(string) } }
|
|
815
|
+
push ".=~($_)" unless condition
|
|
783
816
|
end
|
|
784
817
|
|
|
785
818
|
visit InterpolatedRegularExpressionNode do |node|
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
regexp_flags(node)
|
|
819
|
+
unconditional(condition(any_parser: true)) do
|
|
820
|
+
regexp(node) { visit_parts(node.parts) { |string| escape_regexp(string) } }
|
|
821
|
+
end
|
|
790
822
|
end
|
|
791
823
|
|
|
792
824
|
visit InterpolatedStringNode do |node|
|
|
793
825
|
doubles do
|
|
794
|
-
visit_parts(node.parts) { |string|
|
|
826
|
+
visit_parts(node.parts) { |string| escape_string(string, '"') }
|
|
795
827
|
end
|
|
796
828
|
end
|
|
797
829
|
|
|
798
830
|
visit InterpolatedSymbolNode do |node|
|
|
799
831
|
push ":"
|
|
800
832
|
doubles do
|
|
801
|
-
visit_parts(node.parts) { |string|
|
|
833
|
+
visit_parts(node.parts) { |string| escape_string(string, '"') }
|
|
802
834
|
end
|
|
803
835
|
end
|
|
804
836
|
|
|
805
837
|
visit InterpolatedXStringNode do |node|
|
|
806
838
|
push "`"
|
|
807
|
-
visit_parts(node.parts) { |string|
|
|
839
|
+
visit_parts(node.parts) { |string| escape_string(string, "`") }
|
|
808
840
|
push "`"
|
|
809
841
|
end
|
|
810
842
|
|
|
@@ -829,9 +861,9 @@ module Refract
|
|
|
829
861
|
push "->"
|
|
830
862
|
|
|
831
863
|
case node.parameters
|
|
832
|
-
|
|
864
|
+
in nil
|
|
833
865
|
nil
|
|
834
|
-
|
|
866
|
+
in ItParametersNode | NumberedParametersNode
|
|
835
867
|
visit node.parameters
|
|
836
868
|
else
|
|
837
869
|
space
|
|
@@ -855,7 +887,7 @@ module Refract
|
|
|
855
887
|
visit LocalVariableAndWriteNode do |node|
|
|
856
888
|
push node.name
|
|
857
889
|
push " &&= "
|
|
858
|
-
|
|
890
|
+
operand node.value
|
|
859
891
|
end
|
|
860
892
|
|
|
861
893
|
visit LocalVariableOperatorWriteNode do |node|
|
|
@@ -863,13 +895,13 @@ module Refract
|
|
|
863
895
|
space
|
|
864
896
|
push node.binary_operator
|
|
865
897
|
push "= "
|
|
866
|
-
|
|
898
|
+
operand node.value
|
|
867
899
|
end
|
|
868
900
|
|
|
869
901
|
visit LocalVariableOrWriteNode do |node|
|
|
870
902
|
push node.name
|
|
871
903
|
push " ||= "
|
|
872
|
-
|
|
904
|
+
operand node.value
|
|
873
905
|
end
|
|
874
906
|
|
|
875
907
|
visit LocalVariableReadNode do |node|
|
|
@@ -883,36 +915,47 @@ module Refract
|
|
|
883
915
|
visit LocalVariableWriteNode do |node|
|
|
884
916
|
push node.name
|
|
885
917
|
push " = "
|
|
886
|
-
|
|
918
|
+
operand node.value
|
|
887
919
|
end
|
|
888
920
|
|
|
889
921
|
visit MatchLastLineNode do |node|
|
|
890
|
-
push
|
|
891
|
-
push
|
|
892
|
-
push "/"
|
|
893
|
-
regexp_flags(node)
|
|
922
|
+
regexp(node) { push escape_regexp(node.unescaped) }
|
|
923
|
+
push ".=~($_)" unless condition
|
|
894
924
|
end
|
|
895
925
|
|
|
896
926
|
visit MatchPredicateNode do |node|
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
927
|
+
parenthesize(endless?(node.pattern)) do
|
|
928
|
+
operand node.value
|
|
929
|
+
push " in "
|
|
930
|
+
visit node.pattern
|
|
931
|
+
end
|
|
900
932
|
end
|
|
901
933
|
|
|
902
934
|
visit MatchRequiredNode do |node|
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
935
|
+
parenthesize(endless?(node.pattern)) do
|
|
936
|
+
operand node.value
|
|
937
|
+
push " => "
|
|
938
|
+
visit node.pattern
|
|
939
|
+
end
|
|
906
940
|
end
|
|
907
941
|
|
|
908
942
|
visit MatchWriteNode do |node|
|
|
943
|
+
start = @buffer.length
|
|
909
944
|
visit node.call.receiver
|
|
945
|
+
names = Array(node.targets).map(&:name)
|
|
946
|
+
captures = Prism.parse("#{join(@buffer[start..])} =~ nil", scopes: [names], encoding: @encoding).value.statements.body.first
|
|
947
|
+
captured = (captures in Prism::MatchWriteNode[targets:]) ? targets.map(&:name) : []
|
|
948
|
+
|
|
949
|
+
unless names == captured
|
|
950
|
+
raise ArgumentError, "MatchWriteNode targets #{names} must match the regexp's named captures #{captured}."
|
|
951
|
+
end
|
|
952
|
+
|
|
910
953
|
push " =~ "
|
|
911
954
|
visit node.call.arguments
|
|
912
955
|
end
|
|
913
956
|
|
|
914
957
|
visit MissingNode do |node|
|
|
915
|
-
|
|
958
|
+
raise ArgumentError, "Can't format a MissingNode. The source it came from has a syntax error."
|
|
916
959
|
end
|
|
917
960
|
|
|
918
961
|
visit ModuleNode do |node|
|
|
@@ -929,16 +972,19 @@ module Refract
|
|
|
929
972
|
end
|
|
930
973
|
|
|
931
974
|
visit MultiTargetNode do |node|
|
|
932
|
-
|
|
933
|
-
|
|
975
|
+
targets = [*node.lefts, node.rest, *node.rights].compact
|
|
976
|
+
|
|
977
|
+
parenthesize(!(ForNode === @stack[-2]) || MultiTargetNode === targets.first) do
|
|
978
|
+
visit_each(targets) { push ", " }
|
|
934
979
|
end
|
|
935
980
|
end
|
|
936
981
|
|
|
937
982
|
visit MultiWriteNode do |node|
|
|
938
|
-
|
|
983
|
+
targets = [*node.lefts, node.rest, *node.rights].compact
|
|
984
|
+
parenthesize((targets in [MultiTargetNode])) { visit_each(targets) { push ", " } }
|
|
939
985
|
space unless ImplicitRestNode === node.rest
|
|
940
986
|
push "= "
|
|
941
|
-
|
|
987
|
+
operand node.value
|
|
942
988
|
end
|
|
943
989
|
|
|
944
990
|
visit NextNode do |node|
|
|
@@ -968,21 +1014,21 @@ module Refract
|
|
|
968
1014
|
visit OptionalKeywordParameterNode do |node|
|
|
969
1015
|
push node.name
|
|
970
1016
|
push ": "
|
|
971
|
-
|
|
1017
|
+
operand node.value
|
|
972
1018
|
end
|
|
973
1019
|
|
|
974
1020
|
visit OptionalParameterNode do |node|
|
|
975
1021
|
push node.name
|
|
976
1022
|
push " = "
|
|
977
|
-
|
|
1023
|
+
operand node.value
|
|
978
1024
|
end
|
|
979
1025
|
|
|
980
1026
|
visit OrNode do |node|
|
|
981
|
-
|
|
1027
|
+
operand node.left
|
|
982
1028
|
space
|
|
983
1029
|
push node.operator
|
|
984
1030
|
space
|
|
985
|
-
|
|
1031
|
+
operand node.right
|
|
986
1032
|
end
|
|
987
1033
|
|
|
988
1034
|
visit ParametersNode do |node|
|
|
@@ -1033,10 +1079,10 @@ module Refract
|
|
|
1033
1079
|
end
|
|
1034
1080
|
|
|
1035
1081
|
visit ProgramNode do |node|
|
|
1036
|
-
@
|
|
1082
|
+
@encoding = node.encoding if node.encoding
|
|
1037
1083
|
|
|
1038
|
-
if
|
|
1039
|
-
push "# encoding: #{
|
|
1084
|
+
if escape_non_ascii?
|
|
1085
|
+
push "# encoding: #{@encoding.name}"
|
|
1040
1086
|
new_line
|
|
1041
1087
|
end
|
|
1042
1088
|
|
|
@@ -1049,13 +1095,13 @@ module Refract
|
|
|
1049
1095
|
end
|
|
1050
1096
|
|
|
1051
1097
|
visit RangeNode do |node|
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1098
|
+
unconditional(condition(any_parser: true)) do
|
|
1099
|
+
(pattern? && !literal_ends?(node)) ? expression { range(node) } : range(node)
|
|
1100
|
+
end
|
|
1055
1101
|
end
|
|
1056
1102
|
|
|
1057
1103
|
visit RationalNode do |node|
|
|
1058
|
-
|
|
1104
|
+
rational(node, "r")
|
|
1059
1105
|
end
|
|
1060
1106
|
|
|
1061
1107
|
visit RedoNode do |node|
|
|
@@ -1063,10 +1109,9 @@ module Refract
|
|
|
1063
1109
|
end
|
|
1064
1110
|
|
|
1065
1111
|
visit RegularExpressionNode do |node|
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
regexp_flags(node)
|
|
1112
|
+
unconditional(condition(any_parser: true)) do
|
|
1113
|
+
regexp(node) { push escape_regexp(node.unescaped) }
|
|
1114
|
+
end
|
|
1070
1115
|
end
|
|
1071
1116
|
|
|
1072
1117
|
visit RequiredKeywordParameterNode do |node|
|
|
@@ -1079,16 +1124,16 @@ module Refract
|
|
|
1079
1124
|
end
|
|
1080
1125
|
|
|
1081
1126
|
visit RescueModifierNode do |node|
|
|
1082
|
-
|
|
1127
|
+
operand node.expression
|
|
1083
1128
|
push " rescue "
|
|
1084
|
-
|
|
1129
|
+
operand(node.rescue_expression, after_keyword: true)
|
|
1085
1130
|
end
|
|
1086
1131
|
|
|
1087
1132
|
visit RescueNode do |node|
|
|
1088
1133
|
push "rescue"
|
|
1089
1134
|
if node.exceptions&.any?
|
|
1090
1135
|
space
|
|
1091
|
-
|
|
1136
|
+
visit_operands(node.exceptions, after_keyword: true) { push ", " }
|
|
1092
1137
|
end
|
|
1093
1138
|
|
|
1094
1139
|
if node.reference
|
|
@@ -1130,16 +1175,12 @@ module Refract
|
|
|
1130
1175
|
end
|
|
1131
1176
|
|
|
1132
1177
|
visit ShareableConstantNode do |node|
|
|
1133
|
-
|
|
1134
|
-
shareable_constant_value(node.value) { visit node.write }
|
|
1135
|
-
else
|
|
1136
|
-
visit node.write
|
|
1137
|
-
end
|
|
1178
|
+
visit node.write
|
|
1138
1179
|
end
|
|
1139
1180
|
|
|
1140
1181
|
visit SingletonClassNode do |node|
|
|
1141
1182
|
push "class << "
|
|
1142
|
-
|
|
1183
|
+
operand node.expression
|
|
1143
1184
|
|
|
1144
1185
|
if node.body
|
|
1145
1186
|
indent do
|
|
@@ -1160,16 +1201,16 @@ module Refract
|
|
|
1160
1201
|
end
|
|
1161
1202
|
|
|
1162
1203
|
visit SourceLineNode do |node|
|
|
1163
|
-
push node.start_line&.to_s || "__LINE__"
|
|
1204
|
+
unconditional(condition(any_parser: true) == :flip_flop) { push node.start_line&.to_s || "__LINE__" }
|
|
1164
1205
|
end
|
|
1165
1206
|
|
|
1166
1207
|
visit SplatNode do |node|
|
|
1167
1208
|
push "*"
|
|
1168
|
-
|
|
1209
|
+
operand node.expression
|
|
1169
1210
|
end
|
|
1170
1211
|
|
|
1171
1212
|
visit StatementsNode do |node|
|
|
1172
|
-
visit_each(
|
|
1213
|
+
visit_each(each_statement(node.body).to_a) { new_line }
|
|
1173
1214
|
end
|
|
1174
1215
|
|
|
1175
1216
|
visit StringNode do |node|
|
|
@@ -1209,78 +1250,66 @@ module Refract
|
|
|
1209
1250
|
end
|
|
1210
1251
|
|
|
1211
1252
|
visit UnlessNode do |node|
|
|
1212
|
-
if node
|
|
1213
|
-
|
|
1214
|
-
push " unless "
|
|
1215
|
-
visit node.predicate
|
|
1253
|
+
if modifier?(node)
|
|
1254
|
+
modifier(node, "unless")
|
|
1216
1255
|
else
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
visit node.predicate
|
|
1220
|
-
|
|
1221
|
-
if node.statements
|
|
1222
|
-
indent do
|
|
1223
|
-
visit node.statements
|
|
1224
|
-
end
|
|
1225
|
-
end
|
|
1256
|
+
push "unless "
|
|
1257
|
+
operand node.predicate
|
|
1226
1258
|
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
visit node.
|
|
1259
|
+
if node.statements
|
|
1260
|
+
indent do
|
|
1261
|
+
visit node.statements
|
|
1230
1262
|
end
|
|
1263
|
+
end
|
|
1231
1264
|
|
|
1265
|
+
if node.else_clause
|
|
1232
1266
|
new_line
|
|
1233
|
-
|
|
1267
|
+
visit node.else_clause
|
|
1234
1268
|
end
|
|
1269
|
+
|
|
1270
|
+
new_line
|
|
1271
|
+
push "end"
|
|
1235
1272
|
end
|
|
1236
1273
|
end
|
|
1237
1274
|
|
|
1238
1275
|
visit UntilNode do |node|
|
|
1239
|
-
if node
|
|
1240
|
-
|
|
1241
|
-
push " until "
|
|
1242
|
-
visit node.predicate
|
|
1276
|
+
if modifier?(node)
|
|
1277
|
+
modifier(node, "until")
|
|
1243
1278
|
else
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
visit node.statements
|
|
1250
|
-
end
|
|
1279
|
+
push "until "
|
|
1280
|
+
operand node.predicate
|
|
1281
|
+
if node.statements
|
|
1282
|
+
indent do
|
|
1283
|
+
visit node.statements
|
|
1251
1284
|
end
|
|
1252
|
-
new_line
|
|
1253
|
-
push "end"
|
|
1254
1285
|
end
|
|
1286
|
+
new_line
|
|
1287
|
+
push "end"
|
|
1255
1288
|
end
|
|
1256
1289
|
end
|
|
1257
1290
|
|
|
1258
1291
|
visit WhenNode do |node|
|
|
1259
1292
|
push "when"
|
|
1260
1293
|
space
|
|
1261
|
-
|
|
1294
|
+
visit_operands(node.conditions) { push ", " }
|
|
1262
1295
|
indent do
|
|
1263
1296
|
visit node.statements
|
|
1264
1297
|
end
|
|
1265
1298
|
end
|
|
1266
1299
|
|
|
1267
1300
|
visit WhileNode do |node|
|
|
1268
|
-
if node
|
|
1269
|
-
|
|
1270
|
-
push " while "
|
|
1271
|
-
visit node.predicate
|
|
1301
|
+
if modifier?(node)
|
|
1302
|
+
modifier(node, "while")
|
|
1272
1303
|
else
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
visit node.statements
|
|
1279
|
-
end
|
|
1304
|
+
push "while "
|
|
1305
|
+
operand node.predicate
|
|
1306
|
+
if node.statements
|
|
1307
|
+
indent do
|
|
1308
|
+
visit node.statements
|
|
1280
1309
|
end
|
|
1281
|
-
new_line
|
|
1282
|
-
push "end"
|
|
1283
1310
|
end
|
|
1311
|
+
new_line
|
|
1312
|
+
push "end"
|
|
1284
1313
|
end
|
|
1285
1314
|
end
|
|
1286
1315
|
|
|
@@ -1301,16 +1330,27 @@ module Refract
|
|
|
1301
1330
|
|
|
1302
1331
|
private def push(value)
|
|
1303
1332
|
string = case value
|
|
1304
|
-
|
|
1333
|
+
in String
|
|
1305
1334
|
value
|
|
1306
|
-
|
|
1335
|
+
in Symbol
|
|
1307
1336
|
value.name
|
|
1337
|
+
else
|
|
1338
|
+
return
|
|
1308
1339
|
end
|
|
1309
1340
|
|
|
1310
|
-
@current_line += string.count("\n")
|
|
1341
|
+
@current_line += string.count("\n")
|
|
1311
1342
|
@buffer << string
|
|
1312
1343
|
end
|
|
1313
1344
|
|
|
1345
|
+
# Strings in other encodings, or with invalid bytes, are written as bytes.
|
|
1346
|
+
private def join(strings)
|
|
1347
|
+
strings.map(&:b).join.force_encoding(@encoding)
|
|
1348
|
+
end
|
|
1349
|
+
|
|
1350
|
+
private def escape_non_ascii?
|
|
1351
|
+
@encoding != Encoding::UTF_8
|
|
1352
|
+
end
|
|
1353
|
+
|
|
1314
1354
|
private def space
|
|
1315
1355
|
push " "
|
|
1316
1356
|
end
|
|
@@ -1328,21 +1368,21 @@ module Refract
|
|
|
1328
1368
|
nil
|
|
1329
1369
|
end
|
|
1330
1370
|
|
|
1331
|
-
private def outdent
|
|
1332
|
-
return yield if @indent == 0
|
|
1333
|
-
@indent -= 1
|
|
1334
|
-
new_line
|
|
1335
|
-
yield
|
|
1336
|
-
@indent += 1
|
|
1337
|
-
nil
|
|
1338
|
-
end
|
|
1339
|
-
|
|
1340
1371
|
private def parens
|
|
1341
1372
|
push "("
|
|
1342
1373
|
yield
|
|
1343
1374
|
push ")"
|
|
1344
1375
|
end
|
|
1345
1376
|
|
|
1377
|
+
private def multiline_parens(condition, &)
|
|
1378
|
+
return yield unless condition
|
|
1379
|
+
|
|
1380
|
+
parens do
|
|
1381
|
+
indent(&)
|
|
1382
|
+
new_line
|
|
1383
|
+
end
|
|
1384
|
+
end
|
|
1385
|
+
|
|
1346
1386
|
private def braces
|
|
1347
1387
|
push "{"
|
|
1348
1388
|
yield
|
|
@@ -1367,28 +1407,68 @@ module Refract
|
|
|
1367
1407
|
push '"'
|
|
1368
1408
|
end
|
|
1369
1409
|
|
|
1370
|
-
private def rational(
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
return "(#{numerator}r/#{denominator})" unless rest == 1
|
|
1410
|
+
private def rational(node, suffix)
|
|
1411
|
+
numerator, denominator = node.numerator, node.denominator
|
|
1412
|
+
places = decimal_places(denominator)
|
|
1413
|
+
return expression { push "#{numerator}#{suffix}/#{denominator}" } unless places
|
|
1375
1414
|
|
|
1376
|
-
places = [twos, fives].max
|
|
1377
1415
|
digits = (numerator.abs * (10 ** places) / denominator).to_s.rjust(places + 1, "0")
|
|
1378
1416
|
sign = numerator.negative? ? "-" : ""
|
|
1379
1417
|
|
|
1380
1418
|
if places.zero?
|
|
1381
|
-
"#{sign}#{digits}
|
|
1419
|
+
push "#{sign}#{digits}#{suffix}"
|
|
1420
|
+
else
|
|
1421
|
+
push "#{sign}#{digits[0...-places]}.#{digits[-places..]}#{suffix}"
|
|
1422
|
+
end
|
|
1423
|
+
end
|
|
1424
|
+
|
|
1425
|
+
# A value written as an expression rather than a literal, pinned in a pattern.
|
|
1426
|
+
private def expression(&)
|
|
1427
|
+
push "^" if pattern?
|
|
1428
|
+
parens(&)
|
|
1429
|
+
end
|
|
1430
|
+
|
|
1431
|
+
# A range in a pattern can only have literals at its ends, so is otherwise pinned whole.
|
|
1432
|
+
private def literal_ends?(range)
|
|
1433
|
+
literal?(range.left) && literal?(range.right)
|
|
1434
|
+
end
|
|
1435
|
+
|
|
1436
|
+
private def literal?(node)
|
|
1437
|
+
case node
|
|
1438
|
+
in FloatNode[value: Float => value]
|
|
1439
|
+
value.finite?
|
|
1440
|
+
in RationalNode
|
|
1441
|
+
!decimal_places(node.denominator).nil?
|
|
1442
|
+
in ImaginaryNode
|
|
1443
|
+
literal?(node.numeric)
|
|
1444
|
+
else
|
|
1445
|
+
true
|
|
1446
|
+
end
|
|
1447
|
+
end
|
|
1448
|
+
|
|
1449
|
+
# How many decimal places a fraction with this denominator needs, or nil if it repeats.
|
|
1450
|
+
private def decimal_places(denominator)
|
|
1451
|
+
rest, twos, fives = denominator, 0, 0
|
|
1452
|
+
(rest /= 2) && (twos += 1) while rest.even?
|
|
1453
|
+
(rest /= 5) && (fives += 1) while (rest % 5).zero?
|
|
1454
|
+
[twos, fives].max if rest == 1
|
|
1455
|
+
end
|
|
1456
|
+
|
|
1457
|
+
# `return a: 1` doesn't parse, though `return :a => 1` does.
|
|
1458
|
+
private def leading_jump_argument?
|
|
1459
|
+
case @stack[-4..-2]
|
|
1460
|
+
in [ReturnNode | BreakNode | NextNode, ArgumentsNode => arguments, KeywordHashNode => hash]
|
|
1461
|
+
arguments.arguments.first.equal?(hash)
|
|
1382
1462
|
else
|
|
1383
|
-
|
|
1463
|
+
false
|
|
1384
1464
|
end
|
|
1385
1465
|
end
|
|
1386
1466
|
|
|
1387
1467
|
private def shorthand?(key, value)
|
|
1388
1468
|
case value
|
|
1389
|
-
|
|
1469
|
+
in LocalVariableReadNode | LocalVariableTargetNode | ConstantReadNode
|
|
1390
1470
|
value.name.name == key.unescaped
|
|
1391
|
-
|
|
1471
|
+
in CallNode
|
|
1392
1472
|
value.name.name == key.unescaped && !value.receiver && !value.arguments && !value.block
|
|
1393
1473
|
else
|
|
1394
1474
|
false
|
|
@@ -1418,36 +1498,54 @@ module Refract
|
|
|
1418
1498
|
|
|
1419
1499
|
private def inline_statements?(parent)
|
|
1420
1500
|
case parent
|
|
1421
|
-
|
|
1422
|
-
parent.inline
|
|
1423
|
-
when ParenthesesNode, EmbeddedStatementsNode
|
|
1501
|
+
in ParenthesesNode | EmbeddedStatementsNode
|
|
1424
1502
|
true
|
|
1425
1503
|
else
|
|
1426
|
-
|
|
1504
|
+
modifier?(parent)
|
|
1427
1505
|
end
|
|
1428
1506
|
end
|
|
1429
1507
|
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
ConstantPathWriteNode, ConstantPathOrWriteNode, ConstantPathAndWriteNode, ConstantPathOperatorWriteNode
|
|
1508
|
+
# A statement takes the setting of its constant writes when they agree.
|
|
1509
|
+
# Otherwise each write that differs from the setting in force gets its own.
|
|
1510
|
+
private def shareable_constant_value_for(node)
|
|
1511
|
+
if statement?(node)
|
|
1512
|
+
values = shareable_constant_values_within(node, @stack[-2])
|
|
1436
1513
|
|
|
1437
|
-
|
|
1514
|
+
if values.one? && (values.first != :none || @shareable_constant_value != :none)
|
|
1515
|
+
return values.first
|
|
1516
|
+
end
|
|
1438
1517
|
end
|
|
1439
1518
|
|
|
1519
|
+
value = own_shareable_constant_value(node, @stack[-2])
|
|
1520
|
+
value unless value == @shareable_constant_value
|
|
1521
|
+
end
|
|
1522
|
+
|
|
1523
|
+
private def own_shareable_constant_value(node, parent)
|
|
1524
|
+
case node
|
|
1525
|
+
in ShareableConstantNode
|
|
1526
|
+
node.value
|
|
1527
|
+
in ConstantWriteNode | ConstantOrWriteNode | ConstantAndWriteNode | ConstantOperatorWriteNode |
|
|
1528
|
+
ConstantPathWriteNode | ConstantPathOrWriteNode | ConstantPathAndWriteNode | ConstantPathOperatorWriteNode
|
|
1529
|
+
:none unless ShareableConstantNode === parent
|
|
1530
|
+
else
|
|
1531
|
+
nil
|
|
1532
|
+
end
|
|
1533
|
+
end
|
|
1534
|
+
|
|
1535
|
+
private def shareable_constant_values_within(node, parent, values = Set.new)
|
|
1536
|
+
value = own_shareable_constant_value(node, parent)
|
|
1537
|
+
values << value if value
|
|
1538
|
+
|
|
1440
1539
|
node.class.attributes.each do |name|
|
|
1441
1540
|
Array(node.public_send(name)).each do |child|
|
|
1442
1541
|
next unless Node === child
|
|
1443
1542
|
next if StatementsNode === child && !inline_statements?(node)
|
|
1444
1543
|
|
|
1445
|
-
|
|
1446
|
-
return value if value
|
|
1544
|
+
shareable_constant_values_within(child, node, values)
|
|
1447
1545
|
end
|
|
1448
1546
|
end
|
|
1449
1547
|
|
|
1450
|
-
|
|
1548
|
+
values
|
|
1451
1549
|
end
|
|
1452
1550
|
|
|
1453
1551
|
private def shareable_constant_value(value)
|
|
@@ -1464,26 +1562,106 @@ module Refract
|
|
|
1464
1562
|
end
|
|
1465
1563
|
|
|
1466
1564
|
private def bare?(name, pattern)
|
|
1467
|
-
name.valid_encoding? && (name.ascii_only? ||
|
|
1565
|
+
name.valid_encoding? && (name.ascii_only? || !escape_non_ascii?) && name.match?(pattern)
|
|
1566
|
+
end
|
|
1567
|
+
|
|
1568
|
+
# Anything that binds more loosely than a method argument.
|
|
1569
|
+
private def compound?(node)
|
|
1570
|
+
case node
|
|
1571
|
+
in AndNode | OrNode | RescueModifierNode | RangeNode | FlipFlopNode | MatchPredicateNode | MatchRequiredNode | MatchWriteNode |
|
|
1572
|
+
MultiWriteNode | ShareableConstantNode | ReturnNode | BreakNode | NextNode | AliasMethodNode | AliasGlobalVariableNode | UndefNode
|
|
1573
|
+
|
|
1574
|
+
true
|
|
1575
|
+
in CallNode
|
|
1576
|
+
setter?(node) || index_write?(node)
|
|
1577
|
+
in nil
|
|
1578
|
+
false
|
|
1579
|
+
else
|
|
1580
|
+
modifier?(node) || node.type.end_with?("write_node")
|
|
1581
|
+
end
|
|
1468
1582
|
end
|
|
1469
1583
|
|
|
1470
|
-
private def
|
|
1584
|
+
private def modifier?(node)
|
|
1471
1585
|
case node
|
|
1472
|
-
|
|
1586
|
+
in IfNode[inline: true, statements: StatementsNode, subsequent: nil] | UnlessNode[inline: true, statements: StatementsNode, else_clause: nil]
|
|
1473
1587
|
true
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1588
|
+
in WhileNode[inline: true, statements: StatementsNode] | UntilNode[inline: true, statements: StatementsNode]
|
|
1589
|
+
true
|
|
1590
|
+
else
|
|
1591
|
+
false
|
|
1592
|
+
end
|
|
1593
|
+
end
|
|
1594
|
+
|
|
1595
|
+
private def setter?(node)
|
|
1596
|
+
node.name.match?(SETTER) && (node.receiver || node.arguments)
|
|
1597
|
+
end
|
|
1598
|
+
|
|
1599
|
+
private def index_write?(node)
|
|
1600
|
+
node.name == :[]= && node.attribute_write && node.block.nil? && node.arguments && !node.safe_navigation
|
|
1601
|
+
end
|
|
1602
|
+
|
|
1603
|
+
private def not?(node)
|
|
1604
|
+
node in CallNode[name: :!, receiver: Node, arguments: nil, block: nil, safe_navigation: false | nil]
|
|
1605
|
+
end
|
|
1606
|
+
|
|
1607
|
+
# Whether a node's source starts with `if`, `unless`, `while` or `until`,
|
|
1608
|
+
# which straight after `return`, `break`, `next` or `rescue` is a modifier.
|
|
1609
|
+
private def starts_with_condition?(node)
|
|
1610
|
+
case node
|
|
1611
|
+
in IfNode | UnlessNode | WhileNode | UntilNode
|
|
1612
|
+
!modifier?(node)
|
|
1613
|
+
in CallNode
|
|
1614
|
+
!not?(node) && starts_with_condition?(node.receiver)
|
|
1615
|
+
in ConstantPathNode
|
|
1616
|
+
starts_with_condition?(node.parent)
|
|
1617
|
+
else
|
|
1618
|
+
false
|
|
1619
|
+
end
|
|
1620
|
+
end
|
|
1621
|
+
|
|
1622
|
+
private def operand(node, after_keyword: false)
|
|
1623
|
+
if StatementsNode === node
|
|
1624
|
+
multiline_parens(true) { visit node }
|
|
1625
|
+
else
|
|
1626
|
+
parenthesize(compound?(node) || (after_keyword && starts_with_condition?(node))) { visit node }
|
|
1627
|
+
end
|
|
1628
|
+
end
|
|
1629
|
+
|
|
1630
|
+
private def receiver(node)
|
|
1631
|
+
not?(node) ? parens { visit node } : operand(node)
|
|
1632
|
+
end
|
|
1633
|
+
|
|
1634
|
+
private def visit_operands(nodes, after_keyword: false, &separator)
|
|
1635
|
+
separate(nodes, separator) { |node| operand(node, after_keyword:) }
|
|
1636
|
+
end
|
|
1637
|
+
|
|
1638
|
+
# A node that printed nothing gets no separator.
|
|
1639
|
+
private def separate(nodes, separator)
|
|
1640
|
+
nodes = Array(nodes).compact
|
|
1641
|
+
last = nodes.length - 1
|
|
1642
|
+
|
|
1643
|
+
nodes.each_with_index do |node, index|
|
|
1644
|
+
length = @buffer.length
|
|
1645
|
+
yield node
|
|
1646
|
+
separator&.call if index < last && @buffer.length > length
|
|
1647
|
+
end
|
|
1648
|
+
end
|
|
1649
|
+
|
|
1650
|
+
private def modifier(node, keyword)
|
|
1651
|
+
if each_statement(node.statements.body).one?
|
|
1652
|
+
visit node.statements
|
|
1478
1653
|
else
|
|
1479
|
-
node.
|
|
1654
|
+
multiline_parens(true) { visit node.statements }
|
|
1480
1655
|
end
|
|
1656
|
+
|
|
1657
|
+
push " #{keyword} "
|
|
1658
|
+
operand node.predicate
|
|
1481
1659
|
end
|
|
1482
1660
|
|
|
1483
1661
|
private def call_receiver(node)
|
|
1484
1662
|
return unless node.receiver
|
|
1485
1663
|
|
|
1486
|
-
|
|
1664
|
+
receiver node.receiver
|
|
1487
1665
|
push "&" if node.safe_navigation
|
|
1488
1666
|
push "."
|
|
1489
1667
|
end
|
|
@@ -1492,23 +1670,146 @@ module Refract
|
|
|
1492
1670
|
condition ? parens(&) : yield
|
|
1493
1671
|
end
|
|
1494
1672
|
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1673
|
+
private def pinned(condition, &)
|
|
1674
|
+
return yield unless condition
|
|
1675
|
+
|
|
1676
|
+
push "^"
|
|
1677
|
+
parens(&)
|
|
1678
|
+
end
|
|
1679
|
+
|
|
1680
|
+
# Every Ruby reads `(x if true)` as a value, even where a range would be a
|
|
1681
|
+
# flip-flop or a regexp a match against `$_`.
|
|
1682
|
+
private def unconditional(condition)
|
|
1683
|
+
return yield unless condition
|
|
1684
|
+
|
|
1685
|
+
parens do
|
|
1686
|
+
yield
|
|
1687
|
+
push " if true"
|
|
1688
|
+
end
|
|
1689
|
+
end
|
|
1690
|
+
|
|
1691
|
+
# Where Ruby reads a range as a flip-flop and a regexp as a match against
|
|
1692
|
+
# `$_`, with every parser or, given `any_parser`, with at least one. At the
|
|
1693
|
+
# ends of a flip-flop, an integer is compared with `$.`. Unlike Prism,
|
|
1694
|
+
# Ruby 3.3's parser reads the last of several statements as a condition, but
|
|
1695
|
+
# not the body of a `begin` with `rescue` or `ensure`.
|
|
1696
|
+
private def condition(index = @stack.length - 1, any_parser: false)
|
|
1697
|
+
return if index < 1
|
|
1698
|
+
|
|
1699
|
+
node, parent = @stack[index], @stack[index - 1]
|
|
1700
|
+
|
|
1701
|
+
case parent
|
|
1702
|
+
in IfNode | UnlessNode | WhileNode | UntilNode
|
|
1703
|
+
:condition if parent.predicate.equal?(node)
|
|
1704
|
+
in CallNode
|
|
1705
|
+
:condition if not?(parent)
|
|
1706
|
+
in AndNode | OrNode
|
|
1707
|
+
:condition if condition(index - 1, any_parser:)
|
|
1708
|
+
in FlipFlopNode
|
|
1709
|
+
:flip_flop
|
|
1710
|
+
in ParenthesesNode
|
|
1711
|
+
condition(index - 1, any_parser:)
|
|
1712
|
+
in BeginNode
|
|
1713
|
+
if parent.statements.equal?(node) && (any_parser || (parent in BeginNode[rescue_clause: nil, else_clause: nil, ensure_clause: nil]))
|
|
1714
|
+
condition(index - 1, any_parser:)
|
|
1715
|
+
end
|
|
1716
|
+
in StatementsNode
|
|
1717
|
+
if each_statement(parent.body).one? || (any_parser && last_statement(node).equal?(last_statement(parent.body)))
|
|
1718
|
+
condition(index - 1, any_parser:)
|
|
1719
|
+
end
|
|
1720
|
+
else
|
|
1721
|
+
nil
|
|
1722
|
+
end
|
|
1723
|
+
end
|
|
1724
|
+
|
|
1725
|
+
private def pattern?(index = @stack.length - 1)
|
|
1726
|
+
return false if index < 1
|
|
1727
|
+
|
|
1728
|
+
node, parent = @stack[index], @stack[index - 1]
|
|
1729
|
+
|
|
1730
|
+
case parent
|
|
1731
|
+
in InNode | MatchPredicateNode | MatchRequiredNode
|
|
1732
|
+
parent.pattern.equal?(node)
|
|
1733
|
+
in ArrayPatternNode | FindPatternNode | AlternationPatternNode
|
|
1500
1734
|
true
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1735
|
+
in CapturePatternNode
|
|
1736
|
+
parent.value.equal?(node)
|
|
1737
|
+
in AssocNode
|
|
1738
|
+
HashPatternNode === @stack[index - 2] && parent.value.equal?(node)
|
|
1739
|
+
in ParenthesesNode
|
|
1740
|
+
pattern?(index - 1)
|
|
1741
|
+
in RangeNode
|
|
1742
|
+
literal_ends?(parent) && pattern?(index - 1)
|
|
1743
|
+
in StatementsNode
|
|
1744
|
+
each_statement(parent.body).one? && pattern?(index - 1)
|
|
1745
|
+
in IfNode | UnlessNode
|
|
1746
|
+
parent.statements.equal?(node) && pattern?(index - 1)
|
|
1747
|
+
else
|
|
1748
|
+
false
|
|
1749
|
+
end
|
|
1750
|
+
end
|
|
1751
|
+
|
|
1752
|
+
private def endless?(pattern)
|
|
1753
|
+
case pattern
|
|
1754
|
+
in RangeNode
|
|
1755
|
+
pattern.right.nil?
|
|
1756
|
+
in AlternationPatternNode
|
|
1757
|
+
endless?(pattern.right)
|
|
1758
|
+
in StatementsNode
|
|
1759
|
+
(each_statement(pattern.body).first(2) in [statement]) && endless?(statement)
|
|
1505
1760
|
else
|
|
1506
1761
|
false
|
|
1507
1762
|
end
|
|
1508
1763
|
end
|
|
1509
1764
|
|
|
1765
|
+
private def range(node)
|
|
1766
|
+
parenthesize(node.right.nil? && !pattern?) do
|
|
1767
|
+
operand node.left
|
|
1768
|
+
push node.exclude_end ? "..." : ".."
|
|
1769
|
+
operand node.right
|
|
1770
|
+
end
|
|
1771
|
+
end
|
|
1772
|
+
|
|
1773
|
+
private def word_list_prefix(elements)
|
|
1774
|
+
if elements.all? { |element| word?(element, StringNode, InterpolatedStringNode) }
|
|
1775
|
+
"%W["
|
|
1776
|
+
elsif elements.all? { |element| word?(element, SymbolNode, InterpolatedSymbolNode) }
|
|
1777
|
+
"%I["
|
|
1778
|
+
end
|
|
1779
|
+
end
|
|
1780
|
+
|
|
1781
|
+
private def word?(node, plain, interpolated)
|
|
1782
|
+
case node
|
|
1783
|
+
in ^plain
|
|
1784
|
+
!node.unescaped.empty?
|
|
1785
|
+
in ^interpolated
|
|
1786
|
+
flatten_parts(node.parts).any? { |part| !(part in StringNode[unescaped: ""]) }
|
|
1787
|
+
else
|
|
1788
|
+
false
|
|
1789
|
+
end
|
|
1790
|
+
end
|
|
1791
|
+
|
|
1792
|
+
private def word_list(prefix, elements)
|
|
1793
|
+
push prefix
|
|
1794
|
+
|
|
1795
|
+
elements.each_with_index do |element, index|
|
|
1796
|
+
space unless index.zero?
|
|
1797
|
+
|
|
1798
|
+
map_source_and_visit(element) do
|
|
1799
|
+
case element
|
|
1800
|
+
in StringNode | SymbolNode
|
|
1801
|
+
push escape_string(element.unescaped, "]")
|
|
1802
|
+
else
|
|
1803
|
+
visit_parts(element.parts) { |string| escape_string(string, "]") }
|
|
1804
|
+
end
|
|
1805
|
+
end
|
|
1806
|
+
end
|
|
1807
|
+
|
|
1808
|
+
push "]"
|
|
1809
|
+
end
|
|
1810
|
+
|
|
1510
1811
|
private def quote(string)
|
|
1511
|
-
if string.include?('"') && string.valid_encoding? && (string.ascii_only? ||
|
|
1812
|
+
if string.include?('"') && string.valid_encoding? && (string.ascii_only? || !escape_non_ascii?) && !string.match?(/['\\]|[^[:print:]]/)
|
|
1512
1813
|
"'#{string}'"
|
|
1513
1814
|
else
|
|
1514
1815
|
%("#{escape_string(string, '"')}")
|
|
@@ -1516,30 +1817,50 @@ module Refract
|
|
|
1516
1817
|
end
|
|
1517
1818
|
|
|
1518
1819
|
private def escape_string(string, delimiter)
|
|
1519
|
-
string = string.b unless string.encoding == Encoding::UTF_8 && string.valid_encoding?
|
|
1520
|
-
|
|
1521
1820
|
pattern = ESCAPE_PATTERNS.fetch(delimiter)
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
"\\#{char}"
|
|
1529
|
-
elsif char.bytesize == 1
|
|
1530
|
-
format("\\x%02X", char.ord)
|
|
1531
|
-
else
|
|
1532
|
-
format("\\u{%X}", char.ord)
|
|
1533
|
-
end
|
|
1821
|
+
|
|
1822
|
+
# Bytes that aren't valid UTF-8 can't be written as they are, and Rubies
|
|
1823
|
+
# disagree on whether a high byte is printable, so all are escaped.
|
|
1824
|
+
unless string.encoding == Encoding::UTF_8 && string.valid_encoding?
|
|
1825
|
+
string = string.b
|
|
1826
|
+
pattern = Regexp.union(pattern, NON_ASCII)
|
|
1534
1827
|
end
|
|
1828
|
+
|
|
1829
|
+
pattern = Regexp.union(pattern, NON_ASCII) if escape_non_ascii?
|
|
1830
|
+
|
|
1831
|
+
string.gsub(pattern) { |char| escape_character(char) }
|
|
1535
1832
|
end
|
|
1536
1833
|
|
|
1834
|
+
private def escape_character(char)
|
|
1835
|
+
if (escape = STRING_ESCAPES[char])
|
|
1836
|
+
escape
|
|
1837
|
+
elsif char.bytesize > 1
|
|
1838
|
+
format("\\u{%X}", char.ord)
|
|
1839
|
+
elsif char.ascii_only? && char.match?(/[[:print:]]/)
|
|
1840
|
+
"\\#{char}"
|
|
1841
|
+
else
|
|
1842
|
+
format("\\x%02X", char.ord)
|
|
1843
|
+
end
|
|
1844
|
+
end
|
|
1845
|
+
|
|
1846
|
+
# Ruby drops a line continuation from a regexp's source, so it can stop a `#`
|
|
1847
|
+
# starting interpolation without changing the pattern, whatever the mode.
|
|
1537
1848
|
private def escape_regexp(string)
|
|
1538
1849
|
string = string.b unless string.valid_encoding?
|
|
1539
|
-
|
|
1850
|
+
|
|
1851
|
+
string.gsub(%r{\\.|/|#{REGEXP_INTERPOLATION}}m) do |match|
|
|
1852
|
+
case match
|
|
1853
|
+
in "/" then "\\/"
|
|
1854
|
+
in "#" then "#\\\n"
|
|
1855
|
+
else match
|
|
1856
|
+
end
|
|
1857
|
+
end
|
|
1540
1858
|
end
|
|
1541
1859
|
|
|
1542
|
-
private def
|
|
1860
|
+
private def regexp(node)
|
|
1861
|
+
push "/"
|
|
1862
|
+
yield
|
|
1863
|
+
push "/"
|
|
1543
1864
|
push "i" if node.ignore_case
|
|
1544
1865
|
push "m" if node.multi_line
|
|
1545
1866
|
push "x" if node.extended
|
|
@@ -1547,24 +1868,56 @@ module Refract
|
|
|
1547
1868
|
push REGEXP_ENCODINGS.fetch(node.encoding) if node.encoding
|
|
1548
1869
|
end
|
|
1549
1870
|
|
|
1871
|
+
# Yields each run of text to be escaped. An embedded variable followed by
|
|
1872
|
+
# escaped text that would extend its name is written in braces.
|
|
1550
1873
|
private def visit_parts(parts)
|
|
1551
|
-
flatten_parts(parts)
|
|
1874
|
+
chunks = flatten_parts(parts)
|
|
1552
1875
|
.chunk_while { |a, b| StringNode === a && StringNode === b }
|
|
1553
|
-
.
|
|
1554
|
-
|
|
1555
|
-
|
|
1556
|
-
|
|
1557
|
-
|
|
1558
|
-
|
|
1876
|
+
.map { |chunk| (StringNode === chunk.first) ? yield(chunk.map(&:unescaped).join) : chunk.first }
|
|
1877
|
+
|
|
1878
|
+
chunks.each_with_index do |chunk, index|
|
|
1879
|
+
case chunk
|
|
1880
|
+
in String
|
|
1881
|
+
push chunk
|
|
1882
|
+
in EmbeddedVariableNode if extended_by?(chunk.variable, chunks[index + 1])
|
|
1883
|
+
visit EmbeddedStatementsNode.new(prism_node: chunk.prism_node, statements: StatementsNode.new(body: [chunk.variable]))
|
|
1884
|
+
else
|
|
1885
|
+
visit chunk
|
|
1559
1886
|
end
|
|
1887
|
+
end
|
|
1560
1888
|
end
|
|
1561
1889
|
|
|
1562
|
-
|
|
1563
|
-
|
|
1890
|
+
# Punctuation variables like `$&` and `$-w` have a fixed length, and `$1` only takes digits.
|
|
1891
|
+
private def extended_by?(variable, text)
|
|
1892
|
+
case variable
|
|
1893
|
+
in NumberedReferenceReadNode
|
|
1894
|
+
/\A\d/ === text
|
|
1895
|
+
in BackReferenceReadNode
|
|
1896
|
+
false
|
|
1897
|
+
else
|
|
1898
|
+
!variable.name.start_with?("$-") && variable.name.match?(/(?:\w|#{NON_ASCII})\z/) && /\A(?:\w|#{NON_ASCII})/ === text
|
|
1899
|
+
end
|
|
1900
|
+
end
|
|
1901
|
+
|
|
1902
|
+
private def each_statement(body, &)
|
|
1903
|
+
return enum_for(__method__, body) unless block_given?
|
|
1904
|
+
|
|
1905
|
+
Array(body).each do |statement|
|
|
1906
|
+
(StatementsNode === statement) ? each_statement(statement.body, &) : yield(statement)
|
|
1907
|
+
end
|
|
1908
|
+
end
|
|
1909
|
+
|
|
1910
|
+
private def last_statement(body)
|
|
1911
|
+
Array(body).reverse_each do |statement|
|
|
1912
|
+
last = (StatementsNode === statement) ? last_statement(statement.body) : statement
|
|
1913
|
+
return last if last
|
|
1914
|
+
end
|
|
1915
|
+
|
|
1916
|
+
nil
|
|
1564
1917
|
end
|
|
1565
1918
|
|
|
1566
1919
|
private def flatten_parts(parts)
|
|
1567
|
-
parts.flat_map { |part| (InterpolatedStringNode === part) ? flatten_parts(part.parts) : part }
|
|
1920
|
+
Array(parts).flat_map { |part| (InterpolatedStringNode === part) ? flatten_parts(part.parts) : part }
|
|
1568
1921
|
end
|
|
1569
1922
|
end
|
|
1570
1923
|
end
|