Sha256: 4a0396f9cc8cd043f1a442e6f19879a3db27db85ae048cee37a950046a8d32fd

Contents?: true

Size: 1.35 KB

Versions: 2

Compression:

Stored size: 1.35 KB

Contents

# frozen_string_literal: true

module WhatDyaReturn
  module AST
    #
    # This file is based on https://github.com/rubocop/rubocop-ast/blob/v1.28.1/lib/rubocop/ast/builder.rb
    # The original code is licensed under the MIT License.
    #
    # https://github.com/rubocop/rubocop-ast/blob/v1.28.1/LICENSE.txt
    #
    class Builder < Parser::Builders::Default
      NODE_MAP = {
        def: DefNode,
        begin: BeginNode,
        kwbegin: BeginNode,
        array: ArrayNode,
        return: ReturnNode,
        if: IfNode,
        case: CaseNode,
        when: WhenNode,
        rescue: RescueNode,
        resbody: ResbodyNode,
        ensure: EnsureNode,
        while: WhileNode,
        until: UntilNode,
        for: ForNode,
        break: BreakNode,
        next: NextNode,
        block: BlockNode
      }.freeze

      #
      # NOTE: Reason why wrap with ArrayNode if the node is `return` or `break` and has multiple children.
      #
      #   To treat `return 1, 2` like `return [1, 2]`.
      #
      # @return [Node]
      #
      def n(type, children, source_map)
        children = [ArrayNode.new(:array, children)] if %i[return break].include?(type) && children.size > 1

        node_klass(type).new(type, children, location: source_map)
      end

      private

      def node_klass(type)
        NODE_MAP[type] || Node
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
what_dya_return-0.2.1 lib/what_dya_return/ast/builder.rb
what_dya_return-0.2.0 lib/what_dya_return/ast/builder.rb