ruby2js 2.0.9 → 2.0.10

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e245926a3e1b1904076667992a1df36b847290c8
4
- data.tar.gz: a97f75c7fffc309c239f93b82c07f83ddd659d8f
3
+ metadata.gz: aaae5d4b63645e55ef04dfd1794a1a75672cf5f6
4
+ data.tar.gz: ac98b88a02cc47c452d4bd919cdcf5bad553e88a
5
5
  SHA512:
6
- metadata.gz: 00b8b27362597b9c89450371072363c6468ce572f09f1e0ccacec551ac3a96bf418ba166b60031c73fefe5fdb4770dec6c419af3815f5dce5c61b22b4d0bb876
7
- data.tar.gz: 219e208e335a4d642205edb6c07537213d15dd25ba55d249dc1ffd6708a59d06b86e0619e23d722b98dd45f75c10bffdcbc844c5c8e5b26a29b5312cbbc0c077
6
+ metadata.gz: f4efbd1394b8f329e4208772589ed57ea15603df2f55b8addbf7a045d4872081897a4c18fb5759194da8e86d7facbc620ca6bc2dfbde9372e66d16a4f2edc769
7
+ data.tar.gz: aaef80bf7d518a15fe01fedfd8ced9c880b9f2027ff5f366d1c53aff2e0720180d176279005f636bad6782f106ffe48451160f095d1d63543aaba6f082072d62
data/README.md CHANGED
@@ -184,7 +184,6 @@ the script.
184
184
  * `.start_with?` becomes `.substring(0, arg.length) == arg`
185
185
  * `.strip` becomes `.trim`
186
186
  * `.sub` becomes `.replace`
187
- * `.to_a` becomes `to_Array`
188
187
  * `.to_f` becomes `parseFloat`
189
188
  * `.to_i` becomes `parseInt`
190
189
  * `.to_s` becomes `.to_String`
@@ -197,7 +196,10 @@ the script.
197
196
  * `.sub!` and `.gsub!` become equivalent `x = x.replace` statements
198
197
  * `.map!`, `.reverse!`, and `.select` become equivalent
199
198
  `.splice(0, .length, *.method())` statements
199
+ * `@foo.call(args)` becomes `this._foo(args)`
200
+ * `@@foo.call(args)` becomes `this.constructor._foo(args)`
200
201
  * `Array(x)` becomes `Array.prototype.slice.call(x)`
202
+ * `delete x` becomes `delete x` (note lack of parenthesis)
201
203
  * `setInterval` and `setTimeout` allow block to be treated as the
202
204
  first parameter on the call
203
205
  * for the following methods, if the block consists entirely of a simple
@@ -311,6 +313,7 @@ the script.
311
313
 
312
314
  * maps Ruby unary operator `~` to jQuery `$` function
313
315
  * maps Ruby attribute syntax to jquery attribute syntax
316
+ * `.to_a` becomes `toArray`
314
317
  * maps `$$` to jQuery `$` function
315
318
  * defaults the fourth parameter of $$.post to `"json"`, allowing Ruby block
316
319
  syntax to be used for the success function.
@@ -20,11 +20,25 @@ module Ruby2JS
20
20
  process S(:send, s(:attr, s(:const, nil, :Math), node.children[1]),
21
21
  :apply, s(:const, nil, :Math), target)
22
22
 
23
+ elsif method == :call and target and target.type == :ivar
24
+ process S(:send, s(:self), "_#{target.children.first.to_s[1..-1]}",
25
+ *args)
26
+
27
+ elsif method == :call and target and target.type == :cvar
28
+ process S(:send, s(:attr, s(:self), :constructor),
29
+ "_#{target.children.first.to_s[2..-1]}", *args)
30
+
23
31
  elsif method == :keys and args.length == 0 and node.is_method?
24
32
  process S(:send, s(:const, nil, :Object), :keys, target)
25
33
 
26
34
  elsif method == :delete and args.length == 1
27
- process S(:undef, S(:send, target, :[], args.first))
35
+ if not target
36
+ process S(:undef, args.first)
37
+ elsif args.first.type == :str
38
+ process S(:undef, S(:attr, target, args.first.children.first))
39
+ else
40
+ process S(:undef, S(:send, target, :[], args.first))
41
+ end
28
42
 
29
43
  elsif method == :to_s
30
44
  process S(:call, target, :toString, *args)
@@ -33,9 +47,6 @@ module Ruby2JS
33
47
  process S(:send, s(:attr, s(:attr, s(:const, nil, :Array),
34
48
  :prototype), :slice), :call, *args)
