Sha256: e21c74bbfa8bdcc156efc8a12c53812102e8d0f9e30d33e9f8f0548fb12a1caa

Contents?: true

Size: 1.38 KB

Versions: 7

Compression:

Stored size: 1.38 KB

Contents

# frozen_string_literal: true
require 'opal/nodes/base'

module Opal
  module Nodes
    class IfNode < Base
      handle :if

      children :test, :true_body, :false_body

      def compile
        truthy, falsy = self.truthy, self.falsy

        push "if (", js_truthy(test), ") {"

        # skip if-body if no truthy sexp
        indent { line stmt(truthy) } if truthy

        if falsy
          if falsy.type == :if
            line "} else ", stmt(falsy)
          else
            indent do
              line "} else {"
              line stmt(falsy)
            end

            line "}"
          end
        else
          push "}"
        end

        wrap "(function() {", "; return nil; })()" if needs_wrapper?
      end

      def truthy
        needs_wrapper? ? compiler.returns(true_body || s(:nil)) : true_body
      end

      def falsy
        needs_wrapper? ? compiler.returns(false_body || s(:nil)) : false_body
      end

      def needs_wrapper?
        expr? or recv?
      end
    end

    class IFlipFlop < Base
      handle :iflipflop

      def compile
        # Unsupported
        # Always compiles to 'true' to not break generated JS
        push 'true'
      end
    end

    class EFlipFlop < Base
      handle :eflipflop

      def compile
        # Unsupported
        # Always compiles to 'true' to not break generated JS
        push 'true'
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
opal-0.11.4 lib/opal/nodes/if.rb
opal-0.11.3 lib/opal/nodes/if.rb
opal-0.11.2 lib/opal/nodes/if.rb
opal-0.11.1 lib/opal/nodes/if.rb
opal-0.11.1.pre lib/opal/nodes/if.rb
opal-0.11.0 lib/opal/nodes/if.rb
opal-0.11.0.rc1 lib/opal/nodes/if.rb