Class: Sirop::Sourcifier
- Inherits:
-
Prism::BasicVisitor
- Object
- Prism::BasicVisitor
- Sirop::Sourcifier
- 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, :child], 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, 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, :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
-
#buffer ⇒ Object
readonly
Returns the value of attribute buffer.
Instance Method Summary collapse
- #adjust_whitespace(loc) ⇒ Object
- #emit(str) ⇒ Object
- #emit_code(loc, semicolon: false, chomp: false) ⇒ Object
- #emit_comma ⇒ Object
- #emit_nothing(node) ⇒ Object
- #emit_semicolon(loc) ⇒ Object
- #emit_str(str) ⇒ Object
- #emit_verbatim(node) ⇒ Object
-
#initialize ⇒ Sourcifier
constructor
A new instance of Sourcifier.
- #loc_end(loc) ⇒ Object
- #loc_start(loc) ⇒ Object
- #method_missing(sym, node, *args) ⇒ Object
- #to_source(node) ⇒ Object
- #visit_arguments_node(node, subscript = 0..-1)) ⇒ Object
- #visit_array_node(node) ⇒ Object
- #visit_array_pattern_node(node) ⇒ Object
- #visit_begin_node(node) ⇒ Object
- #visit_block_parameters_node(node) ⇒ Object
- #visit_call_node(node) ⇒ Object
- #visit_call_node_attribute_write(node) ⇒ Object
- #visit_call_node_unary_op(node) ⇒ Object
- #visit_call_target_node(node) ⇒ Object
- #visit_case_match_node(node) ⇒ Object
- #visit_case_node(node) ⇒ Object
- #visit_class_node(node) ⇒ Object
- #visit_comma_separated_nodes(list, comma = false) ⇒ Object
- #visit_def_node(node) ⇒ Object
- #visit_else_node(node) ⇒ Object
- #visit_ensure_node(node) ⇒ Object
- #visit_find_pattern_node(node) ⇒ Object
- #visit_for_node(node) ⇒ Object
- #visit_hash_node(node) ⇒ Object
- #visit_hash_pattern_node(node) ⇒ Object
- #visit_if_node(node) ⇒ Object
- #visit_if_node_guard(node) ⇒ Object
- #visit_if_node_ternary(node) ⇒ Object
- #visit_index_operator_write_node(node) ⇒ Object (also: #visit_index_and_write_node, #visit_index_or_write_node)
- #visit_interpolated_regular_expression_node(node) ⇒ Object
- #visit_interpolated_symbol_node(node) ⇒ Object (also: #visit_interpolated_string_node)
- #visit_interpolated_x_string_node(node) ⇒ Object
- #visit_keyword_hash_node(node) ⇒ Object
- #visit_lambda_node(node) ⇒ Object
- #visit_module_node(node) ⇒ Object
- #visit_multi_target_node(node) ⇒ Object
- #visit_multi_write_node(node) ⇒ Object
- #visit_parameters_node(node) ⇒ Object
- #visit_plan(plan, node) ⇒ Object
- #visit_rescue_node(node) ⇒ Object
- #visit_singleton_class_node(node) ⇒ Object
- #visit_space_separated_nodes(list) ⇒ Object
- #visit_statements_node(node) ⇒ Object
- #visit_super_node(node) ⇒ Object
- #visit_undef_node(node) ⇒ Object
- #visit_unless_node(node) ⇒ Object
- #visit_unless_node_guard(node) ⇒ Object
- #visit_until_node(node) ⇒ Object
- #visit_until_node_guard(node) ⇒ Object
- #visit_when_node(node) ⇒ Object
- #visit_while_node(node) ⇒ Object
- #visit_while_node_guard(node) ⇒ Object
Constructor Details
#initialize ⇒ Sourcifier
Returns a new instance of Sourcifier.
94 95 96 |
# File 'lib/sirop/sourcifier.rb', line 94 def initialize @buffer = +'' end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, node, *args) ⇒ Object
185 186 187 188 189 190 |
# File 'lib/sirop/sourcifier.rb', line 185 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
#buffer ⇒ Object (readonly)
Returns the value of attribute buffer.
92 93 94 |
# File 'lib/sirop/sourcifier.rb', line 92 def buffer @buffer end |
Instance Method Details
#adjust_whitespace(loc) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/sirop/sourcifier.rb', line 116 def adjust_whitespace(loc) 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 elsif line_diff == 0 ofs = loc.start_column - @last_loc_end.last if ofs > 0 @buffer << ' ' * ofs end end else # empty buffer @buffer << ' ' * loc.start_column end @last_loc = loc @last_loc_start = loc_start(loc) @last_loc_end = loc_end(loc) end |
#emit(str) ⇒ Object
112 113 114 |
# File 'lib/sirop/sourcifier.rb', line 112 def emit(str) @buffer << str end |
#emit_code(loc, semicolon: false, chomp: false) ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/sirop/sourcifier.rb', line 139 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_comma ⇒ Object
172 173 174 |
# File 'lib/sirop/sourcifier.rb', line 172 def emit_comma emit_str(',') end |
#emit_nothing(node) ⇒ Object
163 164 165 |
# File 'lib/sirop/sourcifier.rb', line 163 def emit_nothing(node) # emit nothing end |
#emit_semicolon(loc) ⇒ Object
176 177 178 179 180 181 182 183 |
# File 'lib/sirop/sourcifier.rb', line 176 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
167 168 169 170 |
# File 'lib/sirop/sourcifier.rb', line 167 def emit_str(str) emit(str) @last_loc_end[1] += str.size end |
#emit_verbatim(node) ⇒ Object
159 160 161 |
# File 'lib/sirop/sourcifier.rb', line 159 def emit_verbatim(node) emit_code(node.location) end |
#loc_end(loc) ⇒ Object
108 109 110 |
# File 'lib/sirop/sourcifier.rb', line 108 def loc_end(loc) [loc.end_line, loc.end_column] end |
#loc_start(loc) ⇒ Object
104 105 106 |
# File 'lib/sirop/sourcifier.rb', line 104 def loc_start(loc) [loc.start_line, loc.start_column] end |
#to_source(node) ⇒ Object
98 99 100 101 102 |
# File 'lib/sirop/sourcifier.rb', line 98 def to_source(node) @buffer.clear visit(node) @buffer end |
#visit_arguments_node(node, subscript = 0..-1)) ⇒ Object
270 271 272 |
# File 'lib/sirop/sourcifier.rb', line 270 def visit_arguments_node(node, subscript = 0..-1) visit_comma_separated_nodes(node.arguments[subscript]) end |
#visit_array_node(node) ⇒ Object
465 466 467 468 469 470 471 472 473 |
# File 'lib/sirop/sourcifier.rb', line 465 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
608 609 610 611 612 613 614 615 616 617 618 619 |
# File 'lib/sirop/sourcifier.rb', line 608 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
498 499 500 501 502 503 504 505 |
# File 'lib/sirop/sourcifier.rb', line 498 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
637 638 639 640 641 642 643 644 645 |
# File 'lib/sirop/sourcifier.rb', line 637 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
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 |
# File 'lib/sirop/sourcifier.rb', line 364 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.) ol = node.opening_loc = !ol || (ol.start_line != ml.start_line) || (ol.start_column != ml.start_column) emit_code(node.) if 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
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 |
# File 'lib/sirop/sourcifier.rb', line 405 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.) 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
398 399 400 401 402 403 |
# File 'lib/sirop/sourcifier.rb', line 398 def visit_call_node_unary_op(node) emit_code(node.) emit_code(node.opening_loc) emit_code(node.receiver) emit_code(node.closing_loc) end |
#visit_call_target_node(node) ⇒ Object
423 424 425 426 427 |
# File 'lib/sirop/sourcifier.rb', line 423 def visit_call_target_node(node) emit_code(node.receiver) emit_code(node.call_operator_loc) emit_code(node.) end |
#visit_case_match_node(node) ⇒ Object
535 536 537 538 539 540 541 |
# File 'lib/sirop/sourcifier.rb', line 535 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
326 327 328 329 330 331 332 |
# File 'lib/sirop/sourcifier.rb', line 326 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.consequent, semicolon: true) emit_code(node.end_keyword_loc, semicolon: true) end |
#visit_class_node(node) ⇒ Object
543 544 545 546 547 548 549 550 |
# File 'lib/sirop/sourcifier.rb', line 543 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
213 214 215 216 217 218 219 220 221 222 |
# File 'lib/sirop/sourcifier.rb', line 213 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
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 |
# File 'lib/sirop/sourcifier.rb', line 347 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
530 531 532 533 |
# File 'lib/sirop/sourcifier.rb', line 530 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
524 525 526 527 528 |
# File 'lib/sirop/sourcifier.rb', line 524 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
595 596 597 598 599 600 601 602 603 604 605 606 |
# File 'lib/sirop/sourcifier.rb', line 595 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
573 574 575 576 577 578 579 580 581 |
# File 'lib/sirop/sourcifier.rb', line 573 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
459 460 461 462 463 |
# File 'lib/sirop/sourcifier.rb', line 459 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
621 622 623 624 625 626 |
# File 'lib/sirop/sourcifier.rb', line 621 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
278 279 280 281 282 283 284 285 286 287 288 289 290 291 |
# File 'lib/sirop/sourcifier.rb', line 278 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.consequent) if node.consequent emit_code(node.end_keyword_loc, semicolon: true) if node.if_keyword_loc.slice == 'if' end |
#visit_if_node_guard(node) ⇒ Object
300 301 302 303 304 |
# File 'lib/sirop/sourcifier.rb', line 300 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
293 294 295 296 297 298 |
# File 'lib/sirop/sourcifier.rb', line 293 def visit_if_node_ternary(node) emit_code(node.predicate) emit_code(node.then_keyword_loc) emit_code(node.statements) emit_code(node.consequent) end |
#visit_index_operator_write_node(node) ⇒ Object Also known as: visit_index_and_write_node, visit_index_or_write_node
507 508 509 510 511 512 513 514 515 516 517 518 519 520 |
# File 'lib/sirop/sourcifier.rb', line 507 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
655 656 657 658 659 |
# File 'lib/sirop/sourcifier.rb', line 655 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
340 341 342 343 344 |
# File 'lib/sirop/sourcifier.rb', line 340 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
567 568 569 570 571 |
# File 'lib/sirop/sourcifier.rb', line 567 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
274 275 276 |
# File 'lib/sirop/sourcifier.rb', line 274 def visit_keyword_hash_node(node) visit_comma_separated_nodes(node.elements) end |
#visit_lambda_node(node) ⇒ Object
647 648 649 650 651 652 653 |
# File 'lib/sirop/sourcifier.rb', line 647 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') emit_code(node.closing_loc, semicolon: node.closing_loc.slice == 'end') end |
#visit_module_node(node) ⇒ Object
552 553 554 555 556 557 |
# File 'lib/sirop/sourcifier.rb', line 552 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
583 584 585 586 587 588 589 590 591 592 593 |
# File 'lib/sirop/sourcifier.rb', line 583 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
475 476 477 478 479 480 481 482 483 484 485 486 487 |
# File 'lib/sirop/sourcifier.rb', line 475 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
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 |
# File 'lib/sirop/sourcifier.rb', line 232 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
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/sirop/sourcifier.rb', line 197 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
489 490 491 492 493 494 495 496 |
# File 'lib/sirop/sourcifier.rb', line 489 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.consequent) end |
#visit_singleton_class_node(node) ⇒ Object
559 560 561 562 563 564 565 |
# File 'lib/sirop/sourcifier.rb', line 559 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
224 225 226 227 228 229 230 |
# File 'lib/sirop/sourcifier.rb', line 224 def visit_space_separated_nodes(list) if list list.each do |child| emit_code(child) end end end |
#visit_statements_node(node) ⇒ Object
628 629 630 631 632 633 634 635 |
# File 'lib/sirop/sourcifier.rb', line 628 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
661 662 663 664 665 666 667 668 669 670 671 672 |
# File 'lib/sirop/sourcifier.rb', line 661 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
674 675 676 677 |
# File 'lib/sirop/sourcifier.rb', line 674 def visit_undef_node(node) emit_code(node.keyword_loc) visit_comma_separated_nodes(node.names) end |
#visit_unless_node(node) ⇒ Object
306 307 308 309 310 311 312 313 314 315 316 317 318 |
# File 'lib/sirop/sourcifier.rb', line 306 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.consequent) if node.consequent emit_code(node.end_keyword_loc, semicolon: true) if node.keyword_loc.slice == 'unless' end |
#visit_unless_node_guard(node) ⇒ Object
320 321 322 323 324 |
# File 'lib/sirop/sourcifier.rb', line 320 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
444 445 446 447 448 449 450 451 |
# File 'lib/sirop/sourcifier.rb', line 444 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
453 454 455 456 457 |
# File 'lib/sirop/sourcifier.rb', line 453 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
334 335 336 337 338 |
# File 'lib/sirop/sourcifier.rb', line 334 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
429 430 431 432 433 434 435 436 |
# File 'lib/sirop/sourcifier.rb', line 429 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
438 439 440 441 442 |
# File 'lib/sirop/sourcifier.rb', line 438 def visit_while_node_guard(node) emit_code(node.statements) emit_code(node.keyword_loc) emit_code(node.predicate) end |