Sha256: c93ad12a9debb9ea2324e954ccaf67986cc66dc853254fa13fc67450471bf325

Contents?: true

Size: 1013 Bytes

Versions: 44

Compression:

Stored size: 1013 Bytes

Contents

module Ruby2JS
  class Converter

    # (case
    #   (send nil :a)
    #   (when
    #      (int 1)
    #      (...))
    #   (...))

    handle :case do |expr, *whens, other|
      begin
        scope, @scope = @scope, false
        mark = output_location

        put 'switch ('; parse expr; puts ') {'

        whens.each_with_index do |node, index|
          puts '' unless index == 0

          *values, code = node.children
          values.each {|value| put 'case '; parse value; put ":#@ws"}
          parse code, :statement
          put "#{@sep}break#@sep" if other or index < whens.length-1
        end

        (put "#{@nl}default:#@ws"; parse other, :statement) if other

        sput '}'

        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
    end
  end
end

Version data entries

44 entries across 44 versions & 1 rubygems

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