ruby2js 3.0.8 → 3.0.9

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
  SHA256:
3
- metadata.gz: 2990f20dc46745e6e632c5cf672ce516f3ca64819a9129024efd7cc333714638
4
- data.tar.gz: 77df7775a4146e1e45278002737c24304b4d4f0ccdb49825c372286cfbb2a44b
3
+ metadata.gz: 075b7a63226221fc941c3ccd747285d1bf9a3e94cfefd39926e530c9e41ba0a0
4
+ data.tar.gz: 7a01d0dfef5bba7c05393a4059f4899631d6bac7894b98801dfc04634b4fcb84
5
5
  SHA512:
6
- metadata.gz: 3e0fd19ebc555112ea283f6d112cac30c47c16c3701568acd725d70115c2fe3aa9ba5870145d1ec002f8d7c7f463f26a0fe5fce9f61eb4492539638b938a8219
7
- data.tar.gz: 8423b871acb6c37b44b4d5e2878fc8b8eba3c3114768b37a8003cccafabf137253b02f4e55c13aea447521a944662b1554d848834fe5ec61e07381dfc967d3de
6
+ metadata.gz: 62e0c730806b6bb3dc2060028d37dee4a181c9aa43be77415ed9cfca016ebae821388ae1b696d6de9a1f634730163b2a2f9d3b95aac321ca6a11ddfabbc1383e
7
+ data.tar.gz: ae0a44b9d2a547def9fcc86b5af2dd1e5134881b0224c4e65a80e614215b1803a6be80649270622113829f531070aacfceed99a2edca364f661f2c75521fb214
@@ -47,7 +47,15 @@ module Ruby2JS
47
47
  end
48
48
 
49
49
  parse code, :statement
50
- put "#{@sep}break#@sep" if other or index < whens.length-1
50
+ last = code
51
+ while last.type == :begin
52
+ last = last.children.last
53
+ end
54
+
55
+ if other or index < whens.length-1
56
+ put "#{@sep}"
57
+ put "break#@sep" unless last.type == :return
58
+ end
51
59
  end
52
60
 
53
61
  (put "#{@nl}default:#@ws"; parse other, :statement) if other
@@ -38,6 +38,8 @@ module Ruby2JS
38
38
  skipped = false
39
39
  body.each_with_index do |m, index|
40
40
  put(index == 0 ? @nl : @sep) unless skipped
41
+ comments = comments(m)
42
+ location = output_location
41
43
  skipped = false
42
44
 
43
45
  # intercept async definitions
@@ -136,7 +138,11 @@ module Ruby2JS
136
138
  end
137
139
  end
138
140
 
139
- post << m if skipped
141
+ if skipped
142
+ post << m if skipped
143
+ else
144
+ comments.reverse.each {|comment| insert location, comment}
145
+ end
140
146
  end
141
147
 
142
148
  put @nl unless skipped
@@ -8,9 +8,6 @@ module Ruby2JS
8
8
 
9
9
  handle :def, :defm, :async do |name, args, body=nil|
10
10
  body ||= s(:begin)
11
- if name =~ /[!?]$/
12
- raise Error.new("invalid method name #{ name }", @ast)
13
- end
14
11
 
15
12
  vars = {}
16
13
  vars.merge! @vars unless name
@@ -99,6 +96,7 @@ module Ruby2JS
99
96
  while expr.type == :begin and expr.children.length == 1
100
97
  expr = expr.children.first
101
98
  end
99
+ expr = expr.children.first if expr.type == :return
102
100
 
103
101
  if EXPRESSIONS.include? expr.type
104
102
  if expr.type == :send and expr.children[0..1] == [nil, :raise]
@@ -131,7 +129,7 @@ module Ruby2JS
131
129
  put @prop
132
130
  @prop = nil
133
131
  elsif name
134
- put "function #{name}"
132
+ put "function #{name.to_s.sub(/[?!]$/, '')}"
135
133
  else
136
134
  put 'function'
137
135
  end
@@ -11,29 +11,30 @@ module Ruby2JS
11
11
 
12
12
  handle :dstr, :dsym do |*children|
13
13
  if es2015
14
- put '`'
15
-
16
14
  # gather length of string parts; if long enough, newlines will
