ruby2js 1.12.2 → 1.13.0
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.
- data/README.md +9 -0
- data/lib/ruby2js/converter/def.rb +1 -0
- data/lib/ruby2js/converter/hash.rb +8 -3
- data/lib/ruby2js/execjs.rb +16 -0
- data/lib/ruby2js/filter/react.rb +46 -10
- data/lib/ruby2js/version.rb +2 -2
- data/ruby2js.gemspec +3 -3
- metadata +3 -2
data/README.md
CHANGED
@@ -46,6 +46,15 @@ require 'ruby2js/filter/functions'
|
|
46
46
|
puts Ruby2JS.convert('"2A".to_i(16)')
|
47
47
|
```
|
48
48
|
|
49
|
+
With [ExecJS](https://github.com/sstephenson/execjs):
|
50
|
+
```ruby
|
51
|
+
require 'ruby2js/execjs'
|
52
|
+
require 'date'
|
53
|
+
|
54
|
+
context = Ruby2JS.compile(Date.today.strftime('d = new Date(%Y, %-m, %-d)'))
|
55
|
+
puts context.eval('d.getYear()')+1900
|
56
|
+
```
|
57
|
+
|
49
58
|
Conversions can be explored interactively using the
|
50
59
|
[demo](https://github.com/rubys/ruby2js/blob/master/demo/ruby2js.rb) provided.
|
51
60
|
|
@@ -11,10 +11,12 @@ module Ruby2JS
|
|
11
11
|
raise NotImplementedError, "kwsplat" if node.type == :kwsplat
|
12
12
|
|
13
13
|
begin
|
14
|
-
block_depth = @block_depth
|
14
|
+
block_depth, block_this, block_hash = @block_depth, @block_this, false
|
15
15
|
left, right = node.children
|
16
16
|
|
17
|
-
|
17
|
+
if Hash === right or right.type == :block
|
18
|
+
@block_depth, block_hash = 0, true
|
19
|
+
end
|
18
20
|
|
19
21
|
if left.type == :prop
|
20
22
|
result = []
|
@@ -34,7 +36,10 @@ module Ruby2JS
|
|
34
36
|
end
|
35
37
|
|
36
38
|
ensure
|
37
|
-
|
39
|
+
if block_hash
|
40
|
+
@block_depth = block_depth
|
41
|
+
@block_this = block_this
|
42
|
+
end
|
38
43
|
end
|
39
44
|
end
|
40
45
|
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'ruby2js'
|
2
|
+
require 'execjs'
|
3
|
+
|
4
|
+
module Ruby2JS
|
5
|
+
def self.compile(source, options={})
|
6
|
+
ExecJS.compile(convert(source, options))
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.eval(source, options={})
|
10
|
+
ExecJS.eval(convert(source, options))
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.exec(source, options={})
|
14
|
+
ExecJS.exec(convert(source, options))
|
15
|
+
end
|
16
|
+
end
|
data/lib/ruby2js/filter/react.rb
CHANGED
@@ -68,13 +68,19 @@ module Ruby2JS
|
|
68
68
|
end
|
69
69
|
|
70
70
|
def on_send(node)
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
71
|
+
if not @react
|
72
|
+
# enable React filtering within React class method calls or
|
73
|
+
# React component calls
|
74
|
+
if
|
75
|
+
node.children.first == s(:const, nil, :React) or
|
76
|
+
node.children.first == nil and node.children[1] =~ /^_[A-Z]/
|
77
|
+
then
|
78
|
+
begin
|
79
|
+
@react = true
|
80
|
+
return on_send(node)
|
81
|
+
ensure
|
82
|
+
@react = false
|
83
|
+
end
|
78
84
|
end
|
79
85
|
end
|
80
86
|
|
@@ -216,6 +222,9 @@ module Ruby2JS
|
|
216
222
|
params << process(text)
|
217
223
|
end
|
218
224
|
|
225
|
+
# trim trailing null if no text or children
|
226
|
+
params.pop if params.last == s(:nil)
|
227
|
+
|
219
228
|
# construct element using params
|
220
229
|
element = s(:send, s(:const, nil, :React), :createElement, *params)
|
221
230
|
|
@@ -344,8 +353,36 @@ module Ruby2JS
|
|
344
353
|
|
345
354
|
# convert blocks to proc arguments
|
346
355
|
def on_block(node)
|
356
|
+
if not @react
|
357
|
+
# enable React filtering on React component calls
|
358
|
+
if
|
359
|
+
node.children[0].children[0] == nil and
|
360
|
+
node.children[0].children[1] =~ /^_[A-Z]/
|
361
|
+
then
|
362
|
+
begin
|
363
|
+
@react = true
|
364
|
+
return on_block(node)
|
365
|
+
ensure
|
366
|
+
@react = false
|
367
|
+
end
|
368
|
+
end
|
369
|
+
end
|
370
|
+
|
347
371
|
return super unless @react
|
348
|
-
|
372
|
+
|
373
|
+
# iterate over Enumerable arguments if (a) node is a createElement
|
374
|
+
# type of call, and (b) there are args present
|
375
|
+
if
|
376
|
+
node.children.first.children[0] == nil and
|
377
|
+
node.children.first.children[1] =~ /^_/ and
|
378
|
+
not node.children[1].children.empty?
|
379
|
+
then
|
380
|
+
send = node.children.first.children
|
381
|
+
return super if send.length < 3
|
382
|
+
return process s(:block, s(:send, *send[0..1], *send[3..-1]),
|
383
|
+
s(:args), s(:block, s(:send, send[2], :forEach),
|
384
|
+
*node.children[1..-1]))
|
385
|
+
end
|
349
386
|
|
350
387
|
# traverse through potential "css proxy" style method calls
|
351
388
|
child = node.children.first
|
@@ -356,8 +393,7 @@ module Ruby2JS
|
|
356
393
|
|
357
394
|
# append block as a standalone proc to wunderbar style method call
|
358
395
|
if child.children[0] == nil and child.children[1] =~ /^_\w/
|
359
|
-
block = s(:block, s(:send, nil, :proc),
|
360
|
-
s(:args, s(:arg, :_index), s(:arg, :_parent)),
|
396
|
+
block = s(:block, s(:send, nil, :proc), s(:args),
|
361
397
|
*node.children[2..-1])
|
362
398
|
return on_send s(:send, *node.children.first.children, block)
|
363
399
|
end
|
data/lib/ruby2js/version.rb
CHANGED
data/ruby2js.gemspec
CHANGED
@@ -2,14 +2,14 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = "ruby2js"
|
5
|
-
s.version = "1.
|
5
|
+
s.version = "1.13.0"
|
6
6
|
|
7
7
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
8
|
s.authors = ["Sam Ruby"]
|
9
|
-
s.date = "2015-
|
9
|
+
s.date = "2015-02-02"
|
10
10
|
s.description = " The base package maps Ruby syntax to JavaScript semantics.\n Filters may be provided to add Ruby-specific or framework specific\n behavior.\n"
|
11
11
|
s.email = "rubys@intertwingly.net"
|
12
|
-
s.files = ["ruby2js.gemspec", "README.md", "lib/ruby2js", "lib/ruby2js/converter.rb", "lib/ruby2js/version.rb", "lib/ruby2js/filter", "lib/ruby2js/filter/jquery.rb", "lib/ruby2js/filter/return.rb", "lib/ruby2js/filter/underscore.rb", "lib/ruby2js/filter/angularrb.rb", "lib/ruby2js/filter/minitest-jasmine.rb", "lib/ruby2js/filter/camelCase.rb", "lib/ruby2js/filter/strict.rb", "lib/ruby2js/filter/functions.rb", "lib/ruby2js/filter/angular-resource.rb", "lib/ruby2js/filter/react.rb", "lib/ruby2js/filter/angular-route.rb", "lib/ruby2js/sinatra.rb", "lib/ruby2js/rails.rb", "lib/ruby2js/cgi.rb", "lib/ruby2js/converter", "lib/ruby2js/converter/self.rb", "lib/ruby2js/converter/arg.rb", "lib/ruby2js/converter/dstr.rb", "lib/ruby2js/converter/xstr.rb", "lib/ruby2js/converter/sym.rb", "lib/ruby2js/converter/return.rb", "lib/ruby2js/converter/break.rb", "lib/ruby2js/converter/for.rb", "lib/ruby2js/converter/defined.rb", "lib/ruby2js/converter/cvasgn.rb", "lib/ruby2js/converter/whilepost.rb", "lib/ruby2js/converter/logical.rb", "lib/ruby2js/converter/args.rb", "lib/ruby2js/converter/def.rb", "lib/ruby2js/converter/prototype.rb", "lib/ruby2js/converter/class.rb", "lib/ruby2js/converter/nthref.rb", "lib/ruby2js/converter/opasgn.rb", "lib/ruby2js/converter/module.rb", "lib/ruby2js/converter/kwbegin.rb", "lib/ruby2js/converter/send.rb", "lib/ruby2js/converter/boolean.rb", "lib/ruby2js/converter/masgn.rb", "lib/ruby2js/converter/hash.rb", "lib/ruby2js/converter/cvar.rb", "lib/ruby2js/converter/ivasgn.rb", "lib/ruby2js/converter/case.rb", "lib/ruby2js/converter/const.rb", "lib/ruby2js/converter/vasgn.rb", "lib/ruby2js/converter/untilpost.rb", "lib/ruby2js/converter/begin.rb", "lib/ruby2js/converter/ivar.rb", "lib/ruby2js/converter/while.rb", "lib/ruby2js/converter/next.rb", "lib/ruby2js/converter/nil.rb", "lib/ruby2js/converter/blockpass.rb", "lib/ruby2js/converter/super.rb", "lib/ruby2js/converter/defs.rb", "lib/ruby2js/converter/array.rb", "lib/ruby2js/converter/block.rb", "lib/ruby2js/converter/if.rb", "lib/ruby2js/converter/until.rb", "lib/ruby2js/converter/regexp.rb", "lib/ruby2js/converter/casgn.rb", "lib/ruby2js/converter/undef.rb", "lib/ruby2js/converter/literal.rb", "lib/ruby2js/converter/var.rb", "lib/ruby2js.rb"]
|
12
|
+
s.files = ["ruby2js.gemspec", "README.md", "lib/ruby2js", "lib/ruby2js/execjs.rb", "lib/ruby2js/converter.rb", "lib/ruby2js/version.rb", "lib/ruby2js/filter", "lib/ruby2js/filter/jquery.rb", "lib/ruby2js/filter/return.rb", "lib/ruby2js/filter/underscore.rb", "lib/ruby2js/filter/angularrb.rb", "lib/ruby2js/filter/minitest-jasmine.rb", "lib/ruby2js/filter/camelCase.rb", "lib/ruby2js/filter/strict.rb", "lib/ruby2js/filter/functions.rb", "lib/ruby2js/filter/angular-resource.rb", "lib/ruby2js/filter/react.rb", "lib/ruby2js/filter/angular-route.rb", "lib/ruby2js/sinatra.rb", "lib/ruby2js/rails.rb", "lib/ruby2js/cgi.rb", "lib/ruby2js/converter", "lib/ruby2js/converter/self.rb", "lib/ruby2js/converter/arg.rb", "lib/ruby2js/converter/dstr.rb", "lib/ruby2js/converter/xstr.rb", "lib/ruby2js/converter/sym.rb", "lib/ruby2js/converter/return.rb", "lib/ruby2js/converter/break.rb", "lib/ruby2js/converter/for.rb", "lib/ruby2js/converter/defined.rb", "lib/ruby2js/converter/cvasgn.rb", "lib/ruby2js/converter/whilepost.rb", "lib/ruby2js/converter/logical.rb", "lib/ruby2js/converter/args.rb", "lib/ruby2js/converter/def.rb", "lib/ruby2js/converter/prototype.rb", "lib/ruby2js/converter/class.rb", "lib/ruby2js/converter/nthref.rb", "lib/ruby2js/converter/opasgn.rb", "lib/ruby2js/converter/module.rb", "lib/ruby2js/converter/kwbegin.rb", "lib/ruby2js/converter/send.rb", "lib/ruby2js/converter/boolean.rb", "lib/ruby2js/converter/masgn.rb", "lib/ruby2js/converter/hash.rb", "lib/ruby2js/converter/cvar.rb", "lib/ruby2js/converter/ivasgn.rb", "lib/ruby2js/converter/case.rb", "lib/ruby2js/converter/const.rb", "lib/ruby2js/converter/vasgn.rb", "lib/ruby2js/converter/untilpost.rb", "lib/ruby2js/converter/begin.rb", "lib/ruby2js/converter/ivar.rb", "lib/ruby2js/converter/while.rb", "lib/ruby2js/converter/next.rb", "lib/ruby2js/converter/nil.rb", "lib/ruby2js/converter/blockpass.rb", "lib/ruby2js/converter/super.rb", "lib/ruby2js/converter/defs.rb", "lib/ruby2js/converter/array.rb", "lib/ruby2js/converter/block.rb", "lib/ruby2js/converter/if.rb", "lib/ruby2js/converter/until.rb", "lib/ruby2js/converter/regexp.rb", "lib/ruby2js/converter/casgn.rb", "lib/ruby2js/converter/undef.rb", "lib/ruby2js/converter/literal.rb", "lib/ruby2js/converter/var.rb", "lib/ruby2js.rb"]
|
13
13
|
s.homepage = "http://github.com/rubys/ruby2js"
|
14
14
|
s.licenses = ["MIT"]
|
15
15
|
s.require_paths = ["lib"]
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby2js
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.13.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2015-
|
12
|
+
date: 2015-02-02 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: parser
|
@@ -36,6 +36,7 @@ extra_rdoc_files: []
|
|
36
36
|
files:
|
37
37
|
- ruby2js.gemspec
|
38
38
|
- README.md
|
39
|
+
- lib/ruby2js/execjs.rb
|
39
40
|
- lib/ruby2js/converter.rb
|
40
41
|
- lib/ruby2js/version.rb
|
41
42
|
- lib/ruby2js/filter/jquery.rb
|