Class: Sirop::Sourcifier

Inherits:
Prism::BasicVisitor
  • Object
show all
Defined in:
lib/sirop/sourcifier.rb

Constant Summary collapse

VISIT_PLANS =
{
  alias_global_variable:      [:keyword_loc, :new_name, :old_name],
  alias_method:               [:keyword_loc, :new_name, :old_name],
  and:                        [:left, :operator_loc, :right],
  assoc:                      [:key, :operator_loc, :value],
  assoc_splat:                [:operator_loc, :value],
  back_reference_read:        :emit_verbatim,
  block:                      [:opening_loc, :parameters, :body, :closing_loc],
  block_argument:             [:operator_loc, :expression],
  block_local_variable:       :emit_verbatim,
  block_parameter:            [:operator_loc, :name_loc],
  break:                      [:keyword_loc, :arguments],
  capture_pattern:            [:value, :operator_loc, :target],
  class_variable_read:        :emit_verbatim,
  class_variable_target:      :emit_verbatim,
  class_variable_write:       [:name_loc, :operator_loc, :value],
  constant_path:              [:parent, :delimiter_loc, :name_loc],
  constant_path_write:        [:target, :operator_loc, :value],
  constant_read:              :emit_verbatim,
  constant_write:             [:name_loc, :operator_loc, :value],
  defined:                    [:keyword_loc, :lparen_loc, :value, :rparen_loc],
  embedded_statements:        [:opening_loc, :statements, :closing_loc],
  embedded_variable:          [:operator_loc, :variable],
  false:                      :emit_verbatim,
  flip_flop:                  [:left, :operator_loc, :right],
  float:                      :emit_verbatim,
  forwarding_arguments:       :emit_verbatim,
  forwarding_parameter:       :emit_verbatim,
  forwarding_super:           :emit_verbatim,
  global_variable_read:       :emit_verbatim,
  global_variable_target:     :emit_verbatim,
  global_variable_write:      [:name_loc, :operator_loc, :value],
  imaginary:                  :emit_verbatim,
  implicit:                   :emit_nothing,
  implicit_rest:              :emit_nothing,
  in:                         [:in_loc, :pattern, :then_loc],
  index_target:               [:receiver, :opening_loc, :arguments, :closing_loc],
  instance_variable_read:     :emit_verbatim,
  instance_variable_target:   :emit_verbatim,
  instance_variable_write:    [:name_loc, :operator_loc, :value],
  integer:                    :emit_verbatim,
  it_parameters:              :emit_nothing,
  it_local_variable_read:     :emit_verbatim,
  keyword_rest_parameter:     [:operator_loc, :name_loc],
  keyword_parameter:          :emit_verbatim,
  local_variable_and_write:   [:name_loc, :operator_loc, :value],
  local_variable_operator_write: [:name_loc, :binary_operator_loc, :value],
  local_variable_or_write:    [:name_loc, :operator_loc, :value],
  local_variable_read:        :emit_verbatim,
  local_variable_target:      :emit_verbatim,
  local_variable_write:       [:name_loc, :operator_loc, :value],
  match_predicate:            [:value, :operator_loc, :pattern],
  match_required:             [:value, :operator_loc, :pattern],
  match_write:                [:call],
  next:                       [:keyword_loc, :arguments],
  nil:                        :emit_verbatim,
  no_keywords_parameter:      :emit_verbatim,
  numbered_parameters:        :emit_nothing,
  optional_parameter:         [:name_loc, :operator_loc, :value],
  optional_keyword_parameter: [:name_loc, :value],
  or:                         [:left, :operator_loc, :right],
  parentheses:                [:opening_loc, :body, :closing_loc],
  pinned_expression:          [:operator_loc, :lparen_loc, :expression, :rparen_loc],
  pinned_variable:            [:operator_loc, :variable],
  range:                      [:left, :operator_loc, :right],
  rational:                   :emit_verbatim,
  redo:                       :emit_verbatim,
  regular_expression:         :emit_verbatim,
  required_parameter:         :emit_verbatim,
  required_keyword_parameter: :emit_verbatim,
  rescue_modifier:            [:expression, :keyword_loc, :rescue_expression],
  rest_parameter:             [:operator_loc, :name_loc],
  retry:                      :emit_verbatim,
  return:                     [:keyword_loc, :arguments],
  self:                       :emit_verbatim,
  source_encoding:            :emit_verbatim,
  source_file:                :emit_verbatim,
  source_line:                :emit_verbatim,
  splat:                      [:operator_loc, :expression],
  string:                     [:opening_loc, :content_loc, :closing_loc],
  symbol:                     :emit_verbatim,
  true:                       :emit_verbatim,
  x_string:                   :emit_verbatim,
  yield:                      [:keyword_loc, :lparen_loc, :arguments, :rparen_loc]
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(minimize_whitespace: false) ⇒ Sourcifier

Returns a new instance of Sourcifier.



96
97
98
99
# File 'lib/sirop/sourcifier.rb', line 96

def initialize(minimize_whitespace: false)
  @buffer = +''
  @minimize_whitespace = minimize_whitespace
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, node, *args) ⇒ Object

