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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
# File 'lib/ruby2js/filter/stimulus.rb', line 34
def on_class(node)
cname, inheritance, *body = node.children
return super unless inheritance == s(:const, nil, :Stimulus) or
inheritance == s(:const, s(:const, nil, :Stimulus), :Controller) or
inheritance == s(:send, s(:const, nil, :Stimulus), :Controller) or
@stim_subclasses.include? @namespace.resolve(inheritance)
if inheritance == s(:const, nil, :Stimulus)
node = node.updated(nil, [node.children.first,
s(:const, s(:const, nil, :Stimulus), :Controller),
*node.children[2..-1]])
end
@stim_subclasses << @stim_scope + @namespace.resolve(cname)
@stim_targets = Set.new
@stim_values = Set.new
@stim_classes = Set.new
stim_walk(node)
if modules_enabled?
prepend_list << (@options[:import_from_skypack] ?
STIMULUS_IMPORT_SKYPACK : STIMULUS_IMPORT)
end
nodes = body
if nodes.length == 1 and nodes.first&.type == :begin
nodes = nodes.first.children.dup
end
unless @stim_classes.empty?
classes = nodes.find_index {|child|
child.type == :send and child.children[0..1] == [s(:self), :classes=]
}
if classes == nil
nodes.unshift s(:send, s(:self), :classes=, s(:array, *@stim_classes))
elsif nodes[classes].children[2].type == :array
@stim_classes.merge(nodes[classes].children[2].children)
nodes[classes] = nodes[classes].updated(nil,
[*nodes[classes].children[0..1], s(:array, *@stim_classes)])
end
end
unless @stim_values.empty?
values = nodes.find_index {|child|
child.type == :send and child.children[0..1] == [s(:self), :values=]
}
if values == nil
nodes.unshift s(:send, s(:self), :values=, s(:hash,
*@stim_values.map {|name| s(:pair, name, s(:const, nil, :String))}))
elsif nodes[values].children[2].type == :hash
stim_values = @stim_values.map {|name|
[s(:sym, name.children.first.to_sym), s(:const, nil, :String)]
}.to_h.merge(
nodes[values].children[2].children.map {|pair| pair.children}.to_h
)
nodes[values] = nodes[values].updated(nil,
[*nodes[values].children[0..1], s(:hash,
*stim_values.map{|name, value| s(:pair, name, value)})])
end
end
unless @stim_targets.empty?
targets = nodes.find_index {|child|
child.type == :send and child.children[0..1] == [s(:self), :targets=]
}
if targets == nil
nodes.unshift s(:send, s(:self), :targets=, s(:array, *@stim_targets))
elsif nodes[targets].children[2].type == :array
@stim_targets.merge(nodes[targets].children[2].children)
nodes[targets] = nodes[targets].updated(nil,
[*nodes[targets].children[0..1], s(:array, *@stim_targets)])
end
end
props = [:element, :application]
props += @stim_targets.map do |name|
name = name.children.first
["#{name}Target", "#{name}Targets", "has#{name[0].upcase}#{name[1..-1]}Target"]
end
props += @stim_values.map do |name|
name = name.children.first
["#{name}Value", "has#{name[0].upcase}#{name[1..-1]}Value"]
end
props += @stim_classes.map do |name|
name = name.children.first
["#{name}Class", "has#{name[0].upcase}#{name[1..-1]}Class"]
end
props = props.flatten.map {|prop| [prop.to_sym, s(:self)]}.to_h
props[:initialize] = s(:autobind, s(:self))
nodes.unshift s(:defineProps, props)
nodes.pop unless nodes.last
node.updated(nil, [*node.children[0..1], s(:begin, *process_all(nodes))])
end
|