Module: Ruby2JS::Filter::Vue
- Includes:
- SEXP
- Defined in:
- lib/ruby2js/filter/vue.rb
Constant Summary collapse
- 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 ]
Instance Method Summary collapse
- #initialize(*args) ⇒ Object
-
#on_block(node) ⇒ Object
convert blocks to proc arguments.
-
#on_class(node) ⇒ Object
Example conversion before: (class (const nil :Foo) (const nil :Vue) nil) after: (casgn nil :Foo, (send nil, :Vue, :component, (:str, “foo”), s(:hash))).
-
#on_cvar(node) ⇒ Object
expand @@ to self.
-
#on_cvasgn(node) ⇒ Object
prevent attempts to assign to Vue properties.
-
#on_for_of(node) ⇒ Object
convert for_of back to forEach for conversion to map.
-
#on_gvar(node) ⇒ Object
expand instance properties like $options to this.$options.
-
#on_ivar(node) ⇒ Object
expand @ to @vue_self.
-
#on_ivasgn(node) ⇒ Object
expand @= to @vue_self.=.
-
#on_lvasgn(node) ⇒ Object
for computed variables with setters, map x= to this.x=.
-
#on_op_asgn(node) ⇒ Object
for instance variables, map @x+= to this.x+= for computed variables with setters, map x+= to this.x+=.
-
#on_pair(node) ⇒ Object
instance methods as hash values (e.g., onClick: method).
-
#on_send(node) ⇒ Object
expand ‘wunderbar’ like method calls.
- #options=(options) ⇒ Object
-
#process(node) ⇒ Object
if options is set, return an array of nodes.
-
#vue_collapse_pushes(node) ⇒ Object
collapse consecutive pushes into a single call to push.
-
#vue_element?(node) ⇒ Boolean
is this a “wunderbar” style call or createElement?.
-
#vue_walk(node, inventory = Hash.new {|h, k| h[k] = []}) ⇒ Object
gather ivar and cvar usage.
-
#vue_wunderbar_free(nodes) ⇒ Object
ensure that there are no “wunderbar” or “createElement” calls in a set of statements.
Methods included from SEXP
Instance Method Details
#initialize(*args) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/ruby2js/filter/vue.rb', line 25 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 |
#on_block(node) ⇒ Object
convert blocks to proc arguments
978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 |
# File 'lib/ruby2js/filter/vue.rb', line 978 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 return node.updated :send, [child.children[0], :new, s(:hash, s(:pair, s(:sym, :el), process(child.children[2])), s(:pair, s(:sym, :render), s(:block, s(:send, nil, :lambda), s(:args, s(:arg, @vue_h)), process(block))))] ensure @vue_h = vue_h end end return super unless @vue_h if \ child.children[1] == :createElement and child.children[0] == s(:const, nil, :Vue) then # block calls to Vue.createElement # # collect array of child elements in a proc, and call that proc # # $h('tag', hash, proc { # var $_ = [] # $_.push($h(...)) # return $_ # }()) # begin vue_apply, @vue_apply = @vue_apply, true element = node.updated :send, [nil, @vue_h, *child.children[2..-1], s(:send, s(:block, s(:send, nil, :proc), s(:args, s(:shadowarg, :$_)), s(:begin, s(:lvasgn, :$_, s(:array)), process(node.children[2]), s(:return, s(:lvar, :$_)))), :[])] ensure @vue_apply = vue_apply end if @vue_apply # if apply is set, emit code that pushes result return s(:send, s(:gvar, :$_), :push, element) else return element end end # traverse through potential "css proxy" style method calls child = node.children.first test = child.children.first while test and test.type == :send and not test.is_method? child, test = test, test.children.first end # wunderbar style calls if child.children[0] == nil and child.children[1] =~ /^_\w/ if node.children[1].children.empty? # append block as a standalone proc block = s(:block, s(:send, nil, :proc), s(:args), *node.children[2..-1]) return on_send node.children.first.updated(:send, [*node.children.first.children, block]) else # iterate over Enumerable arguments if there are args present send = node.children.first.children return super if send.length < 3 return process s(:block, s(:send, *send[0..1], *send[3..-1]), s(:args), s(:block, s(:send, send[2], :forEach), *node.children[1..-1])) end elsif \ node.children.first.children.last == :forEach and vue_element?(node.children.last) then # map forEach to map map = node.children.first.updated(nil, [*node.children.first.children[0..-2], :map]) node = node.updated(nil, [map, *node.children[1..-2], s(:autoreturn, node.children[-1])]) begin vue_apply, @vue_apply = @vue_apply, false if vue_apply s(:send, s(:gvar, :$_), :push, s(:splat, process(node))) else s(:splat, process(node)) end ensure @vue_apply = vue_apply end else super end end |
#on_class(node) ⇒ Object
Example conversion
before:
(class (const nil :Foo) (const nil :Vue) nil)
after:
(casgn nil :Foo, (send nil, :Vue, :component, (:str, "foo"),
s(:hash)))
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 |
# File 'lib/ruby2js/filter/vue.rb', line 85 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 = [] = [] 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 += 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 (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(*) # 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 |
#on_cvar(node) ⇒ Object
expand @@ to self
1111 1112 1113 1114 |
# File 'lib/ruby2js/filter/vue.rb', line 1111 def on_cvar(node) return super unless @vue_self s(:attr, s(:attr, s(:self), :$props), node.children[0].to_s[2..-1]) end |
#on_cvasgn(node) ⇒ Object
prevent attempts to assign to Vue properties
1127 1128 1129 1130 |
# File 'lib/ruby2js/filter/vue.rb', line 1127 def on_cvasgn(node) return super unless @vue_self raise Error.new("setting a Vue property", node) end |
#on_for_of(node) ⇒ Object
convert for_of back to forEach for conversion to map
1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 |
# File 'lib/ruby2js/filter/vue.rb', line 1098 def on_for_of(node) if vue_element?(node.children.last) process node.updated(:block, [ s(:send, node.children[1], :forEach), s(:args, s(:arg, node.children.first.children.last)), node.children.last ]) else super end end |
#on_gvar(node) ⇒ Object
expand instance properties like $options to this.$options
1117 1118 1119 1120 1121 1122 1123 1124 |
# File 'lib/ruby2js/filter/vue.rb', line 1117 def on_gvar(node) return super unless @vue_self if VUE_PROPERTIES.include? node.children[0] node.updated :attr, [s(:self), node.children[0]] else super end end |
#on_ivar(node) ⇒ Object
expand @ to @vue_self
1133 1134 1135 1136 |
# File 'lib/ruby2js/filter/vue.rb', line 1133 def on_ivar(node) return super unless @vue_self s(:attr, @vue_self, node.children[0].to_s[1..-1]) end |
#on_ivasgn(node) ⇒ Object
expand @= to @vue_self.=
1139 1140 1141 1142 1143 1144 1145 1146 1147 |
# File 'lib/ruby2js/filter/vue.rb', line 1139 def on_ivasgn(node) return super unless @vue_self if node.children.length == 1 s(:attr, @vue_self, "#{node.children[0].to_s[1..-1]}") else s(:send, @vue_self, "#{node.children[0].to_s[1..-1]}=", process(node.children[1])) end end |
#on_lvasgn(node) ⇒ Object
for computed variables with setters, map x= to this.x=
1172 1173 1174 1175 1176 |
# File 'lib/ruby2js/filter/vue.rb', line 1172 def on_lvasgn(node) return super unless @vue_props.include? node.children.first s(:send, s(:self), "#{node.children.first}=", process(node.children[1])) end |
#on_op_asgn(node) ⇒ Object
for instance variables, map @x+= to this.x+= for computed variables with setters, map x+= to this.x+=
1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 |
# File 'lib/ruby2js/filter/vue.rb', line 1151 def on_op_asgn(node) return super unless @vue_self if node.children.first.type == :ivasgn node.updated nil, [s(:attr, @vue_self, node.children[0].children[0].to_s[1..-1]), node.children[1], process(node.children[2])] elsif \ node.children.first.type == :lvasgn and @vue_props.include? node.children[0].children[0] then node.updated nil, [s(:attr, s(:self), node.children[0].children[0]), node.children[1], process(node.children[2])] else super end end |
#on_pair(node) ⇒ Object
instance methods as hash values (e.g., onClick: method)
1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 |
# File 'lib/ruby2js/filter/vue.rb', line 1179 def on_pair(node) key, value = node.children return super unless Parser::AST::Node === value return super unless value.type == :send and value.children.length == 2 and value.children[0] == nil and @vue_methods.include? value.children[1] node.updated nil, [process(key), value.updated(nil, [s(:self), value.children[1]])] end |
#on_send(node) ⇒ Object
expand ‘wunderbar’ like method calls
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 |
# File 'lib/ruby2js/filter/vue.rb', line 444 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 ([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 |
#options=(options) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/ruby2js/filter/vue.rb', line 43 def () super @vue_h ||= [:vue_h] filters = [:filters] || Filter::DEFAULTS if \ defined? Ruby2JS::Filter::Functions and filters.include? Ruby2JS::Filter::Functions then @vue_filter_functions = true end end |
#process(node) ⇒ Object
if options is set, return an array of nodes
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/ruby2js/filter/vue.rb', line 57 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 |
#vue_collapse_pushes(node) ⇒ Object
collapse consecutive pushes into a single call to push
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 |
# File 'lib/ruby2js/filter/vue.rb', line 938 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 |
#vue_element?(node) ⇒ Boolean
is this a “wunderbar” style call or createElement?
1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 |
# File 'lib/ruby2js/filter/vue.rb', line 1191 def vue_element?(node) return false unless node forEach = [:forEach] forEach << :each if @vue_filter_functions return true if node.type == :block and forEach.include? node.children.first.children.last and vue_element?(node.children.last) # explicit call to Vue.createElement return true if node.children[1] == :createElement and node.children[0] == s(:const, nil, :Vue) # wunderbar style call node = node.children.first if node.type == :block while node.type == :send and node.children.first != nil node = node.children.first end node.type == :send and node.children[1].to_s.start_with? '_' end |
#vue_walk(node, inventory = Hash.new {|h, k| h[k] = []}) ⇒ Object
gather ivar and cvar usage
1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 |
# File 'lib/ruby2js/filter/vue.rb', line 1230 def vue_walk(node, inventory = Hash.new {|h, k| h[k] = []}) # extract ivars and cvars if [:ivar, :cvar].include? node.type symbol = node.children.first unless inventory[node.type].include? symbol inventory[node.type] << symbol end elsif node.type == :ivasgn symbol = nil symbol = node.children.first if node.children.length == 1 if node.children.length == 2 value = node.children[-1] symbol = value.children.first if value.type == :ivasgn end if symbol unless inventory[:ivar].include? symbol inventory[:ivar] << symbol end end end # recurse node.children.each do |child| vue_walk(child, inventory) if Parser::AST::Node === child end return inventory end |
#vue_wunderbar_free(nodes) ⇒ Object
ensure that there are no “wunderbar” or “createElement” calls in a set of statements.
1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 |
# File 'lib/ruby2js/filter/vue.rb', line 1215 def (nodes) nodes.each do |node| if Parser::AST::Node === node return false if vue_element?(node) # recurse return false unless (node.children) end end # no problems found return true end |