-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathconverter.rb
386 lines (328 loc) · 9.66 KB
/
converter.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
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
require 'ruby2js/serializer'
module Ruby2JS
class Error < NotImplementedError
def initialize(message, ast)
if ast.loc
message += ' at ' + ast.loc.expression.source_buffer.name.to_s
message += ':' + ast.loc.expression.line.inspect
message += ':' + ast.loc.expression.column.to_s
end
super(message)
end
end
class Converter < Serializer
attr_accessor :ast
LOGICAL = :and, :not, :or
OPERATORS = [:[], :[]=], [:not, :!], [:**], [:*, :/, :%], [:+, :-],
[:>>, :<<], [:&], [:^, :|], [:<=, :<, :>, :>=],
[:==, :!=, :===, :"!==", :=~, :!~], [:and, :or]
INVERT_OP = {
:< => :>=,
:<= => :>,
:== => :!=,
:!= => :==,
:> => :<=,
:>= => :<,
:=== => :'!=='
}
GROUP_OPERATORS = [:begin, :dstr, :dsym, :and, :or, :casgn, :if]
VASGN = [:cvasgn, :ivasgn, :gvasgn, :lvasgn]
attr_accessor :binding, :ivars, :namespace
def initialize( ast, comments, vars = {} )
super()
@ast, @comments, @vars = ast, comments, vars.dup
@varstack = []
@scope = ast
@inner = nil
@rbstack = []
@next_token = :return
@handlers = {}
@@handlers.each do |name|
@handlers[name] = method("on_#{name}")
end
@state = nil
@block_this = nil
@block_depth = nil
@prop = nil
@instance_method = nil
@prototype = nil
@class_parent = nil
@class_name = nil
@jsx = false
@autobind = true
@eslevel = :es5
@strict = false
@comparison = :equality
@or = :logical
@underscored_private = true
@redoable = false
end
def width=(width)
@width = width
end
def convert
scope @ast
if @strict
if @sep == '; '
@lines.first.unshift "\"use strict\"#@sep"
else
@lines.unshift Line.new('"use strict";')
end
end
end
def operator_index op
OPERATORS.index( OPERATORS.find{ |el| el.include? op } ) || -1
end
# define a new scope; primarily determines what variables are visible and deals with hoisting of
# declarations
def scope( ast, args=nil )
scope, @scope = @scope, ast
inner, @inner = @inner, nil
mark = output_location
@varstack.push @vars
@vars = args if args
@vars = Hash[@vars.map {|key, value| [key, true]}]
parse( ast, :statement )
# retroactively add a declaration for 'pending' variables
vars = @vars.select {|key, value| value == :pending}.keys
unless vars.empty?
insert mark, "#{es2015 ? 'let' : 'var'} #{vars.join(', ')}#{@sep}"
vars.each {|var| @vars[var] = true}
end
ensure
@vars = @varstack.pop
@scope = scope
@inner = inner
end
# handle the oddity where javascript considers there to be a scope (e.g. the body of an if statement),
# whereas Ruby does not.
def jscope( ast, args=nil )
@varstack.push @vars
@vars = args if args
@vars = Hash[@vars.map {|key, value| [key, true]}]
parse( ast, :statement )
ensure
pending = @vars.select {|key, value| value == :pending}
@vars = @varstack.pop
@vars.merge! pending
end
def s(type, *args)
Parser::AST::Node.new(type, args)
end
attr_accessor :strict, :eslevel, :module_type, :comparison, :or, :underscored_private
def es2015
@eslevel >= 2015
end
def es2016
@eslevel >= 2016
end
def es2017
@eslevel >= 2017
end
def es2018
@eslevel >= 2018
end
def es2019
@eslevel >= 2019
end
def es2020
@eslevel >= 2020
end
def es2021
@eslevel >= 2021
end
def es2022
@eslevel >= 2022
end
@@handlers = []
def self.handle(*types, &block)
types.each do |type|
define_method("on_#{type}", block)
@@handlers << type
end
end
# extract comments that either precede or are included in the node.
# remove from the list this node may appear later in the tree.
def comments(ast)
if ast.loc and ast.loc.respond_to? :expression
expression = ast.loc.expression
list = @comments[ast].select do |comment|
expression.source_buffer == comment.loc.expression.source_buffer and
comment.loc.expression.begin_pos < expression.end_pos
end
else
list = @comments[ast]
end
@comments[ast] -= list
list.map do |comment|
if comment.text.start_with? '=begin'
if comment.text.include? '*/'
comment.text.sub(/\A=begin/, '').sub(/^=end\Z/, '').gsub(/^/, '//')
else
comment.text.sub(/\A=begin/, '/*').sub(/^=end\Z/, '*/')
end
else
comment.text.sub(/^#/, '//') + "\n"
end
end
end
def parse(ast, state=:expression)
oldstate, @state = @state, state
oldast, @ast = @ast, ast
return unless ast
handler = @handlers[ast.type]
unless handler
raise Error.new("unknown AST type #{ ast.type }", ast)
end
if state == :statement and not @comments[ast].empty?
comments(ast).each {|comment| puts comment.chomp}
end
handler.call(*ast.children)
ensure
@ast = oldast
@state = oldstate
end
def parse_all(*args)
@options = (Hash === args.last) ? args.pop : {}
sep = @options[:join].to_s
state = @options[:state] || :expression
index = 0
args.each do |arg|
put sep unless index == 0
parse arg, state
index += 1 unless arg == s(:begin)
end
end
def group( ast )
if [:dstr, :dsym].include? ast.type and es2015
parse ast
else
put '('; parse ast; put ')'
end
end
def redoable(block)
save_redoable = @redoable
has_redo = proc do |node|
node.children.any? do |child|
next false unless child.is_a? Parser::AST::Node
next true if child.type == :redo
next false if %i[for while while_post until until_post].include? child.type
has_redo[child]
end
end
@redoable = has_redo[@ast]
if @redoable
put es2015 ? 'let ' : 'var '
put "redo$#@sep"
puts 'do {'
put "redo$ = false#@sep"
scope block
put "#@nl} while(redo$)"
else
scope block
end
ensure
@redoable = save_redoable
end
def timestamp(file)
super
return unless file
walk = proc do |ast|
if ast.loc and ast.loc.expression
filename = ast.loc.expression.source_buffer.name
if filename and not filename.empty?
@timestamps[filename] ||= File.mtime(filename) rescue nil
end
end
ast.children.each do |child|
walk[child] if child.is_a? Parser::AST::Node
end
end
walk[@ast] if @ast
end
end
end
module Parser
module AST
class Node
def is_method?
return false if type == :attr
return true if type == :call
return true unless loc
if loc.respond_to? :selector
return true if children.length > 2
selector = loc.selector
elsif type == :defs
return true if children[1] =~ /[!?]$/
return true if children[2].children.length > 0
selector = loc.name
elsif type == :def
return true if children[0] =~ /[!?]$/
return true if children[1].children.length > 0
selector = loc.name
end
return true unless selector and selector.source_buffer
selector.source_buffer.source[selector.end_pos] == '('
end
end
end
end
# see https://github.com/whitequark/parser/blob/master/doc/AST_FORMAT.md
require 'ruby2js/converter/arg'
require 'ruby2js/converter/args'
require 'ruby2js/converter/array'
require 'ruby2js/converter/assign'
require 'ruby2js/converter/begin'
require 'ruby2js/converter/block'
require 'ruby2js/converter/blockpass'
require 'ruby2js/converter/boolean'
require 'ruby2js/converter/break'
require 'ruby2js/converter/case'
require 'ruby2js/converter/casgn'
require 'ruby2js/converter/class'
require 'ruby2js/converter/class2'
require 'ruby2js/converter/const'
require 'ruby2js/converter/cvar'
require 'ruby2js/converter/cvasgn'
require 'ruby2js/converter/def'
require 'ruby2js/converter/defs'
require 'ruby2js/converter/defined'
require 'ruby2js/converter/dstr'
require 'ruby2js/converter/fileline'
require 'ruby2js/converter/for'
require 'ruby2js/converter/hash'
require 'ruby2js/converter/hide'
require 'ruby2js/converter/if'
require 'ruby2js/converter/in'
require 'ruby2js/converter/import'
require 'ruby2js/converter/ivar'
require 'ruby2js/converter/ivasgn'
require 'ruby2js/converter/kwbegin'
require 'ruby2js/converter/literal'
require 'ruby2js/converter/logical'
require 'ruby2js/converter/masgn'
require 'ruby2js/converter/match'
require 'ruby2js/converter/module'
require 'ruby2js/converter/next'
require 'ruby2js/converter/nil'
require 'ruby2js/converter/nthref'
require 'ruby2js/converter/opasgn'
require 'ruby2js/converter/prototype'
require 'ruby2js/converter/redo'
require 'ruby2js/converter/regexp'
require 'ruby2js/converter/return'
require 'ruby2js/converter/self'
require 'ruby2js/converter/send'
require 'ruby2js/converter/super'
require 'ruby2js/converter/sym'
require 'ruby2js/converter/taglit'
require 'ruby2js/converter/undef'
require 'ruby2js/converter/until'
require 'ruby2js/converter/untilpost'
require 'ruby2js/converter/var'
require 'ruby2js/converter/vasgn'
require 'ruby2js/converter/while'
require 'ruby2js/converter/whilepost'
require 'ruby2js/converter/xstr'
require 'ruby2js/converter/xnode'
require 'ruby2js/converter/yield'