Sha256: 2f6b28a611997563e63c55267709db5ec48192a8ad83088fc532ddcdd3f7ed89

Contents?: true

Size: 1.46 KB

Versions: 11

Compression:

Stored size: 1.46 KB

Contents

# frozen_string_literal: true

module Unparser
  class Emitter
    # Emitter control flow modifiers
    class FlowModifier < self

      MAP = {
        return: K_RETURN,
        next:   K_NEXT,
        break:  K_BREAK
      }.freeze

      handle(*MAP.keys)

      def terminated?
        children.empty?
      end

    private

      # Perform dispatch
      #
      # @return [undefined]
      #
      # @api private
      #
      def dispatch
        write(MAP.fetch(node.type))
        case children.length
        when 0 # rubocop:disable Lint/EmptyWhen
        when 1
          emit_single_argument
        else
          emit_arguments
        end
      end

      # Emit break or return arguments
      #
      # @return [undefined]
      #
      # @api private
      #
      def emit_arguments
        ws
        head, *tail = children
        emit_argument(head)
        tail.each do |node|
          write(DEFAULT_DELIMITER)
          emit_argument(node)
        end
      end

      PARENS = %i[if case begin].to_set.freeze

      # Emit argument
      #
      # @param [Parser::AST::Node] node
      #
      # @api private
      #
      def emit_argument(node)
        visit_plain(node)
      end

      # Emit single argument
      #
      # @api private
      #
      def emit_single_argument
        ws
        conditional_parentheses(PARENS.include?(first_child.type)) do
          visit_plain(first_child)
        end
      end

    end # Return
  end # Emitter
end # Unparser

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
unparser-0.4.9 lib/unparser/emitter/flow_modifier.rb
unparser-0.4.8 lib/unparser/emitter/flow_modifier.rb
unparser-0.4.7 lib/unparser/emitter/flow_modifier.rb
unparser-0.4.6 lib/unparser/emitter/flow_modifier.rb
unparser-0.4.5 lib/unparser/emitter/flow_modifier.rb
unparser-0.4.4 lib/unparser/emitter/flow_modifier.rb
unparser-0.4.3 lib/unparser/emitter/flow_modifier.rb
unparser-0.4.2 lib/unparser/emitter/flow_modifier.rb
unparser-0.4.1 lib/unparser/emitter/flow_modifier.rb
unparser-0.4.0 lib/unparser/emitter/flow_modifier.rb
unparser-0.3.0 lib/unparser/emitter/flow_modifier.rb