ruby2js 3.3.4 → 3.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +96 -65
- data/lib/ruby2js.rb +3 -0
- data/lib/ruby2js/converter.rb +9 -4
- data/lib/ruby2js/converter/block.rb +5 -0
- data/lib/ruby2js/converter/class2.rb +9 -3
- data/lib/ruby2js/converter/cvar.rb +1 -1
- data/lib/ruby2js/converter/cvasgn.rb +1 -1
- data/lib/ruby2js/converter/def.rb +16 -0
- data/lib/ruby2js/converter/dstr.rb +1 -1
- data/lib/ruby2js/converter/hash.rb +9 -2
- data/lib/ruby2js/converter/import.rb +78 -10
- data/lib/ruby2js/converter/ivar.rb +13 -5
- data/lib/ruby2js/converter/ivasgn.rb +1 -1
- data/lib/ruby2js/converter/logical.rb +1 -1
- data/lib/ruby2js/converter/return.rb +3 -1
- data/lib/ruby2js/converter/taglit.rb +13 -0
- data/lib/ruby2js/converter/yield.rb +12 -0
- data/lib/ruby2js/filter/camelCase.rb +78 -35
- data/lib/ruby2js/filter/esm.rb +44 -49
- data/lib/ruby2js/filter/esm_migration.rb +72 -0
- data/lib/ruby2js/filter/fast-deep-equal.rb +23 -0
- data/lib/ruby2js/filter/functions.rb +15 -1
- data/lib/ruby2js/filter/node.rb +10 -3
- data/lib/ruby2js/filter/nokogiri.rb +7 -3
- data/lib/ruby2js/filter/require.rb +0 -2
- data/lib/ruby2js/filter/return.rb +7 -3
- data/lib/ruby2js/filter/tagged_templates.rb +40 -0
- data/lib/ruby2js/serializer.rb +1 -1
- data/lib/ruby2js/version.rb +2 -2
- data/ruby2js.gemspec +1 -1
- metadata +8 -4
- data/lib/ruby2js/filter/rubyjs.rb +0 -112
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f659d6312b558d08a3ca6c1f30183d602445ed580910715909ea2b4334a114a9
|
4
|
+
data.tar.gz: 3f07730f5727ea25b95e095c7b45c19f3fb85dcc5d640e822a3a629fa88b8056
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a85a3d7f144485575608cd136d764e36868d095fb8dbf4f6bc8b8adb8a218379775eeb12934c148574cac0f03dc6e12fc6223ef302973b5f4cb0a0629ba41de4
|
7
|
+
data.tar.gz: 4c154cd393ce76cc0f72e23180fbfdd1b0b8e876a5d60686d246e9f53effff1c4f4ce4ea3c5573cf9ac5cb7cfcda3d2300ee865c2d8d21855a717fa8f203de4a
|
data/README.md
CHANGED
@@ -5,6 +5,7 @@ Minimal yet extensible Ruby to JavaScript conversion.
|
|
5
5
|
|
6
6
|
[](https://travis-ci.org/rubys/ruby2js)
|
7
7
|
[](https://badge.fury.io/rb/ruby2js)
|
8
|
+
[](https://gitter.im/ruby2js/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
|
8
9
|
|
9
10
|
Description
|
10
11
|
---
|
@@ -80,6 +81,12 @@ require 'ruby2js/filter/functions'
|
|
80
81
|
puts Ruby2JS.convert('"2A".to_i(16)')
|
81
82
|
```
|
82
83
|
|
84
|
+
Host variable substitution:
|
85
|
+
|
86
|
+
```ruby
|
87
|
+
puts Ruby2JS.convert("@name", ivars: {:@name => "Joe"})
|
88
|
+
```
|
89
|
+
|
83
90
|
Enable ES2015 support:
|
84
91
|
|
85
92
|
```ruby
|
@@ -104,6 +111,13 @@ Emit nullish coalescing operators:
|
|
104
111
|
puts Ruby2JS.convert('a || 1', or: :nullish)
|
105
112
|
```
|
106
113
|
|
114
|
+
Emit underscored private fields (allowing subclass access):
|
115
|
+
|
116
|
+
```ruby
|
117
|
+
puts Ruby2JS.convert('class C; def initialize; @f=1; end; end',
|
118
|
+
eslevel: 2020, underscored_private: true)
|
119
|
+
```
|
120
|
+
|
107
121
|
With [ExecJS](https://github.com/sstephenson/execjs):
|
108
122
|
```ruby
|
109
123
|
require 'ruby2js/execjs'
|
@@ -193,7 +207,7 @@ Integrations
|
|
193
207
|
|
194
208
|
While this is a low level library suitable for DIY integration, one of the
|
195
209
|
obvious uses of a tool that produces JavaScript is by web servers. Ruby2JS
|
196
|
-
includes
|
210
|
+
includes several integrations:
|
197
211
|
|
198
212
|
* [CGI](https://github.com/rubys/ruby2js/blob/master/lib/ruby2js/cgi.rb)
|
199
213
|
* [Sinatra](https://github.com/rubys/ruby2js/blob/master/lib/ruby2js/sinatra.rb)
|
@@ -204,6 +218,9 @@ As you might expect, CGI is a bit sluggish. By contrast, Sinatra and Rails
|
|
204
218
|
are quite speedy as the bulk of the time is spent on the initial load of the
|
205
219
|
required libraries.
|
206
220
|
|
221
|
+
For easy integration with Webpack (and Webpacker in Rails 5+), you can use the
|
222
|
+
[rb2js-loader](https://github.com/whitefusionhq/rb2js-loader) plugin.
|
223
|
+
|
207
224
|
Filters
|
208
225
|
---
|
209
226
|
|
@@ -270,10 +287,12 @@ the script.
|
|
270
287
|
* `(b...a).to_a` becomes `Array.apply(null, {length: (a-b)}).map(Function.call, Number).map(function (idx) { return idx+b })`
|
271
288
|
* `.strip` becomes `.trim`
|
272
289
|
* `.sub` becomes `.replace`
|
290
|
+
* `.tap {|n| n}` becomes `(function(n) {n; return n})(...)`
|
273
291
|
* `.to_f` becomes `parseFloat`
|
274
292
|
* `.to_i` becomes `parseInt`
|
275
293
|
* `.to_s` becomes `.to_String`
|
276
294
|
* `.upcase` becomes `.toUpperCase`
|
295
|
+
* `.yield_self {|n| n}` becomes `(function(n) {return n})(...)`
|
277
296
|
* `[-n]` becomes `[*.length-n]` for literal values of `n`
|
278
297
|
* `[n...m]` becomes `.slice(n,m)`
|
279
298
|
* `[n..m]` becomes `.slice(n,m+1)`
|
@@ -297,12 +316,78 @@ the script.
|
|
297
316
|
of `Error` instead; and default constructors will be provided
|
298
317
|
* `loop do...end` will be replaced with `while (true) {...}`
|
299
318
|
* `raise Exception.new(...)` will be replaced with `throw new Error(...)`
|
319
|
+
* `block_given?` will check for the presence of optional argument `_implicitBlockYield` which is a function made accessible through the use of `yield` in a method body.
|
300
320
|
|
301
321
|
Additionally, there is one mapping that will only be done if explicitly
|
302
|
-
|
322
|
+
included (pass `include: :class` as a `convert` option to enable):
|
303
323
|
|
304
324
|
* `.class` becomes `.constructor`
|
305
325
|
|
326
|
+
* <a id="tagged_templates" href="https://github.com/rubys/ruby2js/blob/master/lib/ruby2js/filter/tagged_templates.rb">tagged_templates</a>
|
327
|
+
|
328
|
+
Allows you to turn certain method calls with a string argument into tagged
|
329
|
+
template literals. By default it supports html and css, so you can write
|
330
|
+
`html "<div>#{1+2}</div>"` which converts to `` html`<div>${1+2}</div>` ``.
|
331
|
+
Works nicely with squiggly heredocs for multi-line templates as well. If you
|
332
|
+
need to configure the tag names yourself, pass a `template_literal_tags`
|
333
|
+
option to `convert` with an array of tag name symbols.
|
334
|
+
|
335
|
+
Note: these conversions are only done if eslevel >= 2015
|
336
|
+
|
337
|
+
* <a href="https://github.com/rubys/ruby2js/blob/master/lib/ruby2js/filter/esm.rb">esm</a>
|
338
|
+
|
339
|
+
Provides conversion of import and export statements for use with modern ES builders like Webpack.
|
340
|
+
|
341
|
+
Examples:
|
342
|
+
|
343
|
+
**import**
|
344
|
+
|
345
|
+
```ruby
|
346
|
+
import "./index.scss"
|
347
|
+
# => import "./index.scss"
|
348
|
+
|
349
|
+
import Something from "./lib/something"
|
350
|
+
# => import Something from "./lib/something"
|
351
|
+
|
352
|
+
import Something, "./lib/something"
|
353
|
+
# => import Something from "./lib/something"
|
354
|
+
|
355
|
+
import [ LitElement, html, css ], from: "lit-element"
|
356
|
+
# => import { LitElement, html, css } from "lit-element"
|
357
|
+
|
358
|
+
import React, from: "react"
|
359
|
+
# => import React from "react"
|
360
|
+
|
361
|
+
import React, as: "*", from: "react"
|
362
|
+
# => import React as * from "react"
|
363
|
+
```
|
364
|
+
|
365
|
+
**export**
|
366
|
+
|
367
|
+
```ruby
|
368
|
+
export hash = { ab: 123 }
|
369
|
+
# => export const hash = {ab: 123};
|
370
|
+
|
371
|
+
export func = ->(x) { x * 10 }
|
372
|
+
# => export const func = x => x * 10;
|
373
|
+
|
374
|
+
export def multiply(x, y)
|
375
|
+
return x * y
|
376
|
+
end
|
377
|
+
# => export function multiply(x, y) {
|
378
|
+
# return x * y
|
379
|
+
# }
|
380
|
+
|
381
|
+
export default class MyClass
|
382
|
+
end
|
383
|
+
# => export default class MyClass {
|
384
|
+
# };
|
385
|
+
|
386
|
+
# or final export statement:
|
387
|
+
export [ one, two, default: three ]
|
388
|
+
# => export { one, two, three as default }
|
389
|
+
```
|
390
|
+
|
306
391
|
* <a id="node" href="https://github.com/rubys/ruby2js/blob/master/spec/node_spec.rb">node</a>
|
307
392
|
|
308
393
|
* `` `command` `` becomes `child_process.execSync("command", {encoding: "utf8"})`
|
@@ -388,68 +473,6 @@ the script.
|
|
388
473
|
* `text` becomes `textContent`
|
389
474
|
* `to_html` becomes `outerHTML`
|
390
475
|
|
391
|
-
* <a id="rubyjs" href="https://github.com/rubys/ruby2js/blob/master/spec/rubyjs_spec.rb">rubyjs</a>
|
392
|
-
* `.at()` becomes `_a.at()`
|
393
|
-
* `.between?()` becomes `R().between()`
|
394
|
-
* `.capitalize()` becomes `_s.capitalize()`
|
395
|
-
* `.center()` becomes `_s.center()`
|
396
|
-
* `.chomp()` becomes `_s.chomp()`
|
397
|
-
* `.collect_concat()` becomes `_e.collect_concat()`
|
398
|
-
* `.compact()` becomes `_a.compact()`
|
399
|
-
* `.compact!()` becomes `_a.compact_bang()`
|
400
|
-
* `.count()` becomes `_e.count()`
|
401
|
-
* `.cycle()` becomes `_e.cycle()`
|
402
|
-
* `.delete_at()` becomes `_a.delete_at()`
|
403
|
-
* `.delete_if()` becomes `_a.delete_if()`
|
404
|
-
* `.drop_while()` becomes `_e.drop_while()`
|
405
|
-
* `.each_index()` becomes `_e.each_index()`
|
406
|
-
* `.each_slice()` becomes `_e.each_slice()`
|
407
|
-
* `.each_with_index()` becomes `_e.each_with_index()`
|
408
|
-
* `.each_with_object()` becomes `_e.each_with_object()`
|
409
|
-
* `.find_all()` becomes `_e.find_all()`
|
410
|
-
* `.find()` becomes `_e.find()`
|
411
|
-
* `.flat_map()` becomes `_e.flat_map()`
|
412
|
-
* `.flatten()` becomes `_a.flatten()`
|
413
|
-
* `.grep()` becomes `_e.grep()`
|
414
|
-
* `.group_by()` becomes `_e.group_by()`
|
415
|
-
* `.inject()` becomes `_e.inject()`
|
416
|
-
* `.insert()` becomes `_a.insert()`
|
417
|
-
* `.keep_if()` becomes `_a.keep_if()`
|
418
|
-
* `.ljust()` becomes `_s.ljust()`
|
419
|
-
* `.lstrip()` becomes `_s.lstrip()`
|
420
|
-
* `.map()` becomes `_e.map()`
|
421
|
-
* `.max_by()` becomes `_e.max_by()`
|
422
|
-
* `.min_by()` becomes `_e.min_by()`
|
423
|
-
* `.one?()` becomes `_e.one()`
|
424
|
-
* `.partition()` becomes `_e.partition()`
|
425
|
-
* `.reject()` becomes `_e.reject()`
|
426
|
-
* `.reverse()` becomes `_a.reverse()`
|
427
|
-
* `.reverse!()` becomes `_a.reverse_bang()`
|
428
|
-
* `.reverse_each()` becomes `_e.reverse_each()`
|
429
|
-
* `.rindex()` becomes `_s.rindex()`
|
430
|
-
* `.rjust()` becomes `_s.rjust()`
|
431
|
-
* `.rotate()` becomes `_a.rotate()`
|
432
|
-
* `.rotate!()` becomes `_a.rotate_bang()`
|
433
|
-
* `.rstrip()` becomes `_s.rstrip()`
|
434
|
-
* `.scan()` becomes `_s.scan()`
|
435
|
-
* `.select()` becomes `_a.select()`
|
436
|
-
* `.shift()` becomes `_a.shift()`
|
437
|
-
* `.shuffle()` becomes `_a.shuffle()`
|
438
|
-
* `.shuffle!()` becomes `_a.shuffle_bang()`
|
439
|
-
* `.slice()` becomes `_a.slice()`
|
440
|
-
* `.slice!()` becomes `_a.slice_bang()`
|
441
|
-
* `.sort_by()` becomes `_e.sort_by()`
|
442
|
-
* `.strftime()` becomes `_t.strftime()`
|
443
|
-
* `.swapcase()` becomes `_s.swapcase()`
|
444
|
-
* `.take_while()` becomes `_e.take_while(`)
|
445
|
-
* `.transpose()` becomes `_a.transpose()`
|
446
|
-
* `.tr()` becomes `_s.tr()`
|
447
|
-
* `.union()` becomes `_a.union()`
|
448
|
-
* `.uniq()` becomes `_a.uniq()`
|
449
|
-
* `.uniq!()` becomes `_a.uniq_bang()`
|
450
|
-
* `<=>` becomes `R.Comparable.cmp()`
|
451
|
-
* `(n..m)` becomes `R.Range.new()`
|
452
|
-
|
453
476
|
* <a id="underscore" href="https://github.com/rubys/ruby2js/blob/master/spec/underscore.rb">underscore</a>
|
454
477
|
|
455
478
|
* `.clone()` becomes `_.clone()`
|
@@ -605,6 +628,14 @@ conversions are made by the `functions` filter:
|
|
605
628
|
* `.entries()` becomes `Object.entries()`
|
606
629
|
* `.each_pair {}` becomes `for (let [key, value] of Object.entries()) {}'
|
607
630
|
|
631
|
+
async support:
|
632
|
+
|
633
|
+
* `async def` becomes `async function`
|
634
|
+
* `async lambda` becomes `async =>`
|
635
|
+
* `async proc` becomes `async =>`
|
636
|
+
* `async ->` becomes `async =>`
|
637
|
+
* `foo bar, async do...end` becomes `foo(bar, async () => {})`
|
638
|
+
|
608
639
|
ES2018 support
|
609
640
|
---
|
610
641
|
|
@@ -677,7 +708,7 @@ License
|
|
677
708
|
|
678
709
|
(The MIT License)
|
679
710
|
|
680
|
-
Copyright (c) 2009,
|
711
|
+
Copyright (c) 2009, 2020 Macario Ortega, Sam Ruby, Jared White
|
681
712
|
|
682
713
|
Permission is hereby granted, free of charge, to any person obtaining
|
683
714
|
a copy of this software and associated documentation files (the
|
data/lib/ruby2js.rb
CHANGED
@@ -56,6 +56,7 @@ module Ruby2JS
|
|
56
56
|
@comments = comments
|
57
57
|
@ast = nil
|
58
58
|
@exclude_methods = []
|
59
|
+
@esm = false
|
59
60
|
end
|
60
61
|
|
61
62
|
def options=(options)
|
@@ -134,6 +135,7 @@ module Ruby2JS
|
|
134
135
|
def on_xnode(node); end
|
135
136
|
def on_export(node); end
|
136
137
|
def on_import(node); end
|
138
|
+
def on_taglit(node); end
|
137
139
|
|
138
140
|
# provide a method so filters can call 'super'
|
139
141
|
def on_sym(node); node; end
|
@@ -201,6 +203,7 @@ module Ruby2JS
|
|
201
203
|
ruby2js.strict = options[:strict]
|
202
204
|
ruby2js.comparison = options[:comparison] || :equality
|
203
205
|
ruby2js.or = options[:or] || :logical
|
206
|
+
ruby2js.underscored_private = (options[:eslevel] < 2020) || options[:underscored_private]
|
204
207
|
if ruby2js.binding and not ruby2js.ivars
|
205
208
|
ruby2js.ivars = ruby2js.binding.eval \
|
206
209
|
'Hash[instance_variables.map {|var| [var, instance_variable_get(var)]}]'
|
data/lib/ruby2js/converter.rb
CHANGED
@@ -3,9 +3,11 @@ require 'ruby2js/serializer'
|
|
3
3
|
module Ruby2JS
|
4
4
|
class Error < NotImplementedError
|
5
5
|
def initialize(message, ast)
|
6
|
-
|
7
|
-
|
8
|
-
|
6
|
+
if ast.loc
|
7
|
+
message += ' at ' + ast.loc.expression.source_buffer.name.to_s
|
8
|
+
message += ':' + ast.loc.expression.line.inspect
|
9
|
+
message += ':' + ast.loc.expression.column.to_s
|
10
|
+
end
|
9
11
|
super(message)
|
10
12
|
end
|
11
13
|
end
|
@@ -63,6 +65,7 @@ module Ruby2JS
|
|
63
65
|
@strict = false
|
64
66
|
@comparison = :equality
|
65
67
|
@or = :logical
|
68
|
+
@underscored_private = true
|
66
69
|
end
|
67
70
|
|
68
71
|
def width=(width)
|
@@ -127,7 +130,7 @@ module Ruby2JS
|
|
127
130
|
Parser::AST::Node.new(type, args)
|
128
131
|
end
|
129
132
|
|
130
|
-
attr_accessor :strict, :eslevel, :comparison, :or
|
133
|
+
attr_accessor :strict, :eslevel, :comparison, :or, :underscored_private
|
131
134
|
|
132
135
|
def es2015
|
133
136
|
@eslevel >= 2015
|
@@ -332,6 +335,7 @@ require 'ruby2js/converter/self'
|
|
332
335
|
require 'ruby2js/converter/send'
|
333
336
|
require 'ruby2js/converter/super'
|
334
337
|
require 'ruby2js/converter/sym'
|
338
|
+
require 'ruby2js/converter/taglit'
|
335
339
|
require 'ruby2js/converter/undef'
|
336
340
|
require 'ruby2js/converter/until'
|
337
341
|
require 'ruby2js/converter/untilpost'
|
@@ -341,3 +345,4 @@ require 'ruby2js/converter/while'
|
|
341
345
|
require 'ruby2js/converter/whilepost'
|
342
346
|
require 'ruby2js/converter/xstr'
|
343
347
|
require 'ruby2js/converter/xnode'
|
348
|
+
require 'ruby2js/converter/yield'
|
@@ -9,6 +9,11 @@ module Ruby2JS
|
|
9
9
|
|
10
10
|
handle :block do |call, args, block|
|
11
11
|
|
12
|
+
if es2017 and call.children.last == s(:send, nil, :async)
|
13
|
+
return parse call.updated(nil, [*call.children[0..-2],
|
14
|
+
s(:send, nil, :async, s(:block, s(:send, nil, :proc), args, block))])
|
15
|
+
end
|
16
|
+
|
12
17
|
if \
|
13
18
|
@state == :statement and args.children.length == 1 and
|
14
19
|
call.children.first and call.children.first.type == :begin and
|
@@ -49,7 +49,7 @@ module Ruby2JS
|
|
49
49
|
end
|
50
50
|
|
51
51
|
# private variable declarations
|
52
|
-
|
52
|
+
unless underscored_private
|
53
53
|
ivars = Set.new
|
54
54
|
cvars = Set.new
|
55
55
|
|
@@ -202,7 +202,7 @@ module Ruby2JS
|
|
202
202
|
end
|
203
203
|
|
204
204
|
elsif m.type == :send and m.children.first == nil
|
205
|
-
p =
|
205
|
+
p = underscored_private ? '_' : '#'
|
206
206
|
|
207
207
|
if m.children[1] == :attr_accessor
|
208
208
|
m.children[2..-1].each_with_index do |child_sym, index2|
|
@@ -236,7 +236,7 @@ module Ruby2JS
|
|
236
236
|
end
|
237
237
|
|
238
238
|
else
|
239
|
-
if m.type == :cvasgn and
|
239
|
+
if m.type == :cvasgn and !underscored_private
|
240
240
|
put 'static #$'; put m.children[0].to_s[2..-1]; put ' = '
|
241
241
|
parse m.children[1]
|
242
242
|
else
|
@@ -288,6 +288,12 @@ module Ruby2JS
|
|
288
288
|
[name, innerclass_name.children[1]])
|
289
289
|
end
|
290
290
|
parse m.updated(nil, [innerclass_name, *m.children[1..-1]])
|
291
|
+
elsif m.type == :send && (m.children[0].nil? || m.children[0].type == :self)
|
292
|
+
if m.children[0].nil?
|
293
|
+
parse m.updated(:send, [@class_name, *m.children[1..-1]])
|
294
|
+
else
|
295
|
+
parse m.updated(:send, [@class_name, *m.children[1..-1]])
|
296
|
+
end
|
291
297
|
else
|
292
298
|
parse m, :statement
|
293
299
|
end
|
@@ -9,6 +9,22 @@ module Ruby2JS
|
|
9
9
|
handle :def, :defm, :async do |name, args, body=nil|
|
10
10
|
body ||= s(:begin)
|
11
11
|
|
12
|
+
add_implicit_block = false
|
13
|
+
|
14
|
+
walk = ->(node) do
|
15
|
+
add_implicit_block = true if node.type == :yield || (node.type == :send && node.children[1] == "_implicitBlockYield")
|
16
|
+
node.children.each do |child|
|
17
|
+
walk[child] if child.is_a? Parser::AST::Node
|
18
|
+
end
|
19
|
+
end
|
20
|
+
walk[body]
|
21
|
+
|
22
|
+
if add_implicit_block
|
23
|
+
children = args.children.dup
|
24
|
+
children.push s(:optarg, "_implicitBlockYield", s(:nil))
|
25
|
+
args = s(:args, *children)
|
26
|
+
end
|
27
|
+
|
12
28
|
vars = {}
|
13
29
|
vars.merge! @vars unless name
|
14
30
|
if args and !args.children.empty?
|
@@ -20,7 +20,7 @@ module Ruby2JS
|
|
20
20
|
put '`'
|
21
21
|
children.each do |child|
|
22
22
|
if child.type == :str
|
23
|
-
str = child.children.first.inspect[1..-2].gsub('${', '$\{')
|
23
|
+
str = child.children.first.inspect[1..-2].gsub('${', '$\{').gsub('`', '\\\`')
|
24
24
|
if heredoc
|
25
25
|
put! str.gsub("\\n", "\n")
|
26
26
|
else
|
@@ -97,10 +97,17 @@ module Ruby2JS
|
|
97
97
|
end
|
98
98
|
|
99
99
|
# use fat arrow syntax if block contains a reference to 'this'
|
100
|
-
if anonfn
|
100
|
+
if anonfn and @class_name
|
101
101
|
walk = proc do |ast|
|
102
|
-
if ast == s(:self)
|
102
|
+
if ast == s(:self)
|
103
103
|
anonfn = false
|
104
|
+
elsif [:ivar, :ivasgn].include? ast.type
|
105
|
+
anonfn = false
|
106
|
+
elsif ast.type == :send and ast.children.first == nil
|
107
|
+
method = ast.children.last if ast.children.length == 2
|
108
|
+
if @rbstack.any? {|rb| rb[method]} or method == :this
|
109
|
+
anonfn = false
|
110
|
+
end
|
104
111
|
end
|
105
112
|
|
106
113
|
ast.children.each do |child|
|
@@ -7,14 +7,47 @@ module Ruby2JS
|
|
7
7
|
|
8
8
|
handle :import do |path, *args|
|
9
9
|
put 'import '
|
10
|
+
if args.length == 0
|
11
|
+
# import "file.css"
|
12
|
+
put path.inspect
|
13
|
+
else
|
14
|
+
# import (x) from "file.js"
|
15
|
+
default_import = !args.first.is_a?(Array) && [:const, :send, :attr].include?(args.first.type)
|
16
|
+
args = args.first if args.first.is_a?(Array)
|
10
17
|
|
11
|
-
|
12
|
-
put
|
13
|
-
|
14
|
-
|
18
|
+
# handle the default name or { ConstA, Const B } portion
|
19
|
+
put "{ " unless default_import
|
20
|
+
args.each_with_index do |arg, index|
|
21
|
+
put ', ' unless index == 0
|
22
|
+
parse arg
|
23
|
+
end
|
24
|
+
put " }" unless default_import
|
25
|
+
|
26
|
+
from_kwarg_position = 0
|
27
|
+
|
28
|
+
# should there be an as clause? e.g., import React as *
|
29
|
+
if path.is_a?(Array) && !path[0].is_a?(String) && path[0].type == :pair && path[0].children[0].children[0] == :as
|
30
|
+
put " as #{path[0].children[1].children[0]}"
|
31
|
+
|
32
|
+
# advance to the next kwarg, aka from
|
33
|
+
from_kwarg_position = 1
|
34
|
+
end
|
15
35
|
|
16
|
-
|
17
|
-
|
36
|
+
put ' from '
|
37
|
+
|
38
|
+
if path.is_a?(Array) && !path[from_kwarg_position].is_a?(String) && path[from_kwarg_position].type == :pair
|
39
|
+
# from: "str" => from "str"
|
40
|
+
if path[from_kwarg_position].children[0].children[0] == :from
|
41
|
+
put path[from_kwarg_position].children[1].children[0].inspect
|
42
|
+
else
|
43
|
+
# from is missing
|
44
|
+
put '""'
|
45
|
+
end
|
46
|
+
else
|
47
|
+
# handle a str in either an array element or directly passed in
|
48
|
+
put path.is_a?(Array) ? path[0].inspect : path.inspect
|
49
|
+
end
|
50
|
+
end
|
18
51
|
end
|
19
52
|
|
20
53
|
# (export const)
|
@@ -24,14 +57,49 @@ module Ruby2JS
|
|
24
57
|
handle :export do |*args|
|
25
58
|
put 'export '
|
26
59
|
|
27
|
-
|
60
|
+
node = args.first
|
61
|
+
final_export = false
|
62
|
+
|
63
|
+
if node == :default
|
28
64
|
put 'default '
|
29
65
|
args.shift
|
66
|
+
elsif node.respond_to?(:type) && node.children[1] == :default
|
67
|
+
put 'default '
|
68
|
+
args[0] = node.children[2]
|
69
|
+
elsif node.respond_to?(:type) && node.type == :lvasgn
|
70
|
+
if node.children[0] == :default
|
71
|
+
put 'default '
|
72
|
+
args[0] = node.children[1]
|
73
|
+
else
|
74
|
+
put 'const '
|
75
|
+
end
|
76
|
+
elsif node.respond_to?(:type) &&
|
77
|
+
node.type == :array &&
|
78
|
+
node.children[0].respond_to?(:type) &&
|
79
|
+
(
|
80
|
+
node.children[0].type == :const ||
|
81
|
+
node.children[0].type == :send ||
|
82
|
+
(node.children[0].type == :hash && node.children[0].children[0].children[0].children[0] == :default )
|
83
|
+
)
|
84
|
+
final_export = true
|
85
|
+
put '{ '
|
86
|
+
node.children.each_with_index do |arg, index|
|
87
|
+
put ', ' unless index == 0
|
88
|
+
if arg.type == :hash && arg.children[0].children[0].children[0] == :default
|
89
|
+
put arg.children[0].children[1].children[1]
|
90
|
+
put ' as default'
|
91
|
+
else
|
92
|
+
parse arg
|
93
|
+
end
|
94
|
+
end
|
95
|
+
put ' }'
|
30
96
|
end
|
31
97
|
|
32
|
-
|
33
|
-
|
34
|
-
|
98
|
+
unless final_export
|
99
|
+
args.each_with_index do |arg, index|
|
100
|
+
put ', ' unless index == 0
|
101
|
+
parse arg
|
102
|
+
end
|
35
103
|
end
|
36
104
|
end
|
37
105
|
end
|