Class: Opal::Rewriters::DotJsSyntax

Inherits:
Base
  • Object
show all
Defined in:
opal/lib/opal/rewriters/dot_js_syntax.rb

Instance Method Summary collapse

Methods inherited from Base

#append_to_body, #prepend_to_body, #s, s

Instance Method Details

#on_send(node) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'opal/lib/opal/rewriters/dot_js_syntax.rb', line 7

def on_send(node)
  recv, meth, *args = *node
  if recv && recv.type == :send
    recv_of_recv, meth_of_recv, _ = *recv
    if meth_of_recv == :JS
      case meth
      when :[]
        if args.size != 1
          raise SyntaxError, ".JS[:property] syntax supports only one argument"
        end
        property = args.first

        node = to_js_attr_call(recv_of_recv, property)
      when :[]=
        if args.size != 2
          raise SyntaxError, '.JS[:property]= syntax supports only two arguments'
        end

        property, value = *args
        node = to_js_attr_assign_call(recv_of_recv, property, value)
      else
        node = to_native_js_call(recv_of_recv, meth, args)
      end
      super(node)
    else
      super
    end
  else
    super
  end
end

#to_js_attr_assign_call(recv, property, value) ⇒ Object

Parameters:

  • recv (AST::Node)

    receiver of .JS[]= method

  • property (AST::Node)

    property passed to brackets

  • value (AST::Node)

    value of assignment



55
56
57
# File 'opal/lib/opal/rewriters/dot_js_syntax.rb', line 55

def to_js_attr_assign_call(recv, property, value)
  s(:jsattrasgn, recv, property, value)
end

#to_js_attr_call(recv, property) ⇒ Object

Parameters:

  • recv (AST::Node)

    receiver of .JS[] method

  • property (AST::Node)

    argument passed to .JS[] method



48
49
50
# File 'opal/lib/opal/rewriters/dot_js_syntax.rb', line 48

def to_js_attr_call(recv, property)
  s(:jsattr, recv, property)
end

#to_native_js_call(recv, meth, args) ⇒ Object

Parameters:

  • recv (AST::Node)

    receiver of .JS. method

  • meth (Symbol)

    name of the JS method

  • args (Array<AST::Node>)

    list of the arguments passed to JS method



42
43
44
# File 'opal/lib/opal/rewriters/dot_js_syntax.rb', line 42

def to_native_js_call(recv, meth, args)
  s(:jscall, recv, meth, *args)
end