17
- # not be escaped
18
- length = children.select {|child| child.type==:str}.
19
- map {|child| child.children.last.length}.inject(:+)
20
-
21
- children.each do |child|
22
- if child.type == :str
23
- str = child.children.first.inspect[1..-2].gsub('${', '$\{')
24
- if length > 40
25
- put str.gsub("\\n", "\n")
26
- else
27
- put str
28
- end
29
- else
30
- put '${'
31
- parse child
32
- put '}'
33
- end
34
- end
35
- put '`'
36
- return
15
+ # not be escaped (poor man's HEREDOC)
16
+ strings = children.select {|child| child.type==:str}.
17
+ map {|child| child.children.last}.join
18
+ heredoc = (strings.length > 40 and strings.scan("\n").length > 3)
19
+
20
+ put '`'
21
+ children.each do |child|
22
+ if child.type == :str
23
+ str = child.children.first.inspect[1..-2].gsub('${', '$\{')
24
+ if heredoc
25
+ put! str.gsub("\\n", "\n")
26
+ else
27
+ put str
28
+ end
29
+ else
30
+ put '${'
31
+ parse child
32
+ put '}'
33
+ end
34
+ end
35
+ put '`'
36
+
37
+ return
37
38
  end
38
39
 
39
40
  children.each_with_index do |child, index|
@@ -45,10 +45,24 @@ module Ruby2JS
45
45
  end
46
46
  put "] = "
47
47
  parse rhs
48
+
49
+ elsif rhs.type == :array
50
+
51
+ if lhs.children.length == rhs.children.length
52
+ block = []
53
+ lhs.children.zip rhs.children.zip do |var, val|
54
+ block << s(var.type, *var.children, *val)
55
+ end
56
+ parse s(:begin, *block), @state
57
+ else
58
+ raise Error.new("unmatched assignment", @ast)
59
+ end
60
+
48
61
  else
62
+
49
63
  block = []
50
- lhs.children.zip rhs.children.zip do |var, val|
51
- block << s(var.type, *var.children, *val)
64
+ lhs.children.each_with_index do |var, i|
65
+ block << s(var.type, *var.children, s(:send, rhs, :[], s(:int, i)))
52
66
  end
53
67
  parse s(:begin, *block), @state
54
68
  end
@@ -6,36 +6,44 @@ module Ruby2JS
6
6
  # (regopt :i))
7
7
 
8
8
  handle :regexp do |*parts, opt|
9
+ # remove "extended" from list of options
9
10
  extended = false
10
11
  opts = opt.children
11
12
  if opts.include? :x
12
- opts = opts.dup - [:x]
13
+ opts = opts - [:x]
13
14
  extended = true
14
15
  end
15
16
 