35
49
 
36
- elsif method == :to_a
37
- process S(:call, target, :toArray, *args)
38
-
39
50
  elsif method == :to_i
40
51
  process node.updated :send, [nil, :parseInt, target, *args]
41
52
 
@@ -172,7 +183,10 @@ module Ruby2JS
172
183
  end
173
184
  end
174
185
 
175
- if index.type == :regexp
186
+ if not index
187
+ super
188
+
189
+ elsif index.type == :regexp
176
190
  process S(:send, S(:send, target, :match, index), :[],
177
191
  args[1] || s(:int, 0))
178
192
 
@@ -274,6 +288,25 @@ module Ruby2JS
274
288
  # output: while(true) {statements}
275
289
  S(:while, s(:true), node.children[2])
276
290
 
291
+ elsif [:delete].include? call.children[1]
292
+ # restore delete methods that are prematurely mapped to undef
293
+ result = super
294
+
295
+ if result.children[0].type == :undef
296
+ call = result.children[0].children[0]
297
+ if call.type == :attr
298
+ call = call.updated(:send,
299
+ [call.children[0], :delete, s(:str, call.children[1])])
300
+ result = result.updated(nil, [call, *result.children[1..-1]])
301
+ else
302
+ call = call.updated(nil,
303
+ [call.children[0], :delete, *call.children[2..-1]])
304
+ result = result.updated(nil, [call, *result.children[1..-1]])
305
+ end
306
+ end
307
+
308
+ result
309
+
277
310
  else
278
311
  super
279
312
  end
@@ -75,7 +75,7 @@ module Ruby2JS
75
75
  end
76
76
 
77
77
  def on_send(node)
78
- if [:call, :[]].include? node.children[1]
78
+ if [:call, :[]].include? node.children[1] and node.children.first
79
79
  # map $$.call(..), $$.(..), and $$[...] to $(...)
80
80
  target = process(node.children.first)
81
81
  if target.type == :gvar and target.children == ['$']
@@ -84,6 +84,9 @@ module Ruby2JS
84
84
  super
85
85
  end
86
86
 
87
+ elsif node.children[1] == :to_a
88
+ process S(:call, node.children[0], :toArray, *node.children[2..-1])
89
+
87
90
  elsif node.children[1] == :~ and not @react
88
91
  # map ~expression.method to $(expression).method
89
92
 
@@ -8,11 +8,12 @@
8
8
  # spec/react_spec.rb contains a specification
9
9
  # demo/react-tutorial.rb contains a working sample
10
10
  #
11
- # Experimental conversions provided:
11
+ # Conversions provided:
12
12
  # * $x becomes this.refs.x
13
13
  # * @x becomes this.state.x
14
14
  # * @@x becomes this.props.x
15
- # * ~x becomes this.refs.x.getDOMNode()
15
+ # * ~x becomes this.refs.x
16
+ # * ~(x) becomes document.querySelector(x)
16
17
  # * ~"x" becomes document.querySelector("x")
17
18
  #
18
19
  require 'ruby2js'
@@ -22,27 +23,43 @@ module Ruby2JS
22
23
  module React
23
24
  include SEXP
24
25
 
25
- # the following code was used to generate ReactAttrs:
26
+ # the following command can be used to generate ReactAttrs:
27
+ #
28
+ # ruby -r ruby2js/filter/react -e "Ruby2JS::Filter::React.genAttrs"
26
29
  #
27
- # require 'nokogumbo'
28
- # page = 'https://facebook.github.io/react/docs/tags-and-attributes.html'
29
- # attrs = Nokogiri::HTML5.get(page).search('code').map(&:text).join(' ')
30
- # attrs = attrs.split(/\s+/).grep(/[A-Z]/).sort.uniq.join(' ')
31
- # puts "ReactAttrs = %w(#{attrs})".gsub(/(.{1,74})(\s+|\Z)/, "\\1\n")
30
+ def self.genAttrs
31
+ require 'nokogumbo'
32
+ page = 'https://facebook.github.io/react/docs/tags-and-attributes.html'
33
+ doc = Nokogiri::HTML5.get(page)
34
+
35
+ # delete contents of page prior to the list of supported attributes
36
+ attrs = doc.at('a[name=supported-attributes]')
37
+ attrs = attrs.parent while attrs and not attrs.name.start_with? 'h'
38
+ attrs.previous_sibling.remove while attrs and attrs.previous_sibling
39
+
40
+ # extract attribute names with uppercase chars from code and format
41
+ attrs = doc.search('code').map(&:text).join(' ')
42
+ attrs = attrs.split(/\s+/).grep(/[A-Z]/).sort.uniq.join(' ')
43
+ puts "ReactAttrs = %w(#{attrs})".gsub(/(.{1,72})(\s+|\Z)/, "\\1\n")
44
+ end
32
45
 
