Sha256: b3fb160c4a6f4c62e296658517d92293eda49cd1be0e26b9f96778045043dd7a

Contents?: true

Size: 1.09 KB

Versions: 4

Compression:

Stored size: 1.09 KB

Contents

module Ruby2JS
  class Converter

    # (if
    #   (true)
    #   (...)
    #   (...))

    handle :if do |condition, then_block, else_block|
      # return parse condition if not else_block and not then_block
      if else_block and not then_block
        return parse(s(:if, s(:send, condition, :!), else_block, nil), @state) 
      end

      then_block ||= s(:nil)

      if @state == :statement
        output = "if (#{ parse condition }) {#@nl#{ scope then_block }#@nl}"
        while else_block and else_block.type == :if
          condition, then_block, else_block = else_block.children
          output <<  " else if (#{ parse condition }) " +
            "{#@nl#{ scope then_block }#@nl}"
        end
        output << " else {#@nl#{ scope else_block }#@nl}" if else_block

        # use short form when appropriate
        unless output.length > 72 or else_block or then_block.type == :begin
          output = "if (#{ parse condition }) #{ scope then_block }"
        end

        output
      else
        "(#{ parse condition } ? #{ parse then_block } : #{ parse else_block })"
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ruby2js-1.1.4 lib/ruby2js/converter/if.rb
ruby2js-1.1.3 lib/ruby2js/converter/if.rb
ruby2js-1.1.2 lib/ruby2js/converter/if.rb
ruby2js-1.1.1 lib/ruby2js/converter/if.rb