Raises:

  • (NotImplementedError)


206
207
208
209
210
211
# File 'lib/sirop/sourcifier.rb', line 206

def method_missing(sym, node, *args)
  puts '!' * 40
  p node
  raise NotImplementedError, "Don't know how to handle #{sym}"
  visit_child_nodes(node)
end

Instance Attribute Details

#bufferObject (readonly)

Returns the value of attribute buffer.



94
95
96
# File 'lib/sirop/sourcifier.rb', line 94

def buffer
  @buffer
end

Instance Method Details

#adjust_whitespace(loc, advance_to_end: true) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/sirop/sourcifier.rb', line 137

def adjust_whitespace(loc, advance_to_end: true)
  return if loc.is_a?(Sirop::Injection)

  if @last_loc_start
    line_diff = loc.start_line - @last_loc_end.first
    if line_diff > 0
      @buffer << "\n" * line_diff
      @buffer << ' ' * loc.start_column if !@minimize_whitespace
    elsif line_diff == 0
      ofs = loc.start_column - @last_loc_end.last
      if ofs > 0
        @buffer << (@minimize_whitespace ? ' ' : (' ' * ofs))
      end
    end
  else
    # empty buffer
    @buffer << ' ' * loc.start_column if !@minimize_whitespace
  end
  @last_loc = loc
  @last_loc_start = loc_start(loc)
  @last_loc_end = advance_to_end ? loc_end(loc) : @last_loc_start
end

#emit(str) ⇒ Object



124
125
126
127
# File 'lib/sirop/sourcifier.rb', line 124

def emit(str)
  update_source_map(str)
  @buffer << str
end

#emit_code(loc, semicolon: false, chomp: false) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/sirop/sourcifier.rb', line 160

def emit_code(loc, semicolon: false, chomp: false)
  return if !loc

  if @last_loc
    loc_loc = loc.is_a?(Prism::Node) ? loc.location : loc
    return if loc_loc.slice == @last_loc.slice && loc_loc.start_line == @last_loc.start_line &&
      loc_loc.start_column == @last_loc.start_column
  end

  semicolon ||= @semicolon
  @semicolon = false
  emit_semicolon(loc) if semicolon
  return visit(loc) if loc.is_a?(Prism::Node)

  adjust_whitespace(loc)
  str = loc.slice
  str = str.chomp if chomp
  emit(str)
end

#emit_commaObject



193
194
195
# File 'lib/sirop/sourcifier.rb', line 193

def emit_comma
  emit_str(',')
end

#emit_nothing(node) ⇒ Object



184
185
186
# File 'lib/sirop/sourcifier.rb', line 184

