ruby2js 2.0.11 → 2.0.12

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 59d4516cb826cddf572184ad24726edede34d3a0
4
- data.tar.gz: 2e04bc01f75f93290e4c355f86d526d0a7f3cb90
3
+ metadata.gz: 6c1a1b51c2433b2fe65efe814eb0e5b2e0e299a9
4
+ data.tar.gz: 6caa0926d512f05819c720a19015de7a37ba5a12
5
5
  SHA512:
6
- metadata.gz: 0e1a1726da1fa6d7dbf53436e72890163b05a374a9ff243738d139b6c6633af719d92cba7d4bc417f5eaa6caec3d8ea819204f241d43176bf08d2be6b4caf378
7
- data.tar.gz: d52b7d7b0170ea25de5605b691425479aba8210459d3ef8c8e9da3faddc2c34769b05bf8183bcd58ebc8dc4f18dcd84d261d18e2686068937b1d785afd4dcc6f
6
+ metadata.gz: 677c0dc3e1e87b4a0081a0e6acda2a6340435096c6d1609080abf3e996189ec487c8d545743337198d54e33d77f0def6fc446e06390dd261d586e361b0704055
7
+ data.tar.gz: a3d8b254ce2031faf24f2d24cd7affcc7d0a6de58f211a88b4187d642196507d6379dbac92746304f235a1cb8f47404b6459c658d6d3f64de377b86b0c55051a
data/README.md CHANGED
@@ -4,6 +4,7 @@ Ruby2js
4
4
  Minimal yet extensible Ruby to JavaScript conversion.
5
5
 