16
- if parts.all? {|part| part.type == :str}
17
- str = parts.map {|part| part.children.first}.join
18
- str = str.gsub(/ #.*/,'').gsub(/\s/,'') if extended
19
- unless str.include? '/'
20
- return put "/#{ str }/#{ opts.join }"
21
- end
22
- put "new RegExp(#{ str.inspect }"
23
- else
24
- put 'new RegExp('
25
-
26
- parts.each_with_index do |part, index|
27
- put ' + ' unless index == 0
28
-
17
+ # remove whitespace and comments from extended regular expressions
18
+ if extended
19
+ parts.map! do |part|
29
20
  if part.type == :str
30
21
  str = part.children.first
31
- str = str.gsub(/ #.*/,'').gsub(/\s/,'') if extended
32
- put str.inspect
22
+ str = str.gsub(/ #.*/,'').gsub(/\s/,'')
23
+ s(:str, str)
33
24
  else
34
- parse part
25
+ part
35
26
  end
36
27
  end
37
28
  end
38
29
 
30
+ # use slash syntax if there are few embedded slashes in the regexp
31
+ if parts.all? {|part| part.type == :str}
32
+ str = parts.map {|part| part.children.first}.join
33
+ unless str.scan('/').length - str.scan("\\").length > 3
34
+ return put "/#{ str.gsub('/', '\\/') }/#{ opts.join }"
35
+ end
36
+ end
37
+
38
+ # create a new RegExp object
39
+ put 'new RegExp('
40
+
41
+ if parts.length == 1
42
+ parse parts.first
43
+ else
44
+ parse s(:dstr, *parts)
45
+ end
46
+
39
47
  unless opts.empty?
40
48
  put ", #{ opts.join.inspect}"
41
49
  end
@@ -26,6 +26,7 @@ module Ruby2JS
26
26
  return if block == []
27
27
  if EXPRESSIONS.include? block.last.type
28
28
  block.push @ast.updated(:return, [block.pop])
29
+
29
30
  elsif block.last.type == :if
30
31
  node = block.pop
31
32
  if node.children[1] and node.children[2] and
@@ -49,6 +50,24 @@ module Ruby2JS
49
50
  end
50
51
  end
51
52
  block.push node
53
+
54
+ elsif block.last.type == :case
55
+ node = block.pop
56
+ children = node.children.dup
57
+ (1...children.length).each do |i|
58
+ if children[i].type == :when
59
+ gchildren = children[i].children.dup
60
+ if !gchildren.empty? and EXPRESSIONS.include? gchildren.last.type
61
+ gchildren.push s(:return, gchildren.pop)
62
+ children[i] = children[i].updated(nil, gchildren)
63
+ else
64
+ end
65
+ elsif EXPRESSIONS.include? children[i].type
66
+ children[i] = children[i].updated(:return, [children[i]])
67
+ end
68
+ end
69
+ block.push node.updated(nil, children)
70
+
52
71
  elsif block.last.type == :lvasgn
53
72
  block.push s(:return, s(:lvar, block.last.children.first))
54
73
  elsif block.last.type == :ivasgn
@@ -16,6 +16,13 @@ module Ruby2JS
16
16
  handle :send, :sendw, :await, :attr, :call do |receiver, method, *args|
17
17
  ast = @ast
18
18
 
19
+ if
20
+ args.length == 1 and method == :+
21
+ then
22
+ node = collapse_strings(ast)
23
+ return parse node if node != ast
24
+ end
25
+
19
26
  # strip '!' and '?' decorations
20
27
  method = method.to_s[0..-2] if method =~ /\w[!?]$/
21
28
 
@@ -285,5 +292,48 @@ module Ruby2JS
285
292
  put '...'
286
293
  parse expr
287
294
  end
295
+
296
+ # do string concatenation when possible
297
+ def collapse_strings(node)
298
+ left = node.children[0]
299
+ return node unless left
300
+ right = node.children[2]
301
+
302
+ # recursively evaluate left hand side
303
+ if
304
+ left.type == :send and left.children.length == 3 and
305
+ left.children[1] == :+
306
+ then
307
+ left = collapse_strings(left)
308
+ end
309
+
310
+ # recursively evaluate right hand side
311
+ if
312
+ right.type == :send and right.children.length == 3 and
313
+ right.children[1] == :+
314
+ then
315
+ right = collapse_strings(right)
316
+ end
317
+
318
+ # if left and right are both strings, perform concatenation
319
+ if [:dstr, :str].include? left.type and [:dstr, :str].include? right.type
320
+ if left.type == :str and right.type == :str
321
+ return left.updated nil,
322
+ [left.children.first + right.children.first]
323
+ else
324
+ left = s(:dstr, left) if left.type == :str
325
+ right = s(:dstr, right) if right.type == :str
326
+ return left.updated(nil, left.children + right.children)
327
+ end
328
+ end
329
+
330
+ # if left and right are unchanged, return original node; otherwise
331
+ # return node modified to include new left and/or right hand sides.
332
+ if left == node.children[0] and right == node.children[2]
333
+ return node
334
+ else
335
+ return node.updated(nil, [left, :+, right])
336
+ end
337
+ end
288
338
  end
289
339
  end
@@ -289,6 +289,18 @@ module Ruby2JS
289
289
  # prevent chained delete methods from being converted to undef
290
290
  S(:send, target.updated(:sendw), *node.children[1..-1])
291
291
 
292
+ elsif es2017 and method==:entries and args.length==0 and node.is_method?
293
+ process node.updated(nil, [s(:const, nil, :Object), :entries, target])
294
+
295
+ elsif es2017 and method==:values and args.length==0 and node.is_method?
296
+ process node.updated(nil, [s(:const, nil, :Object), :values, target])
297
+
298
+ elsif es2017 and method==:rjust
299
+ process node.updated(nil, [target, :padStart, *args])
300
+
301
+ elsif es2017 and method==:ljust
302
+ process node.updated(nil, [target, :padEnd, *args])
303
+
292
304
  else
293
305
  super
294
306
  end
@@ -143,6 +143,11 @@ module Ruby2JS
143
143
  end
144
144
  end
145
145
 
146
+ # add a single token to the current line without checking for newline
147
+ def put!(string)
148
+ @line << Token.new(string.gsub("\r", "\n"), @ast)
149
+ end
150
+
146
151
  # add a single token to the current line and then advance to next line
147
152
  def puts(string)
148
153
  unless String === string and string.include? "\n"
@@ -2,7 +2,7 @@ module Ruby2JS
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 3
4
4
  MINOR = 0
5
- TINY = 8
5
+ TINY = 9
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
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: 3.0.8
4
+ version: 3.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Ruby
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-05-09 00:00:00.000000000 Z
11
+ date: 2018-05-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: parser