ruby2js 1.8.0 → 1.9.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/ruby2js.rb CHANGED
@@ -19,6 +19,7 @@ module Ruby2JS
19
19
  def on_attr(node); on_send(node); end
20
20
  def on_autoreturn(node); on_return(node); end
21
21
  def on_constructor(node); on_def(node); end
22
+ def on_defp(node); on_defs(node); end
22
23
  def on_method(node); on_send(node); end
23
24
  def on_prop(node); on_array(node); end
24
25
  def on_prototype(node); on_begin(node); end
@@ -100,9 +100,21 @@ module Parser
100
100
  class Node
101
101
  def is_method?
102
102
  return false if type == :attr
103
- return true if children.length > 2
104
103
  return true unless loc
105
- selector = loc.selector
104
+
105
+ if loc.respond_to? :selector
106
+ return true if children.length > 2
107
+ selector = loc.selector
108
+ elsif type == :defs
109
+ return true if children[1] =~ /[!?]$/
110
+ return true if children[2].children.length > 0
111
+ selector = loc.name
112
+ elsif type == :def
113
+ return true if children[0] =~ /[!?]$/
114
+ return true if children[1].children.length > 0
115
+ selector = loc.name
116
+ end
117
+
106
118
  return true unless selector.source_buffer
107
119
  selector.source_buffer.source[selector.end_pos] == '('
108
120
  end
@@ -7,7 +7,45 @@ module Ruby2JS
7
7
 
8
8
  handle :begin do |*statements|
9
9
  state = @state
10
+ props = false
11
+
12
+ statements.map! do |statement|
13
+ case statement and statement.type
14
+ when :defs, :defp
15
+ props = true
16
+ @ast = statement
17
+ transform_defs(*statement.children)
18
+ when :prop
19
+ props = true
20
+ statement
21
+ else
22
+ statement
23
+ end
24
+ end
25
+
26
+ if props
27
+ combine_properties(statements) if props
28
+ statements.compact!
29
+ end
30
+
10
31
  statements.map{ |statement| parse statement, state }.join(@sep)
11
32
  end
33
+
34
+ def combine_properties(body)
35
+ for i in 0...body.length-1
36
+ next unless body[i] and body[i].type == :prop
37
+ for j in i+1...body.length
38
+ break unless body[j] and body[j].type == :prop
39
+ if body[i].children[0] == body[j].children[0]
40
+ merge = Hash[(body[i].children[1].to_a+body[j].children[1].to_a).
41
+ group_by {|name, value| name.to_s}.map {|name, values|
42
+ [name, values.map(&:last).reduce(:merge)]}]
43
+ body[j] = s(:prop, body[j].children[0], merge)
44
+ body[i] = nil
45
+ break
46
+ end
47
+ end
48
+ end
49
+ end
12
50
  end
13
51
  end
@@ -35,10 +35,7 @@ module Ruby2JS
35
35
  {enumerable: s(:true), configurable: s(:true),
36
36
  set: s(:block, s(:send, nil, :proc), *m.children[1..-1])})
37
37
  else
38
- if m.children[1].children.length == 0 and
39
- m.children.first !~ /!/ and m.loc and m.loc.name and
40
- m.loc.name.source_buffer.source[m.loc.name.end_pos] != '('
41
-
38
+ if not m.is_method?
42
39
  # property getter