def emit_nothing(node)
  # emit nothing
end

#emit_semicolon(loc) ⇒ Object



197
198
199
200
201
202
203
204
# File 'lib/sirop/sourcifier.rb', line 197

def emit_semicolon(loc)
  loc = loc.location if loc.is_a?(Prism::Node)
  if @last_loc && loc.start_line == @last_loc.end_line
    if @buffer[-1] != ';' && loc.start_column > @last_loc_end[1]
      emit_str(';')
    end
  end
end

#emit_str(str) ⇒ Object



188
189
190
191
# File 'lib/sirop/sourcifier.rb', line 188

def emit_str(str)
  emit(str)
  @last_loc_end[1] += str.size
end

#emit_verbatim(node) ⇒ Object



180
181
182
# File 'lib/sirop/sourcifier.rb', line 180

def emit_verbatim(node)
  emit_code(node.location)
end

#loc_end(loc) ⇒ Object



120
121
122
# File 'lib/sirop/sourcifier.rb', line 120

def loc_end(loc)
  [loc.end_line, loc.end_column]
end

#loc_start(loc) ⇒ Object



116
117
118
# File 'lib/sirop/sourcifier.rb', line 116

def loc_start(loc)
  [loc.start_line, loc.start_column]
end

#to_source(node) ⇒ Object



101
102
103
104
105
106
# File 'lib/sirop/sourcifier.rb', line 101

def to_source(node)
  @buffer.clear
  @source_map = nil
  visit(node)
  @buffer
end

#to_source_with_source_map(obj, node, line_ofs) ⇒ Object



108
109
110
111
112
113
114
# File 'lib/sirop/sourcifier.rb', line 108

def to_source_with_source_map(obj, node, line_ofs)
  @buffer.clear
  @source_map_line_ofs = line_ofs
  @source_map = { source_fn: obj.source_location.first }
  visit(node)
  [@buffer, @source_map]
end

#update_source_map(str = nil) ⇒ Object



129
130
131
132
133
134
135
# File 'lib/sirop/sourcifier.rb', line 129

def update_source_map(str = nil)
  return if !@source_map

  buffer_cur_line = @buffer.count("\n") + 1
  orig_source_cur_line = @last_loc_start ? @last_loc_start.first : 1
  @source_map[buffer_cur_line + @source_map_line_ofs] ||= orig_source_cur_line
end

#visit_arguments_node(node, subscript = 0..-1)) ⇒ Object



291
292
293
# File 'lib/sirop/sourcifier.rb', line 291

def visit_arguments_node(node, subscript = 0..-1)
  visit_comma_separated_nodes(node.arguments[subscript])
end

#visit_array_node(node) ⇒ Object



486
487
488
489
490
491
492
493
494
# File 'lib/sirop/sourcifier.rb', line 486

def visit_array_node(node)
  emit_code(node.opening_loc)
  if node.opening_loc && node.opening_loc.slice =~ /^%/
    visit_space_separated_nodes(node.elements)
  else
    visit_comma_separated_nodes(node.elements)
  end
  emit_code(node.closing_loc)
end

#visit_array_pattern_node(node) ⇒ Object



629
630
631
632
633
634
635
636
637
638
639
640
# File 'lib/sirop/sourcifier.rb', line 629

def visit_array_pattern_node(node)
  emit_code(node.constant)
  emit_code(node.opening_loc)
  comma = visit_comma_separated_nodes(node.requireds, comma)
  if node.rest
    emit_comma if comma
    emit_code(node.rest)
    comma = true
  end
  visit_comma_separated_nodes(node.posts, comma)
  emit_code(node.closing_loc)
end

#visit_begin_node(node) ⇒ Object



519
520
521
522
523
524
525
526
# File 'lib/sirop/sourcifier.rb', line 519

