Sha256: 3b3b65cd46d9e00f761a81f7560dc1af002652cb18df2f1a5281773f86433c5c

Contents?: true

Size: 1.88 KB

Versions: 21

Compression:

Stored size: 1.88 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 var.type == :lvar
        name = var.children.first
        receiver = @rbstack.map {|rb| rb[name]}.compact.last
        if receiver
          var = s(:attr, nil, name)
        end
      end

      if \
        [:+, :-].include?(op) and value.type==:int and 
        (value.children==[1] or value.children==[-1])
      then
        if value.children.first == -1
          op = (op == :+ ? :- : :+)
        end

        if @state == :statement
          parse var; put "#{ op }#{ op }"
        else
          put "#{ op }#{ op }"; parse var
        end
      else
        parse var; put " #{ 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 es2021
        op = type == :and ? '&&' : (@or == :nullish ? '??' : '||')
        parse s(:op_asgn, asgn, op, value);
      elsif vtype
        parse s(asgn.type, asgn.children.first, s(type, 
          s(vtype, asgn.children.first), value))
      elsif asgn.type == :send and asgn.children[1] == :[]
        parse s(:send, asgn.children.first, :[]=,
          asgn.children[2], s(type, asgn, value))
      else
        parse s(:send, asgn.children.first, "#{asgn.children[1]}=",
          s(type, asgn, value))
      end
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

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