Sha256: ccd79d7531b65058ffd643fe3fe327ae2e76cc62dc0c2fbf59b4a5997d6fefb7

Contents?: true

Size: 1.12 KB

Versions: 21

Compression:

Stored size: 1.12 KB

Contents

module Ruby2JS
  class Converter

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

    handle :if do |condition, then_block, else_block|
      # return parse not condition if else_block and no then_block
      if else_block and not then_block
        return parse(s(:if, s(:not, 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>@width-8 or else_block or then_block.type == :begin
          output = "if (#{ parse condition }) #{ scope then_block }"
        end

        output
      else
        else_block ||= s(:nil)
        "(#{ parse condition } ? #{ parse then_block } : #{ parse else_block })"
      end
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
ruby2js-1.15.1 lib/ruby2js/converter/if.rb
ruby2js-1.15.0 lib/ruby2js/converter/if.rb
ruby2js-1.14.1 lib/ruby2js/converter/if.rb
ruby2js-1.14.0 lib/ruby2js/converter/if.rb
ruby2js-1.13.1 lib/ruby2js/converter/if.rb
ruby2js-1.13.0 lib/ruby2js/converter/if.rb
ruby2js-1.12.2 lib/ruby2js/converter/if.rb
ruby2js-1.12.1 lib/ruby2js/converter/if.rb
ruby2js-1.12.0 lib/ruby2js/converter/if.rb
ruby2js-1.11.1 lib/ruby2js/converter/if.rb
ruby2js-1.11.0 lib/ruby2js/converter/if.rb
ruby2js-1.10.0 lib/ruby2js/converter/if.rb
ruby2js-1.9.3 lib/ruby2js/converter/if.rb
ruby2js-1.9.2 lib/ruby2js/converter/if.rb
ruby2js-1.9.1 lib/ruby2js/converter/if.rb
ruby2js-1.9.0 lib/ruby2js/converter/if.rb
ruby2js-1.8.0 lib/ruby2js/converter/if.rb
ruby2js-1.7.0 lib/ruby2js/converter/if.rb
ruby2js-1.6.0 lib/ruby2js/converter/if.rb
ruby2js-1.5.0 lib/ruby2js/converter/if.rb