Sha256: ec1b323c3d9e68a6ade4962d1f05c493b7e20995d650b04f79506dd110bb14b2

Contents?: true

Size: 1.31 KB

Versions: 9

Compression:

Stored size: 1.31 KB

Contents

require 'ruby2js'

module Ruby2JS
  module Filter
    module Return
      include SEXP

      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
    end

    DEFAULTS.push Return
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
ruby2js-1.3.0 lib/ruby2js/filter/return.rb
ruby2js-1.2.0 lib/ruby2js/filter/return.rb
ruby2js-1.1.5 lib/ruby2js/filter/return.rb
ruby2js-1.1.4 lib/ruby2js/filter/return.rb
ruby2js-1.1.3 lib/ruby2js/filter/return.rb
ruby2js-1.1.2 lib/ruby2js/filter/return.rb
ruby2js-1.1.1 lib/ruby2js/filter/return.rb
ruby2js-1.1.0 lib/ruby2js/filter/return.rb
ruby2js-1.0.0 lib/ruby2js/filter/return.rb