Sha256: 4cc4898e770501561a73d2e219d61cc3090e1dade754d6821b00c86e56a5be43

Contents?: true

Size: 1.38 KB

Versions: 13

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 = self.truthy
        falsy = 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
            line '} else {'
            indent do
              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? || 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

13 entries across 13 versions & 1 rubygems

Version Path
opal-1.2.0 lib/opal/nodes/if.rb
opal-1.2.0.beta1 lib/opal/nodes/if.rb
opal-1.1.1 lib/opal/nodes/if.rb
opal-1.1.1.rc1 lib/opal/nodes/if.rb
opal-1.1.0 lib/opal/nodes/if.rb
opal-1.1.0.rc1 lib/opal/nodes/if.rb
opal-1.0.5 lib/opal/nodes/if.rb
opal-1.0.4 lib/opal/nodes/if.rb
opal-1.0.3 lib/opal/nodes/if.rb
opal-1.0.2 lib/opal/nodes/if.rb
opal-1.0.1 lib/opal/nodes/if.rb
opal-1.0.0 lib/opal/nodes/if.rb
opal-1.0.0.beta1 lib/opal/nodes/if.rb