Sha256: 1cdb4b5185eebb30f3e37db23c65ca2c5921af2de9873a57c94b5c67d91f0127

Contents?: true

Size: 1.42 KB

Versions: 1

Compression:

Stored size: 1.42 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)
        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

    DEFAULTS.push Return
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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