def visit_begin_node(node)
  emit_code(node.begin_keyword_loc) #, semicolon: true)
  emit_code(node.statements, semicolon: true)
  emit_code(node.rescue_clause)
  emit_code(node.else_clause)
  emit_code(node.ensure_clause)
  emit_code(node.end_keyword_loc, semicolon: true) if node.begin_keyword_loc
end

#visit_block_parameters_node(node) ⇒ Object



658
659
660
661
662
663
664
665
666
# File 'lib/sirop/sourcifier.rb', line 658

def visit_block_parameters_node(node)
  emit_code(node.opening_loc)
  emit_code(node.injected_parameters)
  emit_code(node.parameters)
  @semicolon = true if node.parameters
  visit_comma_separated_nodes(node.locals)
  emit_code(node.closing_loc)
  @semicolon = false
end

#visit_call_node(node) ⇒ Object



385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
# File 'lib/sirop/sourcifier.rb', line 385

def visit_call_node(node)
  if node.receiver && !node.call_operator_loc && !node.arguments && node.name != :[]
    return visit_call_node_unary_op(node)
  end

  if node.receiver && !node.call_operator_loc && node.name == :!
    return visit_call_node_unary_op(node)
  end

  if node.attribute_write?
    return visit_call_node_attribute_write(node)
  end

  block = node.block

  emit_code(node.receiver)
  emit_code(node.call_operator_loc)
  if (ml = node.message_loc)
    ol = node.opening_loc
    emit_message_loc = !ol || (ol.start_line != ml.start_line) || (ol.start_column != ml.start_column)
    emit_code(node.message_loc) if emit_message_loc
  end
  emit_code(node.opening_loc)
  emit_code(node.arguments)

  if block.is_a?(Prism::BlockArgumentNode)
    emit_comma if node.arguments && node.arguments.arguments.size > 0
    emit_code(block)
    block = nil
  end
  emit_code(node.closing_loc)
  emit_code(block)
end

#visit_call_node_attribute_write(node) ⇒ Object



426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
# File 'lib/sirop/sourcifier.rb', line 426

def visit_call_node_attribute_write(node)
  emit_code(node.receiver)
  if node.call_operator_loc
    emit_code(node.call_operator_loc)
    emit_code(node.message_loc)
  end
  emit_code(node.opening_loc)
  comma = visit_arguments_node(node.arguments, 0..-2)
  if node.block
    emit_comma if comma
    emit_code(node.block)
  end
  emit_code(node.closing_loc)
  emit_str(" = ")
  emit_code(node.arguments.arguments[-1])
  return
end

#visit_call_node_unary_op(node) ⇒ Object



419
420
421
422
423
424
# File 'lib/sirop/sourcifier.rb', line 419

def visit_call_node_unary_op(node)
  emit_code(node.message_loc)
  emit_code(node.opening_loc)
  emit_code(node.receiver)
  emit_code(node.closing_loc)
end

#visit_call_target_node(node) ⇒ Object



444
445
446
447
448
# File 'lib/sirop/sourcifier.rb', line 444

def visit_call_target_node(node)
  emit_code(node.receiver)
  emit_code(node.call_operator_loc)
  emit_code(node.message_loc)
end

#visit_case_match_node(node) ⇒ Object



556
557
558
559
560
561
562
# File 'lib/sirop/sourcifier.rb', line 556

def visit_case_match_node(node)
  emit_code(node.case_keyword_loc)
  emit_code(node.predicate)
  @semicolon = true
  visit_comma_separated_nodes(node.conditions)
  emit_code(node.end_keyword_loc)
end

#visit_case_node(node) ⇒ Object



347
348
349
350
351
352
353
# File 'lib/sirop/sourcifier.rb', line 347

def visit_case_node(node)
  emit_code(node.case_keyword_loc)
  emit_code(node.predicate)
  node.conditions.each { |c| emit_code(c, semicolon: true) }
  emit_code(node.else_clause, semicolon: true)
  emit_code(node.end_keyword_loc, semicolon: true)
