-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathjsx.rb
87 lines (75 loc) · 2.64 KB
/
jsx.rb
1
2
3
4
5
6
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# TODO: This feature is deprecated.
require 'ruby2js'
# Convert Wunderbar syntax to JSX
module Ruby2JS
module Filter
module JSX
include SEXP
def on_send(node)
target, method, *args = node.children
if target == s(:const, nil, :Wunderbar)
if [:debug, :info, :warn, :error, :fatal].include? method
method = :error if method == :fatal
return node.updated(nil, [s(:const, nil, :console), method, *args])
end
end
stack = []
while target!=nil and target.type==:send and target.children.length==2
name = method.to_s
if name.end_with? '!'
stack << s(:hash, s(:pair, s(:sym, :id), s(:str, name[0..-2])))
else
stack << s(:hash, s(:pair, s(:sym, :class), s(:str, name)))
end
target, method = target.children
end
if target == nil and method.to_s.start_with? "_"
S(:xnode, method.to_s[1..-1], *stack, *process_all(args))
elsif method == :createElement and target == s(:const, nil, :React)
if args.first.type == :str and \
(args.length == 1 or %i(nil hash).include? args[1].type)
attrs = (args[1]&.type != :nil && args[1]) || s(:hash)
S(:xnode, args[0].children.first, attrs, *process_all(args[2..-1]))
else
super
end
else
super
end
end
def on_block(node)
send, args, *block = node.children
target, method, *_ = send.children
while target!=nil and target.type==:send and target.children.length==2
target, method = target.children
end
if target == nil and method.to_s.start_with? "_"
if args.children.empty?
if method == :_
# Fragment
if send.children.length == 2
process send.updated(:xnode, ['', *process_all(block)])
else
process s(:xnode, 'React.Fragment', *send.children[2..-1],
*process_all(block))
end
else
# append block as a standalone proc
process send.updated(nil, [*send.children, *process_all(block)])
end
else
# iterate over Enumerable arguments if there are args present
send = send.children
return super if send.length < 3
process s(:block, s(:send, *send[0..1], *send[3..-1]),
s(:args), s(:block, s(:send, send[2], :map),
*node.children[1..-1]))
end
else
super
end
end
end
DEFAULTS.push JSX
end
end