43
40
  s(:prop, s(:attr, name, :prototype), m.children.first =>
44
41
  {enumerable: s(:true), configurable: s(:true),
@@ -133,21 +130,8 @@ module Ruby2JS
133
130
 
134
131
  body.flatten!
135
132
 
136
- # merge nearby property definitions
137
- for i in 0...body.length-1
138
- next unless body[i] and body[i].type == :prop
139
- for j in i+1...body.length
140
- break unless body[j] and body[j].type == :prop
141
- if body[i].children[0] == body[j].children[0]
142
- merge = Hash[(body[i].children[1].to_a+body[j].children[1].to_a).
143
- group_by(&:first).map {|name, values|
144
- [name, values.map(&:last).reduce(:merge)]}]
145
- body[j] = s(:prop, body[j].children[0], merge)
146
- body[i] = nil
147
- break
148
- end
149
- end
150
- end
133
+ # merge property definitions
134
+ combine_properties(body)
151
135
 
152
136
  if inheritance
153
137
  body.unshift s(:send, name, :prototype=,
@@ -1,13 +1,31 @@
1
1
  module Ruby2JS
2
2
  class Converter
3
3
 
4
- # (def (self) :foo
4
+ # (defs (self) :foo
5
5
  # (args)
6
6
  # (...)
7
7
 
8
- handle :defs do |target, method, args, body|
9
- parse s(:send, target, "#{method}=",
10
- s(:block, s(:send, nil, :lambda), args, body))
8
+ # NOTE: defp is only produced by filters
9
+
10
+ handle :defs, :defp do |target, method, args, body|
11
+ parse transform_defs(target, method, args, body)
12
+ end
13
+
14
+ def transform_defs(target, method, args, body)
15
+ if not @ast.is_method? or @ast.type == :defp
16
+ s(:prop, target, method.to_s =>
17
+ {enumerable: s(:true), configurable: s(:true),
18
+ get: s(:block, s(:send, nil, :proc), args,
19
+ s(:autoreturn, body))})
20
+ elsif method =~ /=$/
21
+ s(:prop, target, method.to_s.sub('=', '') =>
22
+ {enumerable: s(:true), configurable: s(:true),
23
+ set: s(:block, s(:send, nil, :proc), args,
24
+ body)})
25
+ else
26
+ s(:send, target, "#{method}=",
27
+ s(:block, s(:send, nil, :lambda), args, body))
28
+ end
11
29
  end
12
30
  end
13
31
  end
@@ -10,7 +10,24 @@ module Ruby2JS
10
10
  # (...))
11
11
 
12
12
  handle :dstr, :dsym do |*children|
13
- children.map{ |child| parse child }.join(' + ')
13
+ children.map! do |child|
14
+ if child.type == :begin and child.children.length == 1
15
+ child = child.children.first
16
+ end
17
+
18
+ if child.type == :send
19
+ op_index = operator_index child.children[1]
20
+ if op_index >= operator_index(:+)
21
+ group child
22
+ else
23
+ parse child
24
+ end
25
+ else
26
+ parse child
27
+ end
28
+ end
29
+
30
+ children.join(' + ')
14
31
  end
15
32
  end
16
33
  end
@@ -542,7 +542,11 @@ module Ruby2JS
542
542
  # convert instance method definitions in controllers to $scope
543
543
  def on_def(node)
544
544
  if @ngContext == :controller
545
- process s(:defs, s(:gvar, :$scope), *node.children)
545
+ if node.is_method?
546
+ process s(:defs, s(:gvar, :$scope), *node.children)
547
+ else
548
+ process s(:defp, s(:gvar, :$scope), *node.children)
549
+ end
546
550
  else
547
551
  super
548
552
  end
@@ -1,7 +1,7 @@
1
1
  module Ruby2JS
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 1
4
- MINOR = 8
4
+ MINOR = 9
5
5
  TINY = 0
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
data/ruby2js.gemspec CHANGED
@@ -2,22 +2,22 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "ruby2js"
5
- s.version = "1.8.0"
5
+ s.version = "1.9.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 = "2014-04-02"
9
+ s.date = "2014-04-07"
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/rails.rb", "lib/ruby2js/version.rb", "lib/ruby2js/converter", "lib/ruby2js/converter/kwbegin.rb", "lib/ruby2js/converter/const.rb", "lib/ruby2js/converter/return.rb", "lib/ruby2js/converter/prototype.rb", "lib/ruby2js/converter/opasgn.rb", "lib/ruby2js/converter/xstr.rb", "lib/ruby2js/converter/args.rb", "lib/ruby2js/converter/defs.rb", "lib/ruby2js/converter/literal.rb", "lib/ruby2js/converter/array.rb", "lib/ruby2js/converter/if.rb", "lib/ruby2js/converter/nil.rb", "lib/ruby2js/converter/logical.rb", "lib/ruby2js/converter/next.rb", "lib/ruby2js/converter/while.rb", "lib/ruby2js/converter/whilepost.rb", "lib/ruby2js/converter/arg.rb", "lib/ruby2js/converter/case.rb", "lib/ruby2js/converter/break.rb", "lib/ruby2js/converter/hash.rb", "lib/ruby2js/converter/for.rb", "lib/ruby2js/converter/boolean.rb", "lib/ruby2js/converter/module.rb", "lib/ruby2js/converter/var.rb", "lib/ruby2js/converter/undef.rb", "lib/ruby2js/converter/blockpass.rb", "lib/ruby2js/converter/until.rb", "lib/ruby2js/converter/regexp.rb", "lib/ruby2js/converter/untilpost.rb", "lib/ruby2js/converter/masgn.rb", "lib/ruby2js/converter/cvasgn.rb", "lib/ruby2js/converter/block.rb", "lib/ruby2js/converter/super.rb", "lib/ruby2js/converter/ivar.rb", "lib/ruby2js/converter/send.rb", "lib/ruby2js/converter/vasgn.rb", "lib/ruby2js/converter/defined.rb", "lib/ruby2js/converter/def.rb", "lib/ruby2js/converter/sym.rb", "lib/ruby2js/converter/cvar.rb", "lib/ruby2js/converter/ivasgn.rb", "lib/ruby2js/converter/casgn.rb", "lib/ruby2js/converter/self.rb", "lib/ruby2js/converter/begin.rb", "lib/ruby2js/converter/dstr.rb", "lib/ruby2js/converter/class.rb", "lib/ruby2js/cgi.rb", "lib/ruby2js/converter.rb", "lib/ruby2js/filter", "lib/ruby2js/filter/return.rb", "lib/ruby2js/filter/strict.rb", "lib/ruby2js/filter/angularrb.rb", "lib/ruby2js/filter/underscore.rb", "lib/ruby2js/filter/angular-resource.rb", "lib/ruby2js/filter/functions.rb", "lib/ruby2js/filter/jquery.rb", "lib/ruby2js/filter/angular-route.rb", "lib/ruby2js/sinatra.rb", "lib/ruby2js.rb"]
12
+ s.files = ["ruby2js.gemspec", "README.md", "lib/ruby2js", "lib/ruby2js/sinatra.rb", "lib/ruby2js/converter.rb", "lib/ruby2js/filter", "lib/ruby2js/filter/angular-route.rb", "lib/ruby2js/filter/jquery.rb", "lib/ruby2js/filter/return.rb", "lib/ruby2js/filter/underscore.rb", "lib/ruby2js/filter/angularrb.rb", "lib/ruby2js/filter/strict.rb", "lib/ruby2js/filter/functions.rb", "lib/ruby2js/filter/angular-resource.rb", "lib/ruby2js/converter", "lib/ruby2js/converter/ivasgn.rb", "lib/ruby2js/converter/self.rb", "lib/ruby2js/converter/prototype.rb", "lib/ruby2js/converter/undef.rb", "lib/ruby2js/converter/casgn.rb", "lib/ruby2js/converter/nil.rb", "lib/ruby2js/converter/logical.rb", "lib/ruby2js/converter/ivar.rb", "lib/ruby2js/converter/super.rb", "lib/ruby2js/converter/class.rb", "lib/ruby2js/converter/for.rb", "lib/ruby2js/converter/arg.rb", "lib/ruby2js/converter/literal.rb", "lib/ruby2js/converter/cvasgn.rb", "lib/ruby2js/converter/dstr.rb", "lib/ruby2js/converter/return.rb", "lib/ruby2js/converter/defined.rb", "lib/ruby2js/converter/boolean.rb", "lib/ruby2js/converter/next.rb", "lib/ruby2js/converter/array.rb", "lib/ruby2js/converter/def.rb", "lib/ruby2js/converter/args.rb", "lib/ruby2js/converter/regexp.rb", "lib/ruby2js/converter/var.rb", "lib/ruby2js/converter/defs.rb", "lib/ruby2js/converter/if.rb", "lib/ruby2js/converter/masgn.rb", "lib/ruby2js/converter/hash.rb", "lib/ruby2js/converter/cvar.rb", "lib/ruby2js/converter/whilepost.rb", "lib/ruby2js/converter/break.rb", "lib/ruby2js/converter/opasgn.rb", "lib/ruby2js/converter/begin.rb", "lib/ruby2js/converter/xstr.rb", "lib/ruby2js/converter/vasgn.rb", "lib/ruby2js/converter/block.rb", "lib/ruby2js/converter/send.rb", "lib/ruby2js/converter/sym.rb", "lib/ruby2js/converter/case.rb", "lib/ruby2js/converter/kwbegin.rb", "lib/ruby2js/converter/const.rb", "lib/ruby2js/converter/module.rb", "lib/ruby2js/converter/until.rb", "lib/ruby2js/converter/untilpost.rb", "lib/ruby2js/converter/blockpass.rb", "lib/ruby2js/converter/while.rb", "lib/ruby2js/cgi.rb", "lib/ruby2js/version.rb", "lib/ruby2js/rails.rb", "lib/ruby2js.rb"]
13
13
  s.homepage = "http://github.com/rubys/ruby2js"
14
14
  s.licenses = ["MIT"]
15
15
  s.require_paths = ["lib"]
16
- s.rubygems_version = "2.0.7"
16
+ s.rubygems_version = "1.8.11"
17
17
  s.summary = "Minimal yet extensible Ruby to JavaScript conversion."
18
18
 
19
19
  if s.respond_to? :specification_version then
20
- s.specification_version = 4
20
+ s.specification_version = 3
21
21
 
22
22
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
23
23
  s.add_runtime_dependency(%q<parser>, [">= 0"])
metadata CHANGED
@@ -1,29 +1,27 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby2js
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.8.0
4
+ version: 1.9.0
5
+ prerelease:
5
6
  platform: ruby
6
7
  authors:
7
8
  - Sam Ruby
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2014-04-02 00:00:00.000000000 Z
12
+ date: 2014-04-07 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: parser
15
- requirement: !ruby/object:Gem::Requirement
16
+ requirement: &15788320 !ruby/object:Gem::Requirement
17
+ none: false
16
18
  requirements:
17
19
  - - ! '>='
18
20
  - !ruby/object:Gem::Version
19
21
  version: '0'
20
22
  type: :runtime
21
23
  prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - ! '>='
25
- - !ruby/object:Gem::Version
26
- version: '0'
24
+ version_requirements: *15788320
27
25
  description: ! " The base package maps Ruby syntax to JavaScript semantics.\n Filters
28
26
  may be provided to add Ruby-specific or framework specific\n behavior.\n"
29
27
  email: rubys@intertwingly.net
@@ -33,88 +31,89 @@ extra_rdoc_files: []
33
31
  files:
34
32
  - ruby2js.gemspec
35
33
  - README.md
36
- - lib/ruby2js/rails.rb
37
- - lib/ruby2js/version.rb
38
- - lib/ruby2js/converter/kwbegin.rb
39
- - lib/ruby2js/converter/const.rb
40
- - lib/ruby2js/converter/return.rb
34
+ - lib/ruby2js/sinatra.rb
35
+ - lib/ruby2js/converter.rb
36
+ - lib/ruby2js/filter/angular-route.rb
37
+ - lib/ruby2js/filter/jquery.rb
38
+ - lib/ruby2js/filter/return.rb
39
+ - lib/ruby2js/filter/underscore.rb
40
+ - lib/ruby2js/filter/angularrb.rb
41
+ - lib/ruby2js/filter/strict.rb
42
+ - lib/ruby2js/filter/functions.rb
43
+ - lib/ruby2js/filter/angular-resource.rb
44
+ - lib/ruby2js/converter/ivasgn.rb
45
+ - lib/ruby2js/converter/self.rb
41
46
  - lib/ruby2js/converter/prototype.rb
42
- - lib/ruby2js/converter/opasgn.rb
43
- - lib/ruby2js/converter/xstr.rb
44
- - lib/ruby2js/converter/args.rb
45
- - lib/ruby2js/converter/defs.rb
46
- - lib/ruby2js/converter/literal.rb
47
- - lib/ruby2js/converter/array.rb
48
- - lib/ruby2js/converter/if.rb
47
+ - lib/ruby2js/converter/undef.rb
48
+ - lib/ruby2js/converter/casgn.rb
49
49
  - lib/ruby2js/converter/nil.rb
50
50
  - lib/ruby2js/converter/logical.rb
51
- - lib/ruby2js/converter/next.rb
52
- - lib/ruby2js/converter/while.rb
53
- - lib/ruby2js/converter/whilepost.rb
54
- - lib/ruby2js/converter/arg.rb
55
- - lib/ruby2js/converter/case.rb
56
- - lib/ruby2js/converter/break.rb
57
- - lib/ruby2js/converter/hash.rb
51
+ - lib/ruby2js/converter/ivar.rb
52
+ - lib/ruby2js/converter/super.rb
53
+ - lib/ruby2js/converter/class.rb
58
54
  - lib/ruby2js/converter/for.rb
55
+ - lib/ruby2js/converter/arg.rb
56
+ - lib/ruby2js/converter/literal.rb
57
+ - lib/ruby2js/converter/cvasgn.rb
58
+ - lib/ruby2js/converter/dstr.rb
59
+ - lib/ruby2js/converter/return.rb
60
+ - lib/ruby2js/converter/defined.rb
59
61
  - lib/ruby2js/converter/boolean.rb
60
- - lib/ruby2js/converter/module.rb
61
- - lib/ruby2js/converter/var.rb
62
- - lib/ruby2js/converter/undef.rb
63
- - lib/ruby2js/converter/blockpass.rb
64
- - lib/ruby2js/converter/until.rb
62
+ - lib/ruby2js/converter/next.rb
63
+ - lib/ruby2js/converter/array.rb
64
+ - lib/ruby2js/converter/def.rb
65
+ - lib/ruby2js/converter/args.rb
65
66
  - lib/ruby2js/converter/regexp.rb
66
- - lib/ruby2js/converter/untilpost.rb
67
+ - lib/ruby2js/converter/var.rb
68
+ - lib/ruby2js/converter/defs.rb
69
+ - lib/ruby2js/converter/if.rb
67
70
  - lib/ruby2js/converter/masgn.rb
68
- - lib/ruby2js/converter/cvasgn.rb
71
+ - lib/ruby2js/converter/hash.rb
72
+ - lib/ruby2js/converter/cvar.rb
73
+ - lib/ruby2js/converter/whilepost.rb
74
+ - lib/ruby2js/converter/break.rb
75
+ - lib/ruby2js/converter/opasgn.rb
76
+ - lib/ruby2js/converter/begin.rb
77
+ - lib/ruby2js/converter/xstr.rb
78
+ - lib/ruby2js/converter/vasgn.rb
69
79
  - lib/ruby2js/converter/block.rb
70
- - lib/ruby2js/converter/super.rb
71
- - lib/ruby2js/converter/ivar.rb
72
80
  - lib/ruby2js/converter/send.rb
73
- - lib/ruby2js/converter/vasgn.rb
74
- - lib/ruby2js/converter/defined.rb
75
- - lib/ruby2js/converter/def.rb
76
81
  - lib/ruby2js/converter/sym.rb
77
- - lib/ruby2js/converter/cvar.rb
78
- - lib/ruby2js/converter/ivasgn.rb
79
- - lib/ruby2js/converter/casgn.rb
80
- - lib/ruby2js/converter/self.rb
81
- - lib/ruby2js/converter/begin.rb
82
- - lib/ruby2js/converter/dstr.rb
83
- - lib/ruby2js/converter/class.rb
82
+ - lib/ruby2js/converter/case.rb
83
+ - lib/ruby2js/converter/kwbegin.rb
84
+ - lib/ruby2js/converter/const.rb
85
+ - lib/ruby2js/converter/module.rb
86
+ - lib/ruby2js/converter/until.rb
87
+ - lib/ruby2js/converter/untilpost.rb
88
+ - lib/ruby2js/converter/blockpass.rb
89
+ - lib/ruby2js/converter/while.rb
84
90
  - lib/ruby2js/cgi.rb
85
- - lib/ruby2js/converter.rb
86
- - lib/ruby2js/filter/return.rb
87
- - lib/ruby2js/filter/strict.rb
88
- - lib/ruby2js/filter/angularrb.rb
89
- - lib/ruby2js/filter/underscore.rb
90
- - lib/ruby2js/filter/angular-resource.rb
91
- - lib/ruby2js/filter/functions.rb
92
- - lib/ruby2js/filter/jquery.rb
93
- - lib/ruby2js/filter/angular-route.rb
94
- - lib/ruby2js/sinatra.rb
91
+ - lib/ruby2js/version.rb
92
+ - lib/ruby2js/rails.rb
95
93
  - lib/ruby2js.rb
96
94
  homepage: http://github.com/rubys/ruby2js
97
95
  licenses:
98
96
  - MIT
99
- metadata: {}
100
97
  post_install_message:
101
98
  rdoc_options: []
102
99
  require_paths:
103
100
  - lib
104
101
  required_ruby_version: !ruby/object:Gem::Requirement
102
+ none: false
105
103
  requirements:
106
104
  - - ! '>='
107
105
  - !ruby/object:Gem::Version
108
106
  version: '0'
109
107
  required_rubygems_version: !ruby/object:Gem::Requirement
108
+ none: false
110
109
  requirements:
111
110
  - - ! '>='
112
111
  - !ruby/object:Gem::Version
113
112
  version: '0'
114
113
  requirements: []
115
114
  rubyforge_project:
116
- rubygems_version: 2.0.7
115
+ rubygems_version: 1.8.11
117
116
  signing_key:
118
- specification_version: 4
117
+ specification_version: 3
119
118
  summary: Minimal yet extensible Ruby to JavaScript conversion.
120
119
  test_files: []
checksums.yaml DELETED
@@ -1,15 +0,0 @@
1
- ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- ZTQ1ZGU1ZTI0NWQ4ZmMwNzNiMjExYjk1MWY5YWJkNTY1MzYzNTQwYg==
5
- data.tar.gz: !binary |-
6
- MmI0MjVhYjkwNzg5YTUxZTgzMDhhYWE3NjM0MmU2NmIyNjQ4MGJkYw==
7
- !binary "U0hBNTEy":
8
- metadata.gz: !binary |-
9
- NzZmNjgxYzU1YmFjZTU4ZmI0MWJkY2I1YmZjN2YxN2RmOTkzOGRhYzNmN2Rm
10
- ZWIzMTIxYWM1NmYxOTRjOTI5MjA5NzY3MTc4Yjk4MWM0MDQ0YTNjMTc3NGEy
11
- NDk0NGE1ZjFkOTQ3YzRlNzAzNmVkYjZmMTAxNmIzZTNkYTlhODU=
12
- data.tar.gz: !binary |-
13
- OTc5OTkwNmY2Yzc1MzgxMWNkN2M5YjFlYzAyNDE3N2M5MTllMTE4ZjA0MWI3
14
- NjlmOWM0ODQ3NTAzZWY1YzFiNWFkMWVhOTE5OThiNmY1Y2UwZjRmYmVkNzll
15
- MTU0OTQ0YTU3NzVkNGQ1NGJlNzYzMjgzNzBkMWYwMWM1ZjAxOWE=