46
+ # list of react attributes that require special processing
33
47
  ReactAttrs = %w(acceptCharset accessKey allowFullScreen
34
48
  allowTransparency autoCapitalize autoComplete autoCorrect autoFocus
35
- autoPlay cellPadding cellSpacing charSet classID className clipPath
36
- colSpan contentEditable contextMenu crossOrigin dangerouslySetInnerHTML
37
- dateTime encType fillOpacity fontFamily fontSize formAction formEncType
38
- formMethod formNoValidate formTarget frameBorder gradientTransform
39
- gradientUnits hrefLang htmlFor httpEquiv itemID itemProp itemRef
40
- itemScope itemType linearGradient marginHeight marginWidth markerEnd
41
- markerMid markerStart maxLength mediaGroup noValidate
42
- patternContentUnits patternUnits preserveAspectRatio radialGradient
43
- radioGroup readOnly rowSpan spellCheck spreadMethod srcDoc srcSet
44
- stopColor stopOpacity strokeDasharray strokeLinecap strokeOpacity
45
- strokeWidth tabIndex textAnchor useMap viewBox)
49
+ autoPlay autoSave cellPadding cellSpacing charSet classID className
50
+ clipPath colSpan contentEditable contextMenu crossOrigin
51
+ dangerouslySetInnerHTML dateTime encType fillOpacity fontFamily fontSize
52
+ formAction formEncType formMethod formNoValidate formTarget frameBorder
53
+ gradientTransform gradientUnits hrefLang htmlFor httpEquiv inputMode
54
+ itemID itemProp itemRef itemScope itemType keyParams keyType
55
+ marginHeight marginWidth markerEnd markerMid markerStart maxLength
56
+ mediaGroup noValidate patternContentUnits patternUnits
57
+ preserveAspectRatio radioGroup readOnly rowSpan spellCheck spreadMethod
58
+ srcDoc srcSet stopColor stopOpacity strokeDasharray strokeLinecap
59
+ strokeOpacity strokeWidth tabIndex textAnchor useMap viewBox
60
+ xlinkActuate xlinkArcrole xlinkHref xlinkRole xlinkShow xlinkTitle
61
+ xlinkType xmlBase xmlLang xmlSpace)
62
+
46
63
  ReactAttrMap = Hash[ReactAttrs.map {|name| [name.downcase, name]}]
47
64
  ReactAttrMap['for'] = 'htmlFor'
48
65
 
@@ -197,31 +214,8 @@ module Ruby2JS
197
214
  end
198
215
  end
199
216
 
200
- # drill down if necessary to find the block
201
- while block.length==1 and block.first and block.first.type==:begin
202
- block = block.first.children.dup
203
- end
204
-
205
- # capture ivars that are both set and referenced
206
- @reactIvars[:pre].uniq.sort.reverse.each do |ivar|
207
- block.unshift(s(:lvasgn, "$#{ivar.to_s[1..-1]}",
208
- s(:attr, s(:attr, s(:self), :state), ivar.to_s[1..-1])))
209
- end
210
-
211
- # update ivars that are set and later referenced
212
- unless @reactIvars[:post].empty?
213
- updates = @reactIvars[:post].uniq.sort.reverse.map do |ivar|
214
- s(:pair, s(:lvar, ivar.to_s[1..-1]),
215
- s(:lvar, "$#{ivar.to_s[1..-1]}"))
216
- end
217
- update = s(:send, s(:self), :setState, s(:hash, *updates))
218
-
219
- if block.last.type == :return
220
- block.insert(block.length-1, update)
221
- else
222
- block.push(update)
223
- end
224
- end
217
+ # capture and update ivars as required
218
+ block = react_process_ivars(block)
225
219
 
226
220
  # add method to class
227
221
  type = (child.is_method? ? :begin : :autoreturn)
@@ -544,7 +538,7 @@ module Ruby2JS
544
538
  elsif node.children[1] == :~
545
539
  # Locate a DOM Node
546
540
  # map ~(expression) to document.querySelector(expression)
