-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathstimulus.rb
187 lines (151 loc) · 6.18 KB
/
stimulus.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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#
require 'ruby2js'
module Ruby2JS
module Filter
module Stimulus
include SEXP
extend SEXP
STIMULUS_IMPORT = s(:import,
[s(:pair, s(:sym, :as), s(:const, nil, :Stimulus)),
s(:pair, s(:sym, :from), s(:str, "@hotwired/stimulus"))],
s(:str, '*'))
STIMULUS_IMPORT_SKYPACK = s(:import,
[s(:pair, s(:sym, :as), s(:const, nil, :Stimulus)),
s(:pair, s(:sym, :from), s(:str, "https://cdn.skypack.dev/@hotwired/stimulus"))],
s(:str, '*'))
def initialize(*args)
super
@stim_scope = []
@stim_subclasses = []
end
def on_module(node)
save_scope = @stim_scope
@stim_scope += @namespace.resolve(node.children.first)
super
ensure
@stim_scope = save_scope
end
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
# analyze ivar usage
def stim_walk(node)
node.children.each do |child|
next unless Parser::AST::Node === child
stim_walk(child)
if child.type == :send and child.children.length == 2 and
[nil, s(:self), s(:send, nil, :this)].include? child.children[0]
if child.children[1] =~ /^has([A-Z]\w*)(Target|Value|Class)$/
name = s(:str, $1[0].downcase + $1[1..-1])
@stim_targets.add name if $2 == 'Target'
@stim_values.add name if $2 == 'Value'
@stim_classes.add name if $2 == 'Class'
elsif child.children[1] =~ /^(\w+)Targets?$/
@stim_targets.add s(:str, $1)
elsif child.children[1] =~ /^(\w+)Value=?$/
@stim_values.add s(:str, $1)
elsif child.children[1] =~ /^(\w+)Class$/
@stim_classes.add s(:str, $1)
end
elsif child.type == :send and child.children.length == 3 and
[s(:self), s(:send, nil, :this)].include? child.children[0]
if child.children[1] =~ /^(\w+)Value=$/
@stim_values.add s(:str, $1)
end
elsif child.type == :lvasgn
if child.children[0] =~ /^(\w+)Value$/
@stim_values.add s(:str, $1)
end
elsif child.type == :def
if child.children[0] =~ /^(\w+)ValueChanged$/
@stim_values.add s(:str, $1)
end
end
end
end
end
DEFAULTS.push Stimulus
end
end