end

#visit_class_node(node) ⇒ Object



564
565
566
567
568
569
570
571
# File 'lib/sirop/sourcifier.rb', line 564

def visit_class_node(node)
  emit_code(node.class_keyword_loc)
  emit_code(node.constant_path)
  emit_code(node.inheritance_operator_loc)
  emit_code(node.superclass)
  emit_code(node.body, semicolon: true)
  emit_code(node.end_keyword_loc, semicolon: true)
end

#visit_comma_separated_nodes(list, comma = false) ⇒ Object



234
235
236
237
238
239
240
241
242
243
# File 'lib/sirop/sourcifier.rb', line 234

def visit_comma_separated_nodes(list, comma = false)
  if list
    list.each do |child|
      emit_comma if comma
      emit_code(child)
      comma = true
    end
  end
  comma
end

#visit_def_node(node) ⇒ Object



368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# File 'lib/sirop/sourcifier.rb', line 368

def visit_def_node(node)
  emit_code(node.def_keyword_loc, semicolon: true)
  emit_code(node.receiver)
  emit_code(node.operator_loc)
  emit_code(node.name_loc)

  emit_code(node.lparen_loc)
  if node.parameters
    emit_code(node.parameters)
  end
  emit_code(node.rparen_loc)
  emit_code(node.equal_loc)
  emit_code(node.body, semicolon: !node.equal_loc)
  emit_code(node.end_keyword_loc, semicolon: true)
  @semicolon = true
end

#visit_else_node(node) ⇒ Object



551
552
553
554
# File 'lib/sirop/sourcifier.rb', line 551

def visit_else_node(node)
  emit_code(node.else_keyword_loc, semicolon: node.else_keyword_loc.slice == 'else')
  emit_code(node.statements, semicolon: node.else_keyword_loc.slice == 'else')
end

#visit_ensure_node(node) ⇒ Object



545
546
547
548
549
# File 'lib/sirop/sourcifier.rb', line 545

def visit_ensure_node(node)
  emit_code(node.ensure_keyword_loc, semicolon: true)
  emit_code(node.statements, semicolon: true)
  emit_code(node.end_keyword_loc, semicolon: true)
end

#visit_find_pattern_node(node) ⇒ Object



616
617
618
619
620
621
622
623
624
625
626
627
# File 'lib/sirop/sourcifier.rb', line 616

def visit_find_pattern_node(node)
  emit_code(node.constant)
  emit_code(node.opening_loc)
  emit_code(node.left)
  comma = node.left
  comma = visit_comma_separated_nodes(node.requireds, comma)
  if node.right
    emit_comma if comma
    emit_code(node.right)
  end
  emit_code(node.closing_loc)
end

#visit_for_node(node) ⇒ Object



594
595
596
597
598
599
600
601
602
# File 'lib/sirop/sourcifier.rb', line 594

def visit_for_node(node)
  emit_code(node.for_keyword_loc)
  emit_code(node.index)
  emit_code(node.in_keyword_loc)
  emit_code(node.collection)
  emit_code(node.do_keyword_loc)
  emit_code(node.statements, semicolon: true)
  emit_code(node.end_keyword_loc, semicolon: true)
end

#visit_hash_node(node) ⇒ Object



480
481
482
483
484
# File 'lib/sirop/sourcifier.rb', line 480

def visit_hash_node(node)
  emit_code(node.opening_loc)
  visit_comma_separated_nodes(node.elements)
  emit_code(node.closing_loc)
end

#visit_hash_pattern_node(node) ⇒ Object



642
643
644
645
646
647
# File 'lib/sirop/sourcifier.rb', line 642

def visit_hash_pattern_node(node)
  emit_code(node.constant)
  emit_code(node.opening_loc)
  visit_comma_separated_nodes(node.elements)
  emit_code(node.closing_loc)
end

