Sha256: 2b6191f5ca5960c23de9ad0827c81adc7e1c42e9889b689490ec48a0c4b607b7

Contents?: true

Size: 1.41 KB

Versions: 1

Compression:

Stored size: 1.41 KB

Contents

require 'ruby2js'

module Ruby2JS
  module Filter
    module Return
      EXPRESSIONS = [ :array, :float, :hash, :if, :int, :lvar, :nil, :send ]

      def on_block(node)
        children = process_all(node.children)

        # find the block
        block = [children.pop || s(:nil)]
        while block.length == 1 and block.first.type == :begin
          block = block.first.children.dup
        end

        if EXPRESSIONS.include? block.last.type 
          block.push s(:return, block.pop)
else
p block.last.type
        end

        if block.length == 1
          children.push block.first
        else
          children.push s(:begin, *block)
        end

        node.updated nil, children
      end

      def on_def(node)
        children = process_all(node.children[1..-1])
        children.unshift node.children.first

        # find the block
        block = [children.pop || s(:nil)]
        while block.length == 1 and block.first.type == :begin
          block = block.first.children.dup
        end

        if EXPRESSIONS.include? block.last.type 
          block.push s(:return, block.pop)
        end

        if block.length == 1
          children.push block.first
        else
          children.push s(:begin, *block)
        end

        node.updated nil, children
      end
      
      private

      # construct an AST Node
      def s(type, *args)
        Parser::AST::Node.new type, args
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ruby2js-0.1.0 lib/ruby2js/filter/return.rb