Sha256: e6bbb5c2c9f020cb544a33f6b4115fdd0fb6ae58535defd2d36f09690c17a57b

Contents?: true

Size: 1.9 KB

Versions: 17

Compression:

Stored size: 1.9 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
        begin
          scope, @scope = @scope, false
          mark = output_location

          # use short form when appropriate
          unless else_block or then_block.type == :begin
            put "if ("; parse condition; put ') '
            wrap { parse then_block, :statement }
          else
            put "if ("; parse condition; puts ') {'
            parse then_block, :statement
            sput '}'

            while else_block and else_block.type == :if
              condition, then_block, else_block = else_block.children
              if then_block
                put ' else if ('; parse condition; puts ') {'
                parse then_block, :statement
                sput '}'
              else
                put ' else if ('; parse s(:not, condition); puts ') {'
                parse else_block, :statement
                sput '}'
                else_block = nil
              end
            end

            if else_block
              puts ' else {'; parse else_block, :statement; sput '}'
            end
          end

          if scope
            vars = @vars.select {|key, value| value == :pending}.keys
            unless vars.empty?
              insert mark, "var #{vars.join(', ')}#{@sep}"
              vars.each {|var| @vars[var] = true}
            end
          end
        ensure
          @scope = scope
        end
      else
        else_block ||= s(:nil)
        put '('; parse condition; put ' ? '; parse then_block
        put ' : '; parse else_block; put ')'
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
ruby2js-2.1.24 lib/ruby2js/converter/if.rb
ruby2js-2.1.23 lib/ruby2js/converter/if.rb
ruby2js-2.1.22 lib/ruby2js/converter/if.rb
ruby2js-2.1.21 lib/ruby2js/converter/if.rb
ruby2js-2.1.20 lib/ruby2js/converter/if.rb
ruby2js-2.1.19 lib/ruby2js/converter/if.rb
ruby2js-2.1.18 lib/ruby2js/converter/if.rb
ruby2js-2.1.17 lib/ruby2js/converter/if.rb
ruby2js-2.1.16 lib/ruby2js/converter/if.rb
ruby2js-2.1.15 lib/ruby2js/converter/if.rb
ruby2js-2.1.14 lib/ruby2js/converter/if.rb
ruby2js-2.1.13 lib/ruby2js/converter/if.rb
ruby2js-2.1.12 lib/ruby2js/converter/if.rb
ruby2js-2.1.11 lib/ruby2js/converter/if.rb
ruby2js-2.1.10 lib/ruby2js/converter/if.rb
ruby2js-2.1.9 lib/ruby2js/converter/if.rb
ruby2js-2.1.8 lib/ruby2js/converter/if.rb