Sha256: 4b040adb59ce9c1a9038cd0f0ca9e8a56bc6112705e4fc49fce3302e72f221ac

Contents?: true

Size: 1.01 KB

Versions: 3

Compression:

Stored size: 1.01 KB

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, "#{es2015 ? 'let' : 'var'} #{vars.join(', ')}#{@sep}"
            vars.each {|var| @vars[var] = true}
          end
        end
      ensure
        @scope = scope
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ruby2js-3.0.2 lib/ruby2js/converter/case.rb
ruby2js-3.0.1 lib/ruby2js/converter/case.rb
ruby2js-3.0.0 lib/ruby2js/converter/case.rb