ruby2js 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/README.md +13 -1
- data/lib/ruby2js.rb +8 -2
- data/lib/ruby2js/converter.rb +7 -0
- data/lib/ruby2js/filter/angularrb.rb +2 -1
- data/lib/ruby2js/filter/functions.rb +20 -5
- data/lib/ruby2js/filter/return.rb +2 -2
- data/lib/ruby2js/version.rb +1 -1
- data/ruby2js.gemspec +3 -2
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
YzA1Mzk5YzBmMWU5OWVhNTQ1MzdlODkwMmE0ZTAyMzhkYjkzZTQzOQ==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
NzA2YmI3ODFkM2I0Y2I0ZjY1MTFiOTBkNDIxMTE5ZTM5ODdiNzU3ZA==
|
7
7
|
!binary "U0hBNTEy":
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
NzY0MjhhMzA5YTI0NzVkZWMzYTQxYzBlOTQ1ZjIyZmE5MTkwNmM3ZTViOTA4
|
10
|
+
OGE0ZDNhY2NjYjk3ZDliNDYxOGRmNWY5NDZjYzUyNjBjMmVjNjFkOGMxOWUz
|
11
|
+
NjA5ZDI5MmVlZGYyNzdlMmUwYmJkMDhlNmEyYjNhYTYzZTEyOWI=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
YzAxMzRhMmY2NGRkMzZlYWNiYjIzZDkyMWM4ZGYyYWYzODRlZGUzMjdkMjQ1
|
14
|
+
ZTAyNzUxOTA4ZTViZmQyZGZiZTg2Y2U3NTk5NDU1NjA2OWVlN2M2YTEyOTQ5
|
15
|
+
ZGI1NTczNDhjNmJlZDNmNjRiMzg1YTljY2EzNmJkMTllZGE3Y2Q=
|
data/README.md
CHANGED
@@ -20,11 +20,23 @@ an AST representation of the code.
|
|
20
20
|
Synopsis
|
21
21
|
---
|
22
22
|
|
23
|
+
Basic:
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
require 'ruby2js'
|
27
|
+
puts Ruby2JS.convert("a={age:3}\na.age+=1")
|
28
|
+
```
|
29
|
+
|
30
|
+
With filter:
|
31
|
+
|
23
32
|
```ruby
|
24
33
|
require 'ruby2js/filter/functions'
|
25
|
-
puts Ruby2JS.convert('"2A".to_i(16)'
|
34
|
+
puts Ruby2JS.convert('"2A".to_i(16)')
|
26
35
|
```
|
27
36
|
|
37
|
+
Conversions can be explored interactively using the
|
38
|
+
[demo](https://github.com/rubys/ruby2js/blob/master/demo/ruby2js.rb) provided.
|
39
|
+
|
28
40
|
License
|
29
41
|
---
|
30
42
|
|
data/lib/ruby2js.rb
CHANGED
@@ -2,6 +2,10 @@ require 'parser/current'
|
|
2
2
|
require 'ruby2js/converter'
|
3
3
|
|
4
4
|
module Ruby2JS
|
5
|
+
module Filter
|
6
|
+
DEFAULTS = []
|
7
|
+
end
|
8
|
+
|
5
9
|
def self.convert(source, options={})
|
6
10
|
|
7
11
|
if Proc === source
|
@@ -15,9 +19,11 @@ module Ruby2JS
|
|
15
19
|
ast = parse( source )
|
16
20
|
end
|
17
21
|
|
18
|
-
|
22
|
+
filters = options[:filters] || Filter::DEFAULTS
|
23
|
+
|
24
|
+
unless filters.empty?
|
19
25
|
filter = Parser::AST::Processor
|
20
|
-
|
26
|
+
filters.reverse.each do |mod|
|
21
27
|
filter = Class.new(filter) {include mod}
|
22
28
|
end
|
23
29
|
ast = filter.new.process(ast)
|
data/lib/ruby2js/converter.rb
CHANGED
@@ -231,6 +231,13 @@ module Ruby2JS
|
|
231
231
|
condition, block = ast.children
|
232
232
|
"while (#{ parse condition }) {#@nl#{ scope block }#@nl}"
|
233
233
|
|
234
|
+
when :for
|
235
|
+
var, expression, block = ast.children
|
236
|
+
parse s(:block,
|
237
|
+
s(:send, expression, :forEach),
|
238
|
+
s(:args, s(:arg, var.children.last)),
|
239
|
+
block);
|
240
|
+
|
234
241
|
when :block
|
235
242
|
call, args, block = ast.children
|
236
243
|
block ||= s(:begin)
|
@@ -14,16 +14,29 @@ module Ruby2JS
|
|
14
14
|
end
|
15
15
|
|
16
16
|
def on_send(node)
|
17
|
+
target = process(node.children.first)
|
18
|
+
args = process_all(node.children[2..-1])
|
19
|
+
|
17
20
|
if node.children[1] == :to_s
|
18
|
-
s(:send,
|
21
|
+
s(:send, target, :toString, *args)
|
19
22
|
|
20
23
|
elsif node.children[1] == :to_i
|
21
|
-
node.updated nil, [nil, :parseInt,
|
22
|
-
*node.children[2..-1]]
|
24
|
+
node.updated nil, [nil, :parseInt, target, *args]
|
23
25
|
|
24
26
|
elsif node.children[1] == :to_f
|
25
|
-
node.updated nil, [nil, :parseFloat,
|
26
|
-
|
27
|
+
node.updated nil, [nil, :parseFloat, target, *args]
|
28
|
+
|
29
|
+
elsif node.children[1] == :each
|
30
|
+
if target.type == :gvar and target.children == ['$']
|
31
|
+
super
|
32
|
+
elsif target.type == :send and target.children == [nil, :jQuery]
|
33
|
+
super
|
34
|
+
else
|
35
|
+
node.updated nil, [target, :forEach, *args]
|
36
|
+
end
|
37
|
+
|
38
|
+
elsif node.children[1] == :each_with_index
|
39
|
+
node.updated nil, [target, :forEach, *args]
|
27
40
|
|
28
41
|
else
|
29
42
|
super
|
@@ -37,5 +50,7 @@ module Ruby2JS
|
|
37
50
|
Parser::AST::Node.new type, args
|
38
51
|
end
|
39
52
|
end
|
53
|
+
|
54
|
+
DEFAULTS.push Functions
|
40
55
|
end
|
41
56
|
end
|
@@ -16,8 +16,6 @@ module Ruby2JS
|
|
16
16
|
|
17
17
|
if EXPRESSIONS.include? block.last.type
|
18
18
|
block.push s(:return, block.pop)
|
19
|
-
else
|
20
|
-
p block.last.type
|
21
19
|
end
|
22
20
|
|
23
21
|
if block.length == 1
|
@@ -59,5 +57,7 @@ p block.last.type
|
|
59
57
|
Parser::AST::Node.new type, args
|
60
58
|
end
|
61
59
|
end
|
60
|
+
|
61
|
+
DEFAULTS.push Return
|
62
62
|
end
|
63
63
|
end
|
data/lib/ruby2js/version.rb
CHANGED
data/ruby2js.gemspec
CHANGED
@@ -2,15 +2,16 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = "ruby2js"
|
5
|
-
s.version = "0.
|
5
|
+
s.version = "0.2.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 = "2013-11-
|
9
|
+
s.date = "2013-11-12"
|
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
12
|
s.files = ["ruby2js.gemspec", "README.md", "lib/ruby2js", "lib/ruby2js/version.rb", "lib/ruby2js/converter.rb", "lib/ruby2js/filter", "lib/ruby2js/filter/return.rb", "lib/ruby2js/filter/angularrb.rb", "lib/ruby2js/filter/functions.rb", "lib/ruby2js.rb"]
|
13
13
|
s.homepage = "http://github.com/rubys/ruby2js"
|
14
|
+
s.licenses = ["MIT"]
|
14
15
|
s.require_paths = ["lib"]
|
15
16
|
s.rubygems_version = "2.0.7"
|
16
17
|
s.summary = "Minimal yet extensible Ruby to JavaScript conversion."
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby2js
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sam Ruby
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-11-
|
11
|
+
date: 2013-11-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: parser
|
@@ -40,7 +40,8 @@ files:
|
|
40
40
|
- lib/ruby2js/filter/functions.rb
|
41
41
|
- lib/ruby2js.rb
|
42
42
|
homepage: http://github.com/rubys/ruby2js
|
43
|
-
licenses:
|
43
|
+
licenses:
|
44
|
+
- MIT
|
44
45
|
metadata: {}
|
45
46
|
post_install_message:
|
46
47
|
rdoc_options: []
|