-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathesm.rb
207 lines (169 loc) · 6.18 KB
/
esm.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
require 'ruby2js'
Ruby2JS.module_default = :esm
module Ruby2JS
module Filter
module ESM
include SEXP
def options=(options)
super
@esm_autoexports = !@disable_autoexports && options[:autoexports]
@esm_autoimports = options[:autoimports]
@esm_defs = options[:defs] || {}
@esm_explicit_tokens = Set.new
@esm_top = nil
# don't convert requires if Require filter is included
filters = options[:filters] || Filter::DEFAULTS
if \
defined? Ruby2JS::Filter::Require and
filters.include? Ruby2JS::Filter::Require
then
@esm_top = []
end
end
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
def on_class(node)
@esm_explicit_tokens << node.children.first.children.last
super
end
def on_def(node)
@esm_explicit_tokens << node.children.first
super
end
def on_lvasgn(node)
@esm_explicit_tokens << node.children.first
super
end
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
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
def on_export(node)
s(:export, *process_all(node.children))
end
end
private
def find_autoimport(token)
return nil if @esm_autoimports.nil?
return nil if @esm_explicit_tokens.include?(token)
token = camelCase(token) if respond_to?(:camelCase)
if @esm_autoimports[token]
[@esm_autoimports[token], s(:const, nil, token)]
elsif found_key = @esm_autoimports.keys.find {|key| key.is_a?(Array) && key.include?(token)}
[@esm_autoimports[found_key], found_key.map {|key| s(:const, nil, key)}]
end
end
DEFAULTS.push ESM
end
end