#visit_if_node(node) ⇒ Object



299
300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'lib/sirop/sourcifier.rb', line 299

def visit_if_node(node)
  if !node.if_keyword_loc
    return visit_if_node_ternary(node)
  elsif !node.end_keyword_loc
    return visit_if_node_guard(node)
  end

  emit_code(node.if_keyword_loc)
  emit_code(node.predicate)
  emit_code(node.then_keyword_loc)
  emit_code(node.statements, semicolon: true)
  emit_code(node.subsequent) if node.subsequent
  emit_code(node.end_keyword_loc, semicolon: true) if node.if_keyword_loc.slice == 'if'
end

#visit_if_node_guard(node) ⇒ Object



321
322
323
324
325
# File 'lib/sirop/sourcifier.rb', line 321

def visit_if_node_guard(node)
  emit_code(node.statements)
  emit_code(node.if_keyword_loc)
  emit_code(node.predicate)
end

#visit_if_node_ternary(node) ⇒ Object



314
315
316
317
318
319
# File 'lib/sirop/sourcifier.rb', line 314

def visit_if_node_ternary(node)
  emit_code(node.predicate)
  emit_code(node.then_keyword_loc)
  emit_code(node.statements)
  emit_code(node.subsequent)
end

#visit_index_operator_write_node(node) ⇒ Object Also known as: visit_index_and_write_node, visit_index_or_write_node



528
529
530
531
532
533
534
535
536
537
538
539
540
541
# File 'lib/sirop/sourcifier.rb', line 528

def visit_index_operator_write_node(node)
  emit_code(node.receiver)
  emit_code(node.opening_loc)
  emit_code(node.arguments)
  if node.block
    if !node.arguments.arguments.empty?
      emit_comma
    end
    emit_code(node.block)
  end
  emit_code(node.closing_loc)
  emit_code(node.operator_loc)
  emit_code(node.value)
end

#visit_interpolated_regular_expression_node(node) ⇒ Object



677
678
679
680
681
# File 'lib/sirop/sourcifier.rb', line 677

def visit_interpolated_regular_expression_node(node)
  emit_code(node.opening_loc)
  node.parts.each { |p| emit_code(p) }
  emit_code(node.closing_loc)
end

#visit_interpolated_symbol_node(node) ⇒ Object Also known as: visit_interpolated_string_node



361
362
363
364
365
# File 'lib/sirop/sourcifier.rb', line 361

def visit_interpolated_symbol_node(node)
  emit_code(node.opening_loc)
  node.parts.each { |p| emit_code(p) }
  emit_code(node.closing_loc)
end

#visit_interpolated_x_string_node(node) ⇒ Object



588
589
590
591
592
# File 'lib/sirop/sourcifier.rb', line 588

def visit_interpolated_x_string_node(node)
  emit_code(node.opening_loc)
  node.parts.each { |p| emit_code(p) }
  emit_code(node.closing_loc, chomp: true)
end

#visit_keyword_hash_node(node) ⇒ Object



295
296
297
# File 'lib/sirop/sourcifier.rb', line 295

def visit_keyword_hash_node(node)
  visit_comma_separated_nodes(node.elements)
end

#visit_lambda_node(node) ⇒ Object



668
669
670
671
672
673
674
675
# File 'lib/sirop/sourcifier.rb', line 668

def visit_lambda_node(node)
  emit_code(node.operator_loc)
  emit_code(node.parameters)
  emit_code(node.opening_loc)
  emit_code(node.body, semicolon: node.opening_loc.slice == 'do')
  node.after_body_proc&.(node)
  emit_code(node.closing_loc, semicolon: node.closing_loc.slice == 'end')
end

#visit_module_node(node) ⇒ Object



573
574
575
576
577
578
# File 'lib/sirop/sourcifier.rb', line 573

