Class: Opal::Nodes::IfNode

Inherits:
Base show all
Defined in:
opal/lib/opal/nodes/if.rb

Constant Summary

RUBY_ENGINE_CHECK =
[:call, [:const, :RUBY_ENGINE],
:==, [:arglist, [:str, "opal"]]]
RUBY_PLATFORM_CHECK =
[:call, [:const, :RUBY_PLATFORM],
:==, [:arglist, [:str, "opal"]]]

Constants included from Helpers

Helpers::BASIC_IDENTIFIER_RULES, Helpers::ES3_RESERVED_WORD_EXCLUSIVE, Helpers::ES51_RESERVED_WORD, Helpers::IMMUTABLE_PROPS

Instance Attribute Summary

Attributes inherited from Base

#compiler, #type

Instance Method Summary collapse

Methods inherited from Base

#add_gvar, #add_ivar, #add_local, #add_temp, children, #children, #compile_to_fragments, #error, #expr, #expr?, #expr_or_nil, #fragment, handle, handlers, #helper, #in_while?, #initialize, #process, #push, #recv, #recv?, #s, #scope, #stmt, #stmt?, #unshift, #while_loop, #with_temp, #wrap

Methods included from Helpers

#current_indent, #empty_line, #indent, #js_falsy, #js_truthy, #js_truthy_optimize, #line, #lvar_to_js, #mid_to_jsid, #property, #valid_name?, #variable

Constructor Details

This class inherits a constructor from Opal::Nodes::Base

Instance Method Details

#compileObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'opal/lib/opal/nodes/if.rb', line 16

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

  if skip_check_present?
    falsy = nil
  end

  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

#falsyObject



58
59
60
# File 'opal/lib/opal/nodes/if.rb', line 58

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

#needs_wrapper?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'opal/lib/opal/nodes/if.rb', line 62

def needs_wrapper?
  expr? or recv?
end

#skip_check_present?Boolean

pre-processing only effects falsy blocks. If engine is opal, then falsy block gets generated as normal. Unless engine is opal then that code gets generated as the falsy block

Returns:

  • (Boolean)


50
51
52
# File 'opal/lib/opal/nodes/if.rb', line 50

def skip_check_present?
  test == RUBY_ENGINE_CHECK or test == RUBY_PLATFORM_CHECK
end

#truthyObject



54
55
56
# File 'opal/lib/opal/nodes/if.rb', line 54

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