Module: Ruby2JS::Filter::ESM
- Includes:
- SEXP
- Defined in:
- lib/ruby2js/filter/esm.rb
Instance Method Summary collapse
- #on_class(node) ⇒ Object
- #on_const(node) ⇒ Object
- #on_def(node) ⇒ Object
- #on_export(node) ⇒ Object
- #on_lvasgn(node) ⇒ Object
- #on_send(node) ⇒ Object
- #options=(options) ⇒ Object
- #process(node) ⇒ Object
Methods included from SEXP
Instance Method Details
#on_class(node) ⇒ Object
79 80 81 82 83 |
# File 'lib/ruby2js/filter/esm.rb', line 79 def on_class(node) @esm_explicit_tokens << node.children.first.children.last super end |
#on_const(node) ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/ruby2js/filter/esm.rb', line 163 def on_const(node) if node.children.first == nil and found_import = find_autoimport(node.children.last) prepend_list << s(:import, found_import[0], found_import[1]) values = @esm_defs[node.children.last] if values values = values.map {|value| if value.to_s.start_with? "@" [value.to_s[1..-1].to_sym, s(:self)] else [value.to_sym, s(:autobind, s(:self))] end }.to_h @namespace.defineProps values, [node.children.last] end end super end |
#on_def(node) ⇒ Object
85 86 87 88 89 |
# File 'lib/ruby2js/filter/esm.rb', line 85 def on_def(node) @esm_explicit_tokens << node.children.first super end |
#on_export(node) ⇒ Object
185 186 187 |
# File 'lib/ruby2js/filter/esm.rb', line 185 def on_export(node) s(:export, *process_all(node.children)) end |
#on_lvasgn(node) ⇒ Object
91 92 93 94 95 |
# File 'lib/ruby2js/filter/esm.rb', line 91 def on_lvasgn(node) @esm_explicit_tokens << node.children.first super end |
#on_send(node) ⇒ Object
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 |
# File 'lib/ruby2js/filter/esm.rb', line 97 def on_send(node) target, method, *args = node.children return super unless target.nil? if method == :import or (method == :require and @esm_top&.include? @ast) # don't do the conversion if the word import is followed by a paren if node.loc.respond_to? :selector selector = node.loc.selector if selector and selector.source_buffer return super if selector.source_buffer.source[selector.end_pos] == '(' end end if args[0].type == :str and args.length == 1 # import "file.css" # => import "file.css" s(:import, args[0].children[0]) elsif args.length == 1 and \ args[0].type == :send and \ args[0].children[0].nil? and \ args[0].children[2].type == :send and \ args[0].children[2].children[0].nil? and \ args[0].children[2].children[1] == :from and \ args[0].children[2].children[2].type == :str # import name from "file.js" # => import name from "file.js" @esm_explicit_tokens << args[0].children[1] s(:import, [args[0].children[2].children[2].children[0]], process(s(:attr, nil, args[0].children[1]))) else # import Stuff, "file.js" # => import Stuff from "file.js" # import Stuff, from: "file.js" # => import Stuff from "file.js" # import Stuff, as: "*", from: "file.js" # => import Stuff as * from "file.js" # import [ Some, Stuff ], from: "file.js" # => import { Some, Stuff } from "file.js" # import Some, [ More, Stuff ], from: "file.js" # => import Some, { More, Stuff } from "file.js" imports = [] if i(const send str).include? args[0].type @esm_explicit_tokens << args[0].children.last imports << process(args.shift) end if args[0].type == :array args[0].children.each {|i| @esm_explicit_tokens << i.children.last} imports << process_all(args.shift.children) end s(:import, args[0].children, *imports) unless args[0].nil? end elsif method == :export s(:export, *process_all(args)) elsif target.nil? and found_import = find_autoimport(method) prepend_list << s(:import, found_import[0], found_import[1]) super else super end end |
#options=(options) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/ruby2js/filter/esm.rb', line 10 def () super @esm_autoexports = !@disable_autoexports && [:autoexports] @esm_autoimports = [:autoimports] @esm_defs = [:defs] || {} @esm_explicit_tokens = Set.new @esm_top = nil # don't convert requires if Require filter is included filters = [:filters] || Filter::DEFAULTS if \ defined? Ruby2JS::Filter::Require and filters.include? Ruby2JS::Filter::Require then @esm_top = [] end end |
#process(node) ⇒ Object
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 |
# File 'lib/ruby2js/filter/esm.rb', line 28 def process(node) return super if @esm_top list = [node] while list.length == 1 and list.first.type == :begin list = list.first.children.dup end @esm_top = list return super unless @esm_autoexports replaced = [] list.map! do |child| replacement = child if [:module, :class].include? child.type and child.children.first.type == :const and child.children.first.children.first == nil \ then replacement = s(:export, child) elsif child.type == :casgn and child.children.first == nil replacement = s(:export, child) elsif child.type == :def replacement = s(:export, child) end if replacement != child replaced << replacement @comments[replacement] = @comments[child] if @comments[child] end replacement end if replaced.length == 1 and @esm_autoexports == :default list.map! do |child| if child == replaced.first replacement = s(:export, s(:send, nil, :default, *child.children)) @comments[replacement] = @comments[child] if @comments[child] replacement else child end end end @esm_autoexports = false process s(:begin, *list) end |