Class: Opal::Rewriters::DotJsSyntax

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

Constant Summary

Constants inherited from Base

Base::DUMMY_LOCATION

Instance Attribute Summary

Attributes inherited from Base

#current_node

Instance Method Summary collapse

Methods inherited from Base

#append_to_body, #begin_with_stmts, #dynamic!, #error, #on_top, #prepend_to_body, #process, s, #s, #stmts_of

Instance Method Details

#on_send(node) ⇒ Object



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
38
# File 'opal/lib/opal/rewriters/dot_js_syntax.rb', line 8

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
          error '.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
          error '.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



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

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



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

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



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

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