Module: Ruby2JS::Filter::LitElement
Constant Summary collapse
- LITELEMENT_IMPORT =
s(:import, [s(:pair, s(:sym, :from), s(:str, "lit-element"))], [s(:const, nil, :LitElement), s(:attr, nil, :css), s(:attr, nil, :html)])
Instance Method Summary collapse
- #html_wrap(node) ⇒ Object
- #initialize(node) ⇒ Object
-
#le_walk(node) ⇒ Object
analyze ivar usage.
- #on_class(node) ⇒ Object
- #on_ivar(node) ⇒ Object
- #on_ivasgn(node) ⇒ Object
- #on_op_asgn(node) ⇒ Object
Methods included from SEXP
Instance Method Details
#html_wrap(node) ⇒ Object
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 |
# File 'lib/ruby2js/filter/lit-element.rb', line 139 def html_wrap(node) if node.type == :str and node.children.first.strip.start_with? '<' s(:taglit, s(:sym, :html), s(:dstr, node)) elsif node.type == :dstr prefix = '' node.children.each do |child| break unless child.type == :str prefix += child.children.first end return node unless prefix.strip.start_with? '<' children = node.children.map do |child| if child.type == :str child else html_wrap(child) end end while children.length > 1 and children.last.type == :str and children.last.children.last.strip == '' children.pop end if children.last.type == :str children << s(:str, children.pop.children.first.chomp) end s(:taglit, s(:sym, :html), node.updated(nil, children)) elsif node.type == :begin node.updated(nil, node.children.map {|child| html_wrap(child)}) elsif node.type == :if node.updated(nil, [node.children.first, *node.children[1..2].map {|child| html_wrap(child)}]) elsif node.type == :block and node.children.first.children[1] == :map node.updated(nil, [*node.children[0..1], html_wrap(node.children[2])]) else node end end |
#initialize(node) ⇒ Object
13 14 15 16 |
# File 'lib/ruby2js/filter/lit-element.rb', line 13 def initialize(node) super @le_props = nil end |
#le_walk(node) ⇒ Object
analyze ivar usage
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 |
# File 'lib/ruby2js/filter/lit-element.rb', line 184 def le_walk(node) node.children.each do |child| next unless child.is_a? Parser::AST::Node if child.type == :ivar @le_props[child.children.first] ||= nil elsif child.type == :ivasgn || child.type == :op_asgn prop = child.children.first unless prop.is_a? Symbol prop = prop.children.first if prop.type == :ivasgn next unless prop.is_a? Symbol end @le_props[prop] = case child.children.last.type when :str, :dstr :String when :array :Array when :int, :float :Number when :true, :false :Boolean else @le_props[prop] || :Object end else le_walk(child) end end end |
#on_class(node) ⇒ Object
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 |
# File 'lib/ruby2js/filter/lit-element.rb', line 38 def on_class(node) cname, inheritance, *body = node.children return super unless inheritance == s(:const, nil, :LitElement) @le_props = {} le_walk(node) prepend_list << LITELEMENT_IMPORT if modules_enabled? nodes = body.dup if nodes.length == 1 and nodes.first&.type == :begin nodes = nodes.first.children.dup end # insert/update static get properties() {} unless @le_props.empty? values = nodes.find_index {|child| child.type == :defs and child.children[0..1] == [s(:self), :properties] } if values == nil nodes.unshift s(:defp, s(:self), :properties, s(:args), s(:return, s(:hash, *@le_props.map {|name, type| s(:pair, s(:str, name.to_s[1..-1]), s(:hash, s(:pair, s(:sym, :type), s(:const, nil, type || :String))))}))) elsif nodes[values].children[3].type == :hash le_props = @le_props.map {|name, type| [s(:sym, name.to_s[1..-1].to_sym), s(:hash, s(:pair, s(:sym, :type), s(:const, nil, type || :String)))] }.to_h.merge( nodes[values].children[3].children.map {|pair| pair.children}.to_h ) nodes[values] = nodes[values].updated(nil, [*nodes[values].children[0..2], s(:hash, *le_props.map{|name, value| s(:pair, name, value)})]) end end # render of a string is converted to a taglit :html render = nodes.find_index {|child| child&.type == :def and child.children.first == :render } if render and %i[str dstr].include?(nodes[render].children[2]&.type) nodes[render] = nodes[render].updated(:deff, [*nodes[render].children[0..1], s(:autoreturn, html_wrap(nodes[render].children[2]))]) end # self.styles returning string is converted to a taglit :css styles = nodes.find_index {|child| child&.type == :defs and child.children[0..1] == [s(:self), :styles] } if styles and %i[str dstr].include?(nodes[styles].children[3]&.type) string = nodes[styles].children[3] string = s(:dstr, string) if string.type == :str children = string.children.dup while children.length > 1 and children.last.type == :str and children.last.children.last.strip == '' children.pop end if children.last.type == :str children << s(:str, children.pop.children.first.chomp) end nodes[styles] = nodes[styles].updated(nil, [*nodes[styles].children[0..2], s(:autoreturn, s(:taglit, s(:sym, :css), s(:dstr, *children)))]) end # insert super calls into initializer initialize = nodes.find_index {|child| child&.type == :def and child.children.first == :initialize } if initialize and nodes[initialize].children.length == 3 statements = nodes[initialize].children[2..-1] if statements.length == 1 and statements.first.type == :begin statements = statements.first.children end unless statements.any? {|statement| %i[super zuper].include? statement.type} nodes[initialize] = nodes[initialize].updated(nil, [*nodes[initialize].children[0..1], s(:begin, s(:zsuper), *statements)]) end end props = @le_props.keys.map {|prop| [prop.to_sym, s(:self)]}.to_h nodes.unshift s(:defineProps, props) nodes.pop unless nodes.last node.updated(nil, [*node.children[0..1], s(:begin, *process_all(nodes))]) ensure @le_props = nil end |
#on_ivar(node) ⇒ Object
18 19 20 21 |
# File 'lib/ruby2js/filter/lit-element.rb', line 18 def on_ivar(node) return super unless @le_props&.include?(node.children.first) s(:attr, s(:self), node.children.first.to_s[1..-1]) end |
#on_ivasgn(node) ⇒ Object
23 24 25 26 27 28 |
# File 'lib/ruby2js/filter/lit-element.rb', line 23 def on_ivasgn(node) return super unless @le_props&.include?(node.children.first) return super unless node.children.length > 1 s(:send, s(:self), node.children.first.to_s[1..-1]+'=', process(node.children[1])) end |
#on_op_asgn(node) ⇒ Object
30 31 32 33 34 35 36 |
# File 'lib/ruby2js/filter/lit-element.rb', line 30 def on_op_asgn(node) return super unless node.children.first.type == :ivasgn var = node.children.first.children.first return super unless @le_props&.include?(var) super node.updated(nil, [s(:attr, s(:attr, nil, :this), var.to_s[1..-1]), *node.children[1..-1]]) end |