Sha256: 9eb558f88287f07a84d506ad3961603be793977c59212c4810ab1d05cfd8512d

Contents?: true

Size: 1023 Bytes

Versions: 1

Compression:

Stored size: 1023 Bytes

Contents

module Ruby2JS
  class Converter

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

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

      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

1 entries across 1 versions & 1 rubygems

Version Path
ruby2js-1.1.0 lib/ruby2js/converter/if.rb