Module: Opal::Nodes::Helpers

Included in:
Base
Defined in:
opal/lib/opal/nodes/helpers.rb

Instance Method Summary collapse

Instance Method Details

#conditional_send(recvr) {|receiver_temp| ... } ⇒ Object

Usefule for safe-operator calls: foo&.bar / foo&.bar ||= baz / ...

Parameters:

  • recvr (sexp_pushable)

    The receiver of the call that will be stored in a temporary variable

Yields:

  • (receiver_temp)


95
96
97
98
99
100
101
102
103
104
# File 'opal/lib/opal/nodes/helpers.rb', line 95

def conditional_send(recvr)
  # temporary variable that stores method receiver
  receiver_temp = scope.new_temp
  push "#{receiver_temp} = ", recvr

  # execute the sexp only if the receiver isn't nil
  push ", (#{receiver_temp} === nil || #{receiver_temp} == null) ? nil : "
  yield receiver_temp
  wrap '(', ')'
end

#current_indentObject



32
33
34
# File 'opal/lib/opal/nodes/helpers.rb', line 32

def current_indent
  compiler.parser_indent
end

#empty_lineObject



41
42
43
# File 'opal/lib/opal/nodes/helpers.rb', line 41

def empty_line
  push "\n"
end

#indent(&block) ⇒ Object



28
29
30
# File 'opal/lib/opal/nodes/helpers.rb', line 28

def indent(&block)
  compiler.indent(&block)
end

#js_falsy(sexp) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
# File 'opal/lib/opal/nodes/helpers.rb', line 54

def js_falsy(sexp)
  if sexp.type == :send
    mid = sexp.children[1]
    if mid == :block_given?
      scope.uses_block!
      return "#{scope.block_name} === nil"
    end
  end

  helper :falsy
  [fragment("$falsy("),expr(sexp),fragment(")")]
end

#js_truthy(sexp) ⇒ Object



45
46
47
48
49
50
51
52
# File 'opal/lib/opal/nodes/helpers.rb', line 45

def js_truthy(sexp)
  if optimize = js_truthy_optimize(sexp)
    return optimize
  end

  helper :truthy
  [fragment("$truthy("),expr(sexp),fragment(")")]
end

#js_truthy_optimize(sexp) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'opal/lib/opal/nodes/helpers.rb', line 67

def js_truthy_optimize(sexp)
  if sexp.type == :send
    mid = sexp.children[1]
    receiver_handler_class = (receiver = sexp.children[0]) && compiler.handlers[receiver.type]

    # Only operator calls on the truthy_optimize? node classes should be optimized.
    # Monkey patch method calls might return 'self'/aka a bridged instance and need
    # the nil check - see discussion at https://github.com/opal/opal/pull/1097
    allow_optimization_on_type = Compiler::COMPARE.include?(mid.to_s) &&
      receiver_handler_class &&
      receiver_handler_class.truthy_optimize?

    if allow_optimization_on_type ||
      mid == :block_given? ||
      mid == :=="
      expr(sexp)
    end
  end
end

#line(*strs) ⇒ Object



36
37
38
39
# File 'opal/lib/opal/nodes/helpers.rb', line 36

def line(*strs)
  push "\n#{current_indent}"
  push(*strs)
end

#mid_to_jsid(mid) ⇒ Object

Converts a ruby method name into its javascript equivalent for a method/function call. All ruby method names get prefixed with a '$', and if the name is a valid javascript identifier, it will have a '.' prefix (for dot-calling), otherwise it will be wrapped in brackets to use reference notation calling.



20
21
22
23
24
25
26
# File 'opal/lib/opal/nodes/helpers.rb', line 20

def mid_to_jsid(mid)
  if /\=|\+|\-|\*|\/|\!|\?|<|\>|\&|\||\^|\%|\~|\[/ =~ mid.to_s
    "['$#{mid}']"
  else
    '.$' + mid
  end
end

#property(name) ⇒ Object



7
8
9
# File 'opal/lib/opal/nodes/helpers.rb', line 7

def property(name)
  valid_name?(name) ? ".#{name}" : "[#{name.inspect}]"
end

#valid_name?(name) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
# File 'opal/lib/opal/nodes/helpers.rb', line 11

def valid_name?(name)
  Opal::Rewriters::JsReservedWords.valid_name?(name)
end