-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathvue.rb
1263 lines (1104 loc) · 43.1 KB
/
vue.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# TODO: This feature is deprecated.
require 'ruby2js'
module Ruby2JS
module Filter
module Vue
include SEXP
VUE_METHODS = [
:delete, :destroy, :emit, :forceUpdate, :mount, :nextTick, :off, :on,
:once, :set, :watch
]
VUE_LIFECYCLE = [
:data, :render, :beforeCreate, :created, :beforeMount, :mounted,
:beforeUpdate, :updated, :beforeDestroy, :destroyed
]
VUE_PROPERTIES = [
:$data, :$props, :$el, :$options, :$parent, :$root, :$children,
:$slots, :$scopedSlots, :$refs, :$isServer, :$attrs, :$listeners
]
def initialize(*args)
@vue_class = nil
@vue_h = nil
@vue_self = nil
@vue_apply = nil
@vue_inventory = Hash.new {|h, k| h[k] = []}
@vue_methods = []
@vue_props = []
@vue_reactive = []
@vue_filter_functions = false
@vue_setup = false
super
@exclude_methods << @vue_methods
end
def options=(options)
super
@vue_h ||= options[:vue_h]
filters = options[:filters] || Filter::DEFAULTS
if \
defined? Ruby2JS::Filter::Functions and
filters.include? Ruby2JS::Filter::Functions
then
@vue_filter_functions = true
end
end
# if options[:vue_h] is set, return an array of nodes
def process(node)
return super if @vue_setup
@vue_setup = true
if @vue_h
if node.type == :begin
begin
@vue_apply = true
process s(:send, s(:block, s(:send, nil, :proc),
s(:args, s(:shadowarg, :$_)),
s(:begin, s(:lvasgn, :$_, s(:array)), process(node),
s(:return, s(:gvar, :$_)))), :[])
ensure
@vue_apply = nil
end
else
process s(:array, node)
end
else
process node
end
end
# Example conversion
# before:
# (class (const nil :Foo) (const nil :Vue) nil)
# after:
# (casgn nil :Foo, (send nil, :Vue, :component, (:str, "foo"),
# s(:hash)))
def on_class(node)
cname, inheritance, *body = node.children
begin
vue_class, @vue_class = @vue_class, cname
return super unless cname.children.first == nil
return super unless inheritance == s(:const, nil, :Vue) or
inheritance == s(:const, s(:const, nil, :Vue), :Mixin)
ensure
@vue_class = vue_class
end
# traverse down to actual list of class statements
if body.length == 1
if not body.first
body = []
elsif body.first.type == :begin
body = body.first.children
end
end
hash = []
methods = []
computed = []
setters = []
options = []
watch = nil
el = nil
mixins = []
# insert constructor if none present
if inheritance == s(:const, nil, :Vue)
unless body.any? {|statement|
statement.type == :def and statement.children.first ==:initialize}
then
body = body.dup
body.unshift s(:def, :initialize, s(:args), nil)
end
end
@vue_inventory = vue_walk(node)
@vue_methods.clear
@vue_props = []
@vue_reactive = []
# collect instance methods (including getters and setters) and
# reactive class attributes
body.each do |statement|
if statement.type == :def
method = statement.children.first
unless VUE_LIFECYCLE.include? method or method == :initialize
if method.to_s.end_with? '='
method = method.to_s[0..-2].to_sym
@vue_props << method unless @vue_props.include? method
elsif statement.is_method?
@vue_methods << method unless @vue_methods.include? method
else
@vue_props << method unless @vue_props.include? method
end
end
elsif \
statement.type == :send and statement.children[0] == cname and
statement.children[1].to_s.end_with? '='
then
@vue_reactive << statement.updated(:send, [
s(:attr, s(:const, nil, :Vue), :util), :defineReactive, cname,
s(:sym, statement.children[1].to_s[0..-2]),
process(statement.children[2])])
end
end
# convert body into hash
body.each do |statement|
# named values (template, props, options, mixin[s])
if statement.type == :send and statement.children.first == nil
if [:template, :props].include? statement.children[1]
hash << s(:pair, s(:sym, statement.children[1]),
statement.children[2])
elsif \
statement.children[1] == :options and
statement.children[2].type == :hash
then
options += statement.children[2].children
elsif \
statement.children[1] == :el and
statement.children[2].type == :str
then
el = statement.children[2]
elsif statement.children[1] == :mixin or
statement.children[1] == :mixins
then
mixins += statement.children[2..-1]
end
# methods
elsif statement.type == :def
begin
@vue_self = s(:attr, s(:self), :$data)
method, args, block = statement.children
if method == :render
args = s(:args, s(:arg, :$h)) if args.children.empty?
block = s(:begin, block) unless block and block.type == :begin
if \
(block.children.length != 1 and
not vue_wunderbar_free(block.children[0..-2])) or
not block.children.last or
(block.children.length == 1 and
not [:send, :block].include? block.children.first.type)
then
# wrap multi-line blocks with a 'span' element
block = s(:return,
s(:block, s(:send, nil, :_span), s(:args), *block))
end
@vue_h = args.children.first.children.last
elsif method == :watch and args.children.length == 0 and block.type == :hash
watch = process(block)
next
elsif method == :initialize
method = :data
# find block
if block == nil
block = s(:begin)
elsif block.type != :begin
block = s(:begin, block)
end
simple = block.children.all? {|child| child.type == :ivasgn}
# not so simple if ivars are being read as well as written
if simple
block_inventory = vue_walk(block)
simple = block_inventory[:ivar].empty?
end
uninitialized = @vue_inventory[:ivar].dup
block.children.each do |child|
if child.type == :ivasgn
uninitialized.delete child.children.first
end
end
# convert to a hash
if simple
# simple case: all statements are ivar assignments
pairs = block.children.map do |child|
s(:pair, s(:sym, child.children[0].to_s[1..-1]),
process(child.children[1]))
end
pairs += uninitialized.map do |symbol|
s(:pair, s(:sym, symbol.to_s[1..-1]),
s(:attr, nil, :undefined))
end
next if pairs.empty? and @comments[statement].empty?
block = s(:return, s(:hash, *pairs))
else
# general case: build up a hash incrementally
block = s(:begin, s(:gvasgn, :$_,
s(:hash, *uninitialized.map {|sym|
s(:pair, s(:sym, sym.to_s[1..-1]),
s(:attr, nil, :undefined))})),
block, s(:return, s(:gvar, :$_)))
@vue_self = s(:gvar, :$_)
end
end
if statement.is_method?
method_type = :proc
else
method_type = :lambda
end
# add to hash in the appropriate location
pair = s(:pair, s(:sym, method.to_s.chomp('=')),
s(:block, s(:send, nil, method_type), args, process(block)))
@comments[pair] = @comments[statement]
if VUE_LIFECYCLE.include? method
hash << pair
elsif not statement.is_method?
computed << pair
elsif method.to_s.end_with? '='
setters << pair
else
methods << pair
end
ensure
@vue_h = nil
@vue_self = nil
end
end
end
# add options to the front
hash.unshift(*options)
# add properties before that
unless hash.any? {|pair| pair.children[0].children[0] == :props}
unless @vue_inventory[:cvar].empty?
hash.unshift s(:pair, s(:sym, :props), s(:array,
*@vue_inventory[:cvar].map {|sym| s(:str, sym.to_s[2..-1])}))
end
end
# add mixins before that
unless mixins.empty?
hash.unshift s(:pair, s(:sym, :mixins), s(:array, *mixins))
end
# add el as first, if app has
hash.unshift(s(:pair, s(:sym, :el), el)) if el
# append methods to hash
unless methods.empty?
hash << s(:pair, s(:sym, :methods), s(:hash, *methods))
end
@vue_methods.clear
# append setters to computed list
setters.each do |setter|
index = computed.find_index do |pair|
pair.children[0].children[0].to_s ==
setter.children[0].children[0]
end
if index
computed[index] = s(:pair, setter.children[0],
s(:hash, s(:pair, s(:sym, :get), computed[index].children[1]),
s(:pair, s(:sym, :set), setter.children[1])))
else
computed << s(:pair, setter.children[0],
s(:hash, s(:pair, s(:sym, :set), setter.children[1])))
end
end
# append computed to hash
unless computed.empty?
hash << s(:pair, s(:sym, :computed), s(:hash, *computed))
end
# append watch to hash
if watch
hash << s(:pair, s(:sym, :watch), watch)
end
# convert class name to camel case
cname = cname.children.last
camel = cname.to_s.gsub(/[^\w]/, '-').
sub(/^[A-Z]/) {|c| c.downcase}.
gsub(/[A-Z]/) {|c| "-#{c.downcase}"}
camel = "#{camel}-" if camel =~ /^[a-z]*$/
if inheritance == s(:const, nil, :Vue)
hash_keys = hash.map do |pair|
pair.children[0].children[0].to_s
end
if hash_keys.any? {|key| %w(render template).include? key}
# build component
defn = s(:casgn, nil, cname,
s(:send, s(:const, nil, :Vue), :component,
s(:str, camel), s(:hash, *hash)))
else
# build app
defn = s(:casgn, nil, cname,
s(:send, s(:const, nil, :Vue), :new,
s(:hash, *hash)))
end
else
# build mixin
defn = s(:casgn, nil, cname, s(:hash, *hash))
end
# append class methods (if any)
class_methods = body.select do |statement|
statement.type == :defs and statement.children[0] == s(:self)
end
unless class_methods.empty? and @vue_reactive.empty?
defn = s(:begin, defn, *process_all(class_methods.map {|method|
fn = if method.is_method?
if not method.children[1].to_s.end_with? '='
# class method
s(:send, s(:const, nil, cname), "#{method.children[1]}=",
s(:block, s(:send , nil, :proc), method.children[2],
*process_all(method.children[3..-1])))
else
getter = class_methods.find do |other_method|
"#{other_method.children[1]}=" == method.children[1].to_s
end
if getter
# both a getter and setter
s(:send, s(:const, nil, :Object), :defineProperty,
s(:const, nil, cname), s(:str, getter.children[1].to_s),
s(:hash, s(:pair, s(:sym, :enumerable), s(:true)),
s(:pair, s(:sym, :configurable), s(:true)),
s(:pair, s(:sym, :get), s(:block, s(:send, nil, :proc),
getter.children[2],
s(:autoreturn, process(getter.children[3])))),
s(:pair, s(:sym, :set), s(:block, s(:send, nil, :proc),
method.children[2],
*process_all(method.children[3..-1])))))
else
# setter only
s(:send, s(:const, nil, :Object), :defineProperty,
s(:const, nil, cname),
s(:str, method.children[1].to_s[0..-2]),
s(:hash, s(:pair, s(:sym, :enumerable), s(:true)),
s(:pair, s(:sym, :configurable), s(:true)),
s(:pair, s(:sym, :set), s(:block, s(:send, nil, :proc),
method.children[2],
*process_all(method.children[3..-1])))))
end
end
elsif \
class_methods.any? do |other_method|
other_method.children[1].to_s == "#{method.children[1]}="
end
then
nil
else
# class computed property
s(:send, s(:const, nil, :Object), :defineProperty,
s(:const, nil, cname), s(:str, method.children[1].to_s),
s(:hash, s(:pair, s(:sym, :enumerable), s(:true)),
s(:pair, s(:sym, :configurable), s(:true)),
s(:pair, s(:sym, :get), s(:block, s(:send, nil, :proc),
method.children[2],
s(:autoreturn, *process_all(method.children[3..-1]))))))
end
@comments[fn] = @comments[method]
fn
}).compact, *@vue_reactive)
end
vue_collapse_pushes(defn)
end
# expand 'wunderbar' like method calls
def on_send(node)
if not @vue_h
if node.children.first == s(:const, nil, :Vue)
# enable React filtering within Vue class method calls or
# React component calls
begin
vue_h, @vue_h = @vue_h, [s(:self), :$createElement]
return on_send(node)
ensure
@vue_h = vue_h
end
elsif node.children.first == s(:send, s(:const, nil, :Vue), :util)
if node.children[1] == :defineReactive
if node.children.length == 4 and @vue_class
var = node.children[2]
if var.type == :cvar
scope = @vue_class
var = s(:str, '_' + var.children[0].to_s[2..-1])
elsif var.type == :ivar
scope = s(:self)
var = s(:str, '_' + var.children[0].to_s[1..-1])
elsif var.type == :send and var.children.length == 2
scope = var.children[0]
var = s(:sym, var.children[1])
else
return super
end
return node.updated nil, [*node.children[0..1],
scope, process(var), *process_all(node.children[3..-1])]
end
end
end
end
# map method calls involving i/g/c vars to straight calls
#
# input:
# @x.(a,b,c)
# output:
# @x(a,b,c)
if @vue_self and node.children[1] == :call
if [:ivar, :gvar, :cvar].include? node.children.first.type
return process(s(:send, node.children.first, nil,
*node.children[2..-1]))
else
return super
end
end
# calls to methods (including getters) defined in this class
if node.children[0]==nil and Symbol === node.children[1]
if node.is_method?
if @vue_methods.include? node.children[1]
# calls to methods defined in this class
return node.updated nil, [s(:self), node.children[1],
*process_all(node.children[2..-1])]
end
else
if @vue_props.include? node.children[1]
# access to properties defined in this class
return node.updated nil, [s(:self), node.children[1],
*process_all(node.children[2..-1])]
end
end
end
return super unless @vue_h
if node.children[0] == nil and node.children[1] =~ /^_\w/
tag = node.children[1].to_s[1..-1]
hash = Hash.new {|h, k| h[k] = {}}
args = []
complex_block = []
component = (tag =~ /^[A-Z]/)
node.children[2..-1].each do |attr|
if attr.type == :hash
# attributes
# https://github.com/vuejs/babel-plugin-transform-vue-jsx#difference-from-react-jsx
pairs = attr.children.dup
# extract all class names
classes = pairs.find_all do |pair|
key = pair.children.first.children.first
[:class, 'class', :className, 'className'].include? key
end
# combine all classes into a single value (or expression)
if classes.length > 0
expr = nil
values = classes.map do |pair|
if [:sym, :str].include? pair.children.last.type
pair.children.last.children.first.to_s
else
expr = pair.children.last
''
end
end
pairs -= classes
if expr
if values.length > 1
while expr.type == :begin and expr.children.length == 1
expr = expr.children.first
end
if expr.type == :array
hash[:class] = s(:array, *expr.children,
*values.join(' ').split(' ').map {|str| s(:str, str)})
elsif expr.type == :hash
hash[:class] = s(:hash, *expr.children,
*values.join(' ').split(' ').
map {|str| s(:pair, s(:str, str), s(:true))})
else
if \
expr.type == :if and expr.children[1] and
expr.children[1].type == :str
then
left = expr.children[1]
right = expr.children[2] || s(:nil)
expr = expr.updated(nil,
[expr.children[0], left, right])
end
hash[:class] = s(:array,
*values.join(' ').split(' ').map {|str| s(:str, str)},
expr)
end
elsif [:hash, :array].include? expr.type
hash[:class] = expr
else
hash[:class] = s(:array, expr)
end
else
hash[:class] = s(:array,
*values.join(' ').split(' ').map {|str| s(:str, str)})
end
end
# search for the presence of a 'style' attribute
style = pairs.find_index do |pair|
['style', :style].include? pair.children.first.children.first
end
# converts style strings into style hashes
if style and pairs[style].children[1].type == :str
rules = []
value = pairs[style].children[1].children[0]
value.split(/;\s+/).each do |prop|
prop.strip!
next unless prop =~ /^([-a-z]+):\s*(.*)$/
name, value = $1, $2
name.gsub!(/-[a-z]/) {|str| str[1].upcase}
if value =~ /^-?\d+$/
rules << s(:pair, s(:str, name), s(:int, value.to_i))
elsif value =~ /^-?\d+$\.\d*/
rules << s(:pair, s(:str, name), s(:float, value.to_f))
else
rules << s(:pair, s(:str, name), s(:str, value))
end
end
pairs.delete_at(style)
hash[:style] = s(:hash, *rules)
end
# process remaining attributes
pairs.each do |pair|
name = pair.children[0].children[0].to_s
if name =~ /^(nativeOn|on)([A-Z])(.*)/
hash[$1]["#{$2.downcase}#$3"] = pair.children[1]
elsif component
hash[:props][name] = pair.children[1]
elsif name =~ /^domProps([A-Z])(.*)/
hash[:domProps]["#{$1.downcase}#$2"] = pair.children[1]
elsif name == 'style' and pair.children[1].type == :hash
hash[:style] = pair.children[1]
elsif %w(key ref refInFor slot).include? name
hash[name] = pair.children[1]
else
hash[:attrs][name.to_s.gsub('_', '-')] = pair.children[1]
end
end
elsif attr.type == :block
# traverse down to actual list of nested statements
statements = attr.children[2..-1]
if statements.length == 1
if not statements.first
statements = []
elsif statements.first.type == :begin
statements = statements.first.children
end
end
# check for normal case: only elements and text
simple = statements.all? {|arg| vue_element?(arg)}
if simple
args << s(:array, *statements)
else
complex_block += statements
end
else
# text or child elements
args << node.children[2]
end
end
# support controlled form components
if %w(input select textarea).include? tag
# search for the presence of a 'value' attribute
value = hash[:attrs]['value']
# search for the presence of a 'onChange' attribute
if value
onChange = hash['on']['input'] ||
hash['on']['change'] ||
hash['nativeOn']['input'] ||
hash['nativeOn']['change']
attr = 'value'
if hash[:attrs]['type'] == s(:str, 'file')
event = 'change'
else
event = 'input'
end
end
# search for the presence of a 'onClick' attribute
if tag == 'input' and hash[:attrs]['checked']
value = hash[:attrs]['checked']
onChange = hash['on']['click'] ||
hash['nativeOn']['click']
attr = 'checked'
event = 'click'
end
# test if value is assignable
test = value
loop do
break unless test and test.type == :send
break unless (test.children.length == 2 and
test.children.last.instance_of? Symbol and
not [:not, :!, :*, :+, :-].include? test.children.last) or
test.children[1] == :[]
test = test.children.first
end
if value and value.type != :cvar and (not test or
test.is_a? Symbol or [:ivar, :cvar, :self].include? test.type)
then
hash[:domProps][attr] ||= value
args.push value if tag == 'textarea' and args.empty?
hash[:attrs].delete(attr)
# disable control until script is ready
unless hash[:domProps]['disabled'] or hash[:attrs]['disabled']
hash[:domProps]['disabled'] = s(:false)
hash[:attrs]['disabled'] = s(:true)
end
# define event handler to update ivar on input events
if not onChange
if attr == 'value'
update = s(:attr, s(:attr, s(:lvar, :event), :target), :value)
upargs = [s(:arg, :event)]
else
update = s(:send, value, :!)
upargs = []
end
if value.type == :ivar
assign = s(:ivasgn, value.children.first, update)
elsif value.type == :cvar
assign = s(:cvasgn, value.children.first, update)
elsif value.type == :send and value.children.first == nil
assign = value.updated :lvasgn, [value.children[1], update]
elsif value.children[1] == :[]
assign = value.updated nil, [value.children[0], :[]=,
value.children[2], update]
else
assign = value.updated nil, [value.children.first,
"#{value.children[1]}=", update]
end
hash['on'][event] ||=
s(:block, s(:send, nil, :proc), s(:args, *upargs), assign)
end
end
end
# put attributes up front
unless hash.empty?
pairs = hash.to_a.map do |k1, v1|
next if Hash === v1 and v1.empty?
s(:pair, s(:str, k1.to_s),
if Parser::AST::Node === v1
v1
else
s(:hash, *v1.map {|k2, v2| s(:pair, s(:str, k2.to_s), v2)})
end
)
end
args.unshift s(:hash, *pairs.compact)
end
# prepend element name
if component
args.unshift s(:const, nil, tag)
else
args.unshift s(:str, tag)
end
begin
vue_apply = @vue_apply
if complex_block.empty?
@vue_apply = false
# emit $h (createElement) call
if @vue_h.instance_of? Array
element = node.updated :send, [*@vue_h, *process_all(args)]
else
element = node.updated :send, [nil, @vue_h, *process_all(args)]
end
else
# calls to $h (createElement) which contain a block
#
# collect array of child elements in a proc, and call that proc
#
# $h('tag', hash, proc {
# var $_ = []
# $_.push($h(...))
# return $_
# }())
#
@vue_apply = false
# gather non-element emitting statements in the front
prolog = []
while not complex_block.empty? and
vue_wunderbar_free([complex_block.first]) do
prolog << process(complex_block.shift)
end
# gather element emitting statements in the front
prefix = []
while vue_element?(complex_block.first) do
prefix << process(complex_block.shift)
end
# gather element emitting statements in the back
suffix = []
while vue_element?(complex_block.last) do
suffix.unshift process(complex_block.pop)
end
result = s(:lvar, :$_)
unless suffix.empty?
result = s(:send, result, :concat, s(:array, *suffix))
end
@vue_apply = true
if suffix.empty? and complex_block.empty?
element = node.updated :send, [nil, @vue_h,
*process_all(args),
s(:send, s(:block, s(:send, nil, :proc),
s(:args), s(:begin, *prolog,
s(:return, s(:array, *prefix)))), :[])]
else
element = node.updated :send, [nil, @vue_h,
*process_all(args),
s(:send, s(:block, s(:send, nil, :proc),
s(:args, s(:shadowarg, :$_)), s(:begin, *prolog,
s(:lvasgn, :$_, s(:array, *prefix)),
*process_all(complex_block),
s(:return, result))), :[])]
end
end
ensure
@vue_apply = vue_apply
end
if @vue_apply
# if apply is set, emit code that pushes result
s(:send, s(:gvar, :$_), :push, element)
else
element
end
elsif node.children[0] == nil and node.children[1] == :_
# text nodes
# https://stackoverflow.com/questions/42414627/create-text-node-with-custom-render-function-in-vue-js
text = s(:send, s(:self), :_v, process(node.children[2]))
if @vue_apply
# if apply is set, emit code that pushes text
s(:send, s(:gvar, :$_), :push, text)
else
# simple/normal case: simply return the text
text
end
elsif node.children[0]==s(:send, nil, :_) and node.children[1]==:[]
if @vue_apply
# if apply is set, emit code that pushes results
s(:send, s(:gvar, :$_), :push, *process_all(node.children[2..-1]))
elsif node.children.length == 3
process(node.children[2])
else
# simple/normal case: simply return the element
s(:splat, s(:array, *process_all(node.children[2..-1])))
end
elsif \
node.children[1] == :createElement and
node.children[0] == s(:const, nil, :Vue)
then
# explicit calls to Vue.createElement
if @vue_h.instance_of? Array
element = node.updated nil, [*@vue_h,
*process_all(node.children[2..-1])]
else
element = node.updated nil, [nil, @vue_h,
*process_all(node.children[2..-1])]
end
if @vue_apply
# if apply is set, emit code that pushes result
s(:send, s(:gvar, :$_), :push, element)
else
element
end
elsif \
@vue_self and VUE_METHODS.include? node.children[1] and
node.children[0] == s(:const, nil, :Vue)
then
# vm methods
node.updated nil, [s(:self), "$#{node.children[1]}",
*process_all(node.children[2..-1])]
elsif node.children[0] and node.children[0].type == :send
# determine if markaby style class and id names are being used
child = node
test = child.children.first
while test and test.type == :send and not test.is_method?
child, test = test, test.children.first
end
if child.children[0] == nil and child.children[1] =~ /^_\w/
# capture the arguments provided on the current node
children = node.children[2..-1]
# convert method calls to id and class values
while node != child
if node.children[1] !~ /!$/
# convert method name to hash {class: name} pair
pair = s(:pair, s(:sym, :class),
s(:str, node.children[1].to_s.gsub('_','-')))
else
# convert method name to hash {id: name} pair
pair = s(:pair, s(:sym, :id),
s(:str, node.children[1].to_s[0..-2].gsub('_','-')))
end
# if a hash argument is already passed, merge in id value
hash = children.find_index {|cnode| cnode.type == :hash}
if hash
children[hash] = s(:hash, pair, *children[hash].children)
else
children << s(:hash, pair)
end
# advance to next node
node = node.children.first
end
# collapse series of method calls into a single call
return process(node.updated(nil, [*node.children[0..1], *children]))
else
super
end
else
super
end
end
# collapse consecutive pushes into a single call to push
def vue_collapse_pushes(node)
if node.type == :begin
prev = nil
(node.children.length-1).downto(0) do |i|
child = node.children[i]
if \
child.type == :send and child.children[0] == s(:gvar, :$_) and
child.children[1] == :push
then
if prev
node = node.updated(nil, [*node.children[0...i],
node.children[i].updated(nil, node.children[i].children +
node.children[i+1].children[2..-1]),
*node.children[prev+1..-1]])
end
prev = i
else
prev = nil
end
end
end
# recurse
children = node.children
children.each_with_index do |child, index|
if Parser::AST::Node === child
replacement = vue_collapse_pushes(child)
unless replacement.equal? child
children = children.dup if children.frozen?
children[index] = replacement
end
end
end
node = node.updated(nil, children) unless children.frozen?
node
end
# convert blocks to proc arguments
def on_block(node)
child = node.children.first
# map Vue.render(el, &block) to Vue.new(el: el, render: block)
if \
child.children[1] == :render and
child.children[0] == s(:const, nil, :Vue)
then
begin
arg = node.children[1].children[0] || s(:arg, :$h)
vue_h, @vue_h = @vue_h, arg.children.first
block = node.children[2]
block = s(:begin, block) unless block and block.type == :begin
if \
block.children.length != 1 or not block.children.last or
not [:send, :block].include? block.children.first.type
then
# wrap multi-line blocks with a 'span' element
block = s(:return,
s(:block, s(:send, nil, :_span), s(:args), *block))
end