6
6
  [![Build Status](https://travis-ci.org/rubys/ruby2js.svg)](https://travis-ci.org/rubys/ruby2js)
7
+ [![Gem Version](https://badge.fury.io/rb/ruby2js.svg)](https://badge.fury.io/rb/ruby2js)
7
8
 
8
9
  Description
9
10
  ---
@@ -166,6 +167,7 @@ the script.
166
167
  * `.each_with_index` becomes `.forEach`
167
168
  * `.end_with?` becomes `.slice(-arg.length) == arg`
168
169
  * `.empty?` becomes `.length == 0`
170
+ * `.find_index` becomes `findIndex`
169
171
  * `.first` becomes `[0]`
170
172
  * `.first(n)` becomes `.slice(0, n)`
171
173
  * `.gsub` becomes `replace //g`
@@ -175,6 +177,7 @@ the script.
175
177
  * `.last` becomes `[*.length-1]`
176
178
  * `.last(n)` becomes `.slice(*.length-1, *.length)`
177
179
  * `.max` becomes `Math.max.apply(Math)`
180
+ * `.merge!` becomes `Object.assign()`
178
181
  * `.min` becomes `Math.min.apply(Math)`
179
182
  * `.nil?` becomes `== null`
180
183
  * `.ord` becomes `charCodeAt(0)`
@@ -182,6 +185,10 @@ the script.
182
185
  * `.replace` becomes `.length = 0; ...push.apply(*)`
183
186
  * `.respond_to?` becomes `right in left`
184
187
  * `.start_with?` becomes `.substring(0, arg.length) == arg`
188
+ * `.upto(lim)` becomes `for (var i=num; i<=lim; i+=1)`
189
+ * `.downto(lim)` becomes `for (var i=num; i>=lim; i-=1)`
190
+ * `.step(lim, n).each` becomes `for (var i=num; i<=lim; i+=n)`
191
+ * `.step(lim, -n).each` becomes `for (var i=num; i>=lim; i-=n)`
185
192
  * `.strip` becomes `.trim`
186
193
  * `.sub` becomes `.replace`
187
194
  * `.to_f` becomes `parseFloat`
@@ -204,7 +211,7 @@ the script.
204
211
  first parameter on the call
205
212
  * for the following methods, if the block consists entirely of a simple
206
213
  expression (or ends with one), a `return` is added prior to the
207
- expression: `sub`, `gsub`, `any?`, `all?`, `map`.
214
+ expression: `sub`, `gsub`, `any?`, `all?`, `map`, `find`, `find_index`.
208
215
  * New classes subclassed off of `Exception` will become subclassed off
209
216
  of `Error` instead; and default constructors will be provided
210
217
  * `loop do...end` will be replaced with `while (true) {...}`
@@ -308,6 +315,12 @@ the script.
308
315
  expression (or ends with one), a `return` is added prior to the
309
316
  expression: `reduce`, `sort_by`, `group_by`, `index_by`, `count_by`,
310
317
  `find`, `select`, `reject`.
318
+ * `is_a?` and `kind_of?` map to `Object.prototype.toString.call() ===
319
+ "[object #{type}]" for the following types: `Arguments`, `Boolean`,
320
+ `Date`, `Error`, `Function`, `Number`, `Object`, `RegExp`, `String`; and
321
+ maps Ruby names to JavaScript equivalents for `Exception`, `Float`,
322
+ `Hash`, `Proc`, and `Regexp`. Additionally, `is_a?` and `kind_of?` map
323
+ to `Array.isArray()` for `Array`.
311
324
 
312
325
  * <a id="jquery" href="https://github.com/rubys/ruby2js/blob/master/spec/jquery.rb">jquery</a>
313
326
 
@@ -20,8 +20,16 @@ module Ruby2JS
20
20
  expression = call.children.first.children.first
21
21
  comp = (expression.type == :irange ? '<=' : '<')
22
22
  put "for (var "; parse var; put " = "; parse expression.children.first
23
- put "; "; parse var; put " #{comp} "; parse expression.children.last
24
- put "; "; parse var; put " += "; parse call.children[2]; puts ") {"
23
+ put "; "; parse var;
24
+ if call.children[2].type == :int and call.children[2].children[0] < 0
25
+ put " #{comp.sub('<', '>')} "; parse expression.children.last
26
+ put "; "; parse var; put " -= "
27
+ parse s(:int, -call.children[2].children[0])
28
+ else
29
+ put " #{comp} "; parse expression.children.last
30
+ put "; "; parse var; put " += "; parse call.children[2]
31
+ end
32
+ puts ") {"
25
33
  scope block
26
34
  sput "}"
27
35
 
@@ -1,12 +1,20 @@
1
1
  module Ruby2JS
2
2
  class Converter
3
3
 
4
+ # (rescue
5
+ # (send nil :a)
6
+ # (resbody nil nil
7
+ # (send nil :b)) nil)
8
+ handle :rescue do |*statements|
9
+ parse_all s(:kwbegin, s(:rescue, *statements))
10
+ end
11
+
4
12
  # (kwbegin
5
13
  # (ensure
6
14
  # (rescue
7
- # (send nil :a)
8
- # (resbody nil nil
9
- # (send nil :b)) nil)
15
+ # (send nil :a)
16
+ # (resbody nil nil
17
+ # (send nil :b)) nil)
10
18
  # (send nil :c)))
11
19
 
12
20
  handle :kwbegin do |*children|
@@ -142,6 +142,13 @@ module Ruby2JS
142
142
  elsif args.length == 1 and args.first.type == :const
143
143
  # accommodation for JavaScript like new syntax w/o argument list
144
144
  parse s(:attr, args.first, :new), @state
145
+ elsif
146
+ args.length == 2 and [:send, :const].include? args.first.type and
147
+ args.last.type == :def and args.last.children.first == nil
148
+ then
149
+ # accommodation for JavaScript like new syntax with block
150
+ parse s(:send, s(:const, nil, args.first.children[1]), :new,
151
+ *args.first.children[2..-1], args.last), @state
145
152
  else
146
153
  raise NotImplementedError, "use of JavaScript keyword new"
147
154
  end
@@ -179,5 +186,29 @@ module Ruby2JS
179
186
  end
180
187
  end
181
188
  end
189
+
190
+ handle :csend do |receiver, method, *args|
191
+ node = @ast
192
+
193
+ # collect up chain of conditional sends
194
+ stack = []
195
+ while node.children.first.type == :csend
196
+ stack << node
197
+ node = node.children.first
198
+ end
199
+
200
+ # conditionally evaluate most nested expression
201
+ expr = node.updated(:send)
202
+ result = s(:and, node.children.first, expr)
203
+
204
+ # build up chain of conditional evaluations
205
+ until stack.empty?
206
+ node = stack.pop
207
+ expr = node.updated(:send, [expr, *node.children[1..-1]])
208
+ result = s(:and, result, expr)
209
+ end
210
+
211
+ parse result
212
+ end
182
213
  end
183
214
  end
@@ -31,6 +31,9 @@ module Ruby2JS
31
31
  elsif method == :keys and args.length == 0 and node.is_method?
32
32
  process S(:send, s(:const, nil, :Object), :keys, target)
33
33
 
34
+ elsif method == :merge!
35
+ process S(:send, s(:const, nil, :Object), :assign, target, *args)
36
+
34
37
  elsif method == :delete and args.length == 1
35
38
  if not target
36
39
  process S(:undef, args.first)
@@ -235,6 +238,32 @@ module Ruby2JS
235
238
  process S(:send, s(:send, s(:const, nil, :Array), :new,
236
239
  s(:send, args.first, :+, s(:int, 1))), :join, target)
237
240
 
241
+ elsif [:is_a?, :kind_of?].include? method and args.length == 1
242
+ if args[0].type == :const
243
+ parent = args[0].children.last
244
+ parent = :Number if parent == :Float
245
+ parent = :Object if parent == :Hash
246
+ parent = :Function if parent == :Proc
247
+ parent = :Error if parent == :Exception
248
+ parent = :RegExp if parent == :Regexp
249
+ if parent == :Array
250
+ S(:send, s(:const, nil, :Array), :isArray, target)
251
+ elsif [:Arguments, :Boolean, :Date, :Error, :Function, :Number,
252
+ :Object, :RegExp, :String].include? parent
253
+ S(:send, s(:send, s(:attr, s(:attr, s(:const, nil, Object),
254
+ :prototype), :toString), :call, target), :===,
255
+ s(:str, "[object #{parent.to_s}]"))
256
+ else
257
+ super
258
+ end
259
+ else
260
+ super
261
+ end
262
+
263
+ elsif target && target.type == :send and target.children[1] == :delete
264
+ # prevent chained delete methods from being converted to undef
265
+ S(:send, target.updated(:sendw), *node.children[1..-1])
266
+
238
267
  else
239
268
  super
240
269
  end
@@ -269,6 +298,15 @@ module Ruby2JS
269
298
  node.updated nil, [process(call), process(node.children[1]),
270
299
  s(:autoreturn, *process_all(node.children[2..-1]))]
271
300
 
301
+ elsif call.children[1] == :find and call.children.length == 2
302
+ node.updated nil, [process(call), process(node.children[1]),
303
+ s(:autoreturn, *process_all(node.children[2..-1]))]
304
+
305
+ elsif call.children[1] == :find_index and call.children.length == 2
306
+ call = call.updated nil, [call.children.first, :findIndex]
307
+ node.updated nil, [process(call), process(node.children[1]),
308
+ s(:autoreturn, *process_all(node.children[2..-1]))]
309
+
272
310
  elsif call.children[1] == :map and call.children.length == 2
273
311
  node.updated nil, [process(call), process(node.children[1]),
274
312
  s(:autoreturn, *process_all(node.children[2..-1]))]
@@ -288,7 +326,7 @@ module Ruby2JS
288
326
  # output: while(true) {statements}
289
327
  S(:while, s(:true), node.children[2])
290
328
 
291
- elsif [:delete].include? call.children[1]
329
+ elsif call.children[1] == :delete
292
330
  # restore delete methods that are prematurely mapped to undef
293
331
  result = super
294
332
 
@@ -307,6 +345,28 @@ module Ruby2JS
307
345
 
308
346
  result
309
347
 
348
+ elsif call.children[1] == :downto
349
+ range = s(:irange, call.children[0], call.children[2])
350
+ call = call.updated(nil, [s(:begin, range), :step, s(:int, -1)])
351
+ process node.updated(nil, [call, *node.children[1..-1]])
352
+
353
+ elsif call.children[1] == :upto
354
+ range = s(:irange, call.children[0], call.children[2])
355
+ call = call.updated(nil, [s(:begin, range), :step, s(:int, 1)])
356
+ process node.updated(nil, [call, *node.children[1..-1]])
357
+
358
+ elsif
359
+ call.children[1] == :each and call.children[0].type == :send and
360
+ call.children[0].children[1] == :step
361
+ then
362
+ # i.step(j, n).each {|v| ...}
363
+ range = call.children[0]
364
+ step = range.children[3] || s(:int, 1)
365
+ call = call.updated(nil, [s(:begin,
366
+ s(:irange, range.children[0], range.children[2])),
367
+ :step, step])
368
+ process node.updated(nil, [call, *node.children[1..-1]])
369
+
310
370
  else
311
371
  super
312
372
  end
@@ -394,6 +394,7 @@ module Ruby2JS
394
394
 
395
395
  # replace attribute names with case-sensitive javascript properties
396
396
  pairs.each_with_index do |pair, index|
397
+ next if pair.type == :kwsplat
397
398
  name = pair.children.first.children.first.downcase
398
399
  if ReactAttrMap[name] and name.to_s != ReactAttrMap[name]
399
400
  pairs[index] = pairs[index].updated(nil,
@@ -426,7 +427,11 @@ module Ruby2JS
426
427
  end
427
428
 
428
429
  # construct hash (or nil) from pairs
429
- hash = (pairs.length > 0 ? process(s(:hash, *pairs)) : s(:nil))
430
+ if pairs.length == 1 and pairs.first.type == :kwsplat
431
+ hash = pairs.first.children.first
432
+ else
433
+ hash = (pairs.length > 0 ? process(s(:hash, *pairs)) : s(:nil))
434
+ end
430
435
 
431
436
  # based on case of tag name, build a HTML tag or React component
432
437
  if tag =~ /^[A-Z]/
@@ -2,7 +2,7 @@ module Ruby2JS
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 2
4
4
  MINOR = 0
5
- TINY = 11
5
+ TINY = 12
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
data/ruby2js.gemspec CHANGED
@@ -1,14 +1,14 @@
1
1
  # -*- encoding: utf-8 -*-
2
- # stub: ruby2js 2.0.11 ruby lib
2
+ # stub: ruby2js 2.0.12 ruby lib
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "ruby2js"
6
- s.version = "2.0.11"
6
+ s.version = "2.0.12"
7
7
 
8
8
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
9
9
  s.require_paths = ["lib"]
10
10
  s.authors = ["Sam Ruby"]
11
- s.date = "2015-12-14"
11
+ s.date = "2015-12-26"
12
12
  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"
13
13
  s.email = "rubys@intertwingly.net"
14
14
  s.files = ["README.md", "lib/ruby2js", "lib/ruby2js.rb", "lib/ruby2js/cgi.rb", "lib/ruby2js/converter", "lib/ruby2js/converter.rb", "lib/ruby2js/converter/arg.rb", "lib/ruby2js/converter/args.rb", "lib/ruby2js/converter/array.rb", "lib/ruby2js/converter/begin.rb", "lib/ruby2js/converter/block.rb", "lib/ruby2js/converter/blockpass.rb", "lib/ruby2js/converter/boolean.rb", "lib/ruby2js/converter/break.rb", "lib/ruby2js/converter/case.rb", "lib/ruby2js/converter/casgn.rb", "lib/ruby2js/converter/class.rb", "lib/ruby2js/converter/const.rb", "lib/ruby2js/converter/cvar.rb", "lib/ruby2js/converter/cvasgn.rb", "lib/ruby2js/converter/def.rb", "lib/ruby2js/converter/defined.rb", "lib/ruby2js/converter/defs.rb", "lib/ruby2js/converter/dstr.rb", "lib/ruby2js/converter/for.rb", "lib/ruby2js/converter/hash.rb", "lib/ruby2js/converter/if.rb", "lib/ruby2js/converter/in.rb", "lib/ruby2js/converter/ivar.rb", "lib/ruby2js/converter/ivasgn.rb", "lib/ruby2js/converter/kwbegin.rb", "lib/ruby2js/converter/literal.rb", "lib/ruby2js/converter/logical.rb", "lib/ruby2js/converter/masgn.rb", "lib/ruby2js/converter/module.rb", "lib/ruby2js/converter/next.rb", "lib/ruby2js/converter/nil.rb", "lib/ruby2js/converter/nthref.rb", "lib/ruby2js/converter/opasgn.rb", "lib/ruby2js/converter/prototype.rb", "lib/ruby2js/converter/regexp.rb", "lib/ruby2js/converter/return.rb", "lib/ruby2js/converter/self.rb", "lib/ruby2js/converter/send.rb", "lib/ruby2js/converter/super.rb", "lib/ruby2js/converter/sym.rb", "lib/ruby2js/converter/undef.rb", "lib/ruby2js/converter/until.rb", "lib/ruby2js/converter/untilpost.rb", "lib/ruby2js/converter/var.rb", "lib/ruby2js/converter/vasgn.rb", "lib/ruby2js/converter/while.rb", "lib/ruby2js/converter/whilepost.rb", "lib/ruby2js/converter/xstr.rb", "lib/ruby2js/execjs.rb", "lib/ruby2js/filter", "lib/ruby2js/filter/angular-resource.rb", "lib/ruby2js/filter/angular-route.rb", "lib/ruby2js/filter/angularrb.rb", "lib/ruby2js/filter/camelCase.rb", "lib/ruby2js/filter/functions.rb", "lib/ruby2js/filter/jquery.rb", "lib/ruby2js/filter/minitest-jasmine.rb", "lib/ruby2js/filter/react.rb", "lib/ruby2js/filter/require.rb", "lib/ruby2js/filter/return.rb", "lib/ruby2js/filter/rubyjs.rb", "lib/ruby2js/filter/strict.rb", "lib/ruby2js/filter/underscore.rb", "lib/ruby2js/rails.rb", "lib/ruby2js/serializer.rb", "lib/ruby2js/sinatra.rb", "lib/ruby2js/version.rb", "ruby2js.gemspec"]
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: 2.0.11
4
+ version: 2.0.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Ruby
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-12-14 00:00:00.000000000 Z
11
+ date: 2015-12-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: parser