Module: Ruby2JS::Filter::ESM

Includes:
SEXP
Defined in:
lib/ruby2js/filter/esm.rb

Instance Method Summary collapse

Methods included from SEXP

#S, #s

Instance Method Details

#on_class(node) ⇒ Object



65
66
67
68
69
# File 'lib/ruby2js/filter/esm.rb', line 65

def on_class(node)
  @esm_explicit_tokens << node.children.first.children.last

  super
end

#on_const(node) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/ruby2js/filter/esm.rb', line 149

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



71
72
73
74
75
# File 'lib/ruby2js/filter/esm.rb', line 71

def on_def(node)
  @esm_explicit_tokens << node.children.first

  super
end

#on_export(node) ⇒ Object



171
172
173
# File 'lib/ruby2js/filter/esm.rb', line 171

def on_export(node)
  s(:export, *process_all(node.children))
end

#on_lvasgn(node) ⇒ Object



77
78
79
80
81
# File 'lib/ruby2js/filter/esm.rb', line 77

def on_lvasgn(node)
  @esm_explicit_tokens << node.children.first

  super
end

#on_send(node) ⇒ Object



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
# File 'lib/ruby2js/filter/esm.rb', line 83

def on_send(node)
  target, method, *args = node.children
  return super unless target.nil?

  if method == :import
    # 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
# File 'lib/ruby2js/filter/esm.rb', line 10

def options=(options)
  super
  @esm_autoexports = !@disable_autoexports && options[:autoexports]
  @esm_autoimports = options[:autoimports]
  @esm_defs = options[:defs] || {}
  @esm_explicit_tokens = Set.new
end

#process(node) ⇒ Object



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
# File 'lib/ruby2js/filter/esm.rb', line 18

def process(node)
  return super unless @esm_autoexports

  list = [node]
  while list.length == 1 and list.first.type == :begin
    list = list.first.children.dup
  end

  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