def visit_module_node(node)
  emit_code(node.module_keyword_loc)
  emit_code(node.constant_path)
  emit_code(node.body, semicolon: true)
  emit_code(node.end_keyword_loc, semicolon: true)
end

#visit_multi_target_node(node) ⇒ Object



604
605
606
607
608
609
610
611
612
613
614
# File 'lib/sirop/sourcifier.rb', line 604

def visit_multi_target_node(node)
  emit_code(node.lparen_loc)
  comma = visit_comma_separated_nodes(node.lefts)
  if node.rest
    emit_comma if comma
    emit_code(node.rest)
    comma = true
  end
  visit_comma_separated_nodes(node.rights, comma)
  emit_code(node.rparen_loc)
end

#visit_multi_write_node(node) ⇒ Object



496
497
498
499
500
501
502
503
504
505
506
507
508
# File 'lib/sirop/sourcifier.rb', line 496

def visit_multi_write_node(node)
  emit_code(node.lparen_loc)
  comma = visit_comma_separated_nodes(node.lefts)
  if node.rest
    emit_comma if comma
    emit_code(node.rest)
    comma = true
  end
  visit_comma_separated_nodes(node.rights, comma)
  emit_code(node.rparen_loc)
  emit_code(node.operator_loc)
  emit_code(node.value)
end

#visit_parameters_node(node) ⇒ Object



253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/sirop/sourcifier.rb', line 253

def visit_parameters_node(node)
  comma = false
  # injected_prefix is a custom attribute added by Sirop to the
  # ParametersNode class (in lib/sirop/prism_ext.rb). It is used
  # as a way to add a first parameter to a block or method.
  if node.injected_prefix
    emit_code(node.injected_prefix)
    # adjust last_loc_end for proper whitespace after comma
    @last_loc_end[1] -= 2 if @last_loc_end
    # binding.irb
    comma = true
  end
  comma = visit_comma_separated_nodes(node.requireds, comma)
  comma = visit_comma_separated_nodes(node.optionals, comma)
  if node.rest
    emit_comma if comma
    emit_code(node.rest)
    comma = true
  end
  comma = visit_comma_separated_nodes(node.posts, comma)
  comma = visit_comma_separated_nodes(node.keywords, comma)
  # if node.rest
  #   emit_comma if comma
  #   comma = true
  #   emit_code(node.rest)
  # end
  if node.keyword_rest
    emit_comma if comma
    comma = true
    emit_code(node.keyword_rest)
  end
  if node.block
    emit_comma if comma
    comma = true
    emit_code(node.block)
  end
end

#visit_plan(plan, node) ⇒ Object



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/sirop/sourcifier.rb', line 218

def visit_plan(plan, node)
  return send(plan, node) if plan.is_a?(Symbol)

  insert_semicolon = false
  plan.each_with_index do |sym, idx|
    if sym == :semicolon
      insert_semicolon = true
      next
    end

    obj = node.send(sym)
    emit_code(obj, semicolon: insert_semicolon)
    insert_semicolon = false
  end
end

#visit_rescue_node(node) ⇒ Object



510
511
512
513
514
515
516
517
# File 'lib/sirop/sourcifier.rb', line 510

def visit_rescue_node(node)
  emit_code(node.keyword_loc, semicolon: true)
  visit_comma_separated_nodes(node.exceptions)
  emit_code(node.operator_loc)
  emit_code(node.reference)
  emit_code(node.statements, semicolon: true)
  emit_code(node.subsequent)
end

#visit_singleton_class_node(node) ⇒ Object



580
581
582
583
584
585
586
# File 'lib/sirop/sourcifier.rb', line 580

def visit_singleton_class_node(node)
  emit_code(node.class_keyword_loc)
  emit_code(node.operator_loc)
  emit_code(node.expression)
  emit_code(node.body, semicolon: true)
  emit_code(node.end_keyword_loc, semicolon: true)
end

#visit_space_separated_nodes(list) ⇒ Object



