Sha256: 9fd7360ffe7238975f3b14733f9002fd2313dcad004de7b67f97d5c89dd6d74e

Contents?: true

Size: 1.9 KB

Versions: 21

Compression:

Stored size: 1.9 KB

Contents

module Ruby2JS
  class Converter

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

    handle :case do |expr, *whens, other|
      begin
        if @state == :expression
          parse s(:kwbegin, @ast), @state
          return
        end

        inner, @inner = @inner, @ast

        has_range = whens.any? do |node| 
          node.children.any? {|child| [:irange, :erange].include? child&.type}
        end

        if has_range
          # https://stackoverflow.com/questions/5619832/switch-on-ranges-of-integers-in-javascript
          puts 'switch (true) {'
        else
          put 'switch ('; parse expr; puts ') {'
        end

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

          *values, code = node.children

          values.each do |value| 
            put 'case '; 
            if has_range
              if value.type == :irange
                parse expr; put ' >= '; parse value.children.first; put " && "
                parse expr; put ' <= '; parse value.children.last; put ":#@ws"
              elsif value.type == :erange
                parse expr; put ' >= '; parse value.children.first; put " && "
                parse expr; put ' < '; parse value.children.last; put ":#@ws"
              else
                parse expr; put ' == '; parse value; put ":#@ws"
              end
            else
              parse value; put ":#@ws"
            end
          end

          parse code, :statement
          last = code
          while last&.type == :begin
            last = last.children.last
          end

          if other or index < whens.length-1
            put "#{@sep}" 
            put "break#@sep" unless last.type == :return
          end
        end

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

        sput '}'
      ensure
        @inner = inner
      end
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
ruby2js-5.1.1 lib/ruby2js/converter/case.rb
ruby2js-5.1.0 lib/ruby2js/converter/case.rb
ruby2js-5.0.1 lib/ruby2js/converter/case.rb
ruby2js-5.0.0 lib/ruby2js/converter/case.rb
ruby2js-4.2.2 lib/ruby2js/converter/case.rb
ruby2js-4.2.1 lib/ruby2js/converter/case.rb
ruby2js-4.2.0 lib/ruby2js/converter/case.rb
ruby2js-4.1.7 lib/ruby2js/converter/case.rb
ruby2js-4.1.6 lib/ruby2js/converter/case.rb
ruby2js-4.1.5 lib/ruby2js/converter/case.rb
ruby2js-4.1.4 lib/ruby2js/converter/case.rb
ruby2js-4.1.3 lib/ruby2js/converter/case.rb
ruby2js-4.1.2 lib/ruby2js/converter/case.rb
ruby2js-4.1.1 lib/ruby2js/converter/case.rb
ruby2js-4.1.0 lib/ruby2js/converter/case.rb
ruby2js-4.0.5 lib/ruby2js/converter/case.rb
ruby2js-4.0.4 lib/ruby2js/converter/case.rb
ruby2js-4.0.3 lib/ruby2js/converter/case.rb
ruby2js-4.0.2 lib/ruby2js/converter/case.rb
ruby2js-4.0.1 lib/ruby2js/converter/case.rb