547
- # map ~name to this.refs.name.getDOMNode()
541
+ # map ~name to this.refs.name
548
542
  # map ~"a b" to document.querySelector("a b")
549
543
  # map ~"#a" to document.getElementById("a")
550
544
  # map ~"a" to document.getElementsByTagName("a")[0]
@@ -571,7 +565,7 @@ module Ruby2JS
571
565
  # before:
572
566
  # (send (send nil :a) :text) :~)
573
567
  # after:
574
- # (send (send (gvar :$a), :getDOMNode)), :text)
568
+ # (send (gvar :$a))), :text)
575
569
  if node.type == :send and node.children[0]
576
570
  if node.children[1] == :~ and node.children[0].children[1] == :~
577
571
  # consecutive tildes
@@ -592,9 +586,9 @@ module Ruby2JS
592
586
  end
593
587
  elsif node.children.first == nil and Symbol === node.children[1]
594
588
  # innermost expression is a scalar
595
- s(:send, s(:gvar, "$#{node.children[1]}"), :getDOMNode)
589
+ s(:gvar, "$#{node.children[1]}")
596
590
  elsif node.type == :lvar
597
- s(:send, s(:gvar, "$#{node.children[0]}"), :getDOMNode)
591
+ s(:gvar, "$#{node.children[0]}")
598
592
  elsif node.type == :str
599
593
  if node.children.first =~ /^#[-\w]+$/
600
594
  s(:send, s(:attr, nil, :document), :getElementById,
@@ -875,6 +869,7 @@ module Ruby2JS
875
869
  # as these create their own scopes.
876
870
  return if node.type == :pair and node.children[0].type == :sym and
877
871
  node.children[1].type == :block
872
+ return if node.type == :defs
878
873
 
879
874
  child = node.children.first
880
875
 
@@ -967,6 +962,53 @@ module Ruby2JS
967
962
  end
968
963
  node
969
964
  end
965
+
966
+ def on_defs(node)
967
+ return super unless @react
968
+
969
+ begin
970
+ reactIvars = @reactIvars
971
+ @reactIvars = {pre: [], post: [], asgn: [], ref: [], cond: []}
972
+ react_walk(node.children.last)
973
+ @reactIvars[:capture] = (@reactIvars[:pre] + @reactIvars[:post]).uniq
974
+ node = super
975
+ block = react_process_ivars([node.children.last.dup])
976
+ node.updated(nil, [*node.children[0..-2], s(:begin, *block)])
977
+ ensure
978
+ @reactIvars = reactIvars
979
+ end
980
+ end
981
+
982
+ # common logic for inserting code to manage state (ivars)
983
+ def react_process_ivars(block)
984
+ # drill down if necessary to find the block
985
+ while block.length==1 and block.first and block.first.type==:begin
986
+ block = block.first.children.dup
987
+ end
988
+
989
+ # capture ivars that are both set and referenced
990
+ @reactIvars[:pre].uniq.sort.reverse.each do |ivar|
991
+ block.unshift(s(:lvasgn, "$#{ivar.to_s[1..-1]}",
992
+ s(:attr, s(:attr, s(:self), :state), ivar.to_s[1..-1])))
993
+ end
994
+
995
+ # update ivars that are set and later referenced
996
+ unless @reactIvars[:post].empty?
997
+ updates = @reactIvars[:post].uniq.sort.reverse.map do |ivar|
998
+ s(:pair, s(:lvar, ivar.to_s[1..-1]),
999
+ s(:lvar, "$#{ivar.to_s[1..-1]}"))
1000
+ end
1001
+ update = s(:send, s(:self), :setState, s(:hash, *updates))
1002
+
1003
+ if block.last.type == :return
1004
+ block.insert(block.length-1, update)
1005
+ else
1006
+ block.push(update)
1007
+ end
1008
+ end
1009
+
1010
+ block
1011
+ end
970
1012
  end
971
1013
 
972
1014
  DEFAULTS.push React
@@ -2,7 +2,7 @@ module Ruby2JS
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 2
4
4
  MINOR = 0
5
- TINY = 9
5
+ TINY = 10
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.9 ruby lib
2
+ # stub: ruby2js 2.0.10 ruby lib
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "ruby2js"
6
- s.version = "2.0.9"
6
+ s.version = "2.0.10"
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-09-04"
11
+ s.date = "2015-12-13"
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.9
4
+ version: 2.0.10
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-09-04 00:00:00.000000000 Z
11
+ date: 2015-12-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: parser