245
246
247
248
249
250
251
# File 'lib/sirop/sourcifier.rb', line 245

def visit_space_separated_nodes(list)
  if list
    list.each do |child|
      emit_code(child)
    end
  end
end

#visit_statements_node(node) ⇒ Object



649
650
651
652
653
654
655
656
# File 'lib/sirop/sourcifier.rb', line 649

def visit_statements_node(node)
  first = true
  node.body&.each do |n|
    @semicolon = !first
    visit(n)
    first = false
  end
end

#visit_super_node(node) ⇒ Object



683
684
685
686
687
688
689
690
691
692
693
694
# File 'lib/sirop/sourcifier.rb', line 683

def visit_super_node(node)
  emit_code(node.keyword_loc)
  emit_code(node.lparen_loc)
  emit_code(node.arguments)
  emit_block_pre_rparen = node.block.is_a?(Prism::BlockArgumentNode)
  if emit_block_pre_rparen
    emit_comma if node.arguments
    emit_code(node.block)
  end
  emit_code(node.rparen_loc)
  emit_code(node.block) if !emit_block_pre_rparen
end

#visit_undef_node(node) ⇒ Object



696
697
698
699
# File 'lib/sirop/sourcifier.rb', line 696

def visit_undef_node(node)
  emit_code(node.keyword_loc)
  visit_comma_separated_nodes(node.names)
end

#visit_unless_node(node) ⇒ Object



327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'lib/sirop/sourcifier.rb', line 327

def visit_unless_node(node)
  if !node.end_keyword_loc
    return visit_unless_node_guard(node)
  end

  emit_code(node.keyword_loc)
  emit_code(node.predicate)
  emit_code(node.then_keyword_loc)
  @semicolon = true
  emit_code(node.statements)
  emit_code(node.else_clause) if node.else_clause
  emit_code(node.end_keyword_loc, semicolon: true) if node.keyword_loc.slice == 'unless'
end

#visit_unless_node_guard(node) ⇒ Object



341
342
343
344
345
# File 'lib/sirop/sourcifier.rb', line 341

def visit_unless_node_guard(node)
  emit_code(node.statements)
  emit_code(node.keyword_loc)
  emit_code(node.predicate)
end

#visit_until_node(node) ⇒ Object



465
466
467
468
469
470
471
472
# File 'lib/sirop/sourcifier.rb', line 465

def visit_until_node(node)
  return visit_until_node_guard(node) if !node.closing_loc

  emit_code(node.keyword_loc)
  emit_code(node.predicate)
  emit_code(node.statements, semicolon: true)
  emit_code(node.closing_loc, semicolon: true)
end

#visit_until_node_guard(node) ⇒ Object



474
475
476
477
478
# File 'lib/sirop/sourcifier.rb', line 474

def visit_until_node_guard(node)
  emit_code(node.statements)
  emit_code(node.keyword_loc)
  emit_code(node.predicate)
end

#visit_when_node(node) ⇒ Object



355
356
357
358
359
# File 'lib/sirop/sourcifier.rb', line 355

def visit_when_node(node)
  emit_code(node.keyword_loc)
  visit_comma_separated_nodes(node.conditions)
  emit_code(node.statements, semicolon: true)
end

#visit_while_node(node) ⇒ Object



450
451
452
453
454
455
456
457
# File 'lib/sirop/sourcifier.rb', line 450

def visit_while_node(node)
  return visit_while_node_guard(node) if !node.closing_loc

  emit_code(node.keyword_loc)
  emit_code(node.predicate)
  emit_code(node.statements, semicolon: true)
  emit_code(node.closing_loc, semicolon: true)
end

#visit_while_node_guard(node) ⇒ Object



459
460
461
462
463
# File 'lib/sirop/sourcifier.rb', line 459

def visit_while_node_guard(node)
  emit_code(node.statements)
  emit_code(node.keyword_loc)
  emit_code(node.predicate)
end