Sha256: db2216cd453b1c62e40febe7af9ce099f4fca754374e75f3d5445cab22e1aabe

Contents?: true

Size: 1.26 KB

Versions: 4

Compression:

Stored size: 1.26 KB

Contents

module Ruby2JS
  class Converter

    # (op-asgn
    #   (lvasgn :a) :+
    #   (int 1))

    # NOTE: and-asgn and or_asgn handled below

    handle :op_asgn do |var, op, value|
      var = s(:ivar, var.children.first) if var.type == :ivasgn
      var = s(:lvar, var.children.first) if var.type == :lvasgn
      var = s(:cvar, var.children.first) if var.type == :cvasgn

      if [:+, :-].include?(op) and value.type==:int and value.children==[1]
        if @state == :statement
          "#{ parse var }#{ op }#{ op }"
        else
          "#{ op }#{ op }#{ parse var }"
        end
      else
        "#{ parse var } #{ op }= #{ parse value }"
      end
    end

    # (or-asgn
    #   (lvasgn :a)
    #   (int 1))

    # (and-asgn
    #   (lvasgn :a)
    #   (int 1))

    handle :or_asgn, :and_asgn do |asgn, value|
      type = (@ast.type == :and_asgn ? :and : :or)

      vtype = nil
      vtype = :lvar if asgn.type == :lvasgn
      vtype = :ivar if asgn.type == :ivasgn
      vtype = :cvar if asgn.type == :cvasgn
      
      if vtype
        parse s(asgn.type, asgn.children.first, s(type, 
          s(vtype, asgn.children.first), value))
      else
        parse s(:send, asgn.children.first, "#{asgn.children[1]}=",
          s(type, asgn, value))
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ruby2js-1.6.0 lib/ruby2js/converter/opasgn.rb
ruby2js-1.5.0 lib/ruby2js/converter/opasgn.rb
ruby2js-1.4.0 lib/ruby2js/converter/opasgn.rb
ruby2js-1.3.0 lib/ruby2js/converter/opasgn.rb