unparser 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/unparser/constants.rb +3 -1
- data/lib/unparser/emitter/assignment.rb +1 -3
- data/lib/unparser/emitter/binary.rb +1 -1
- data/lib/unparser/emitter/block.rb +1 -3
- data/lib/unparser/emitter/break.rb +4 -4
- data/lib/unparser/emitter/def.rb +1 -3
- data/lib/unparser/emitter/defined.rb +1 -3
- data/lib/unparser/emitter/if.rb +8 -3
- data/lib/unparser/emitter/literal/dynamic.rb +1 -3
- data/lib/unparser/emitter/literal/dynamic_body.rb +1 -1
- data/lib/unparser/emitter/literal/execute_string.rb +1 -3
- data/lib/unparser/emitter/literal/regexp.rb +0 -2
- data/lib/unparser/emitter/next.rb +5 -3
- data/lib/unparser/emitter/return.rb +1 -13
- data/lib/unparser/emitter.rb +32 -3
- data/spec/unit/unparser_spec.rb +68 -1
- data/unparser.gemspec +1 -1
- metadata +2 -2
data/lib/unparser/constants.rb
CHANGED
@@ -76,7 +76,9 @@ module Unparser
|
|
76
76
|
K_THEN = 'then'
|
77
77
|
|
78
78
|
TERMINATED = [
|
79
|
-
:int, :float, :self, :kwbegin, :const, :regexp
|
79
|
+
:int, :float, :self, :kwbegin, :const, :regexp, :args, :lvar,
|
80
|
+
:ivar, :gvar, :cvar, :if, :case, :module, :class, :sclass, :super,
|
81
|
+
:yield, :zsuper, :break, :next, :defined?, :str, :block
|
80
82
|
].to_set
|
81
83
|
|
82
84
|
KEYWORDS = constants.each_with_object([]) do |name, keywords|
|
@@ -16,10 +16,10 @@ module Unparser
|
|
16
16
|
# @api private
|
17
17
|
#
|
18
18
|
def dispatch
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
19
|
+
maybe_parentheses(parent_type == :or || parent_type == :and) do
|
20
|
+
write(K_BREAK)
|
21
|
+
return unless arguments
|
22
|
+
visit_parentheses(arguments)
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
data/lib/unparser/emitter/def.rb
CHANGED
data/lib/unparser/emitter/if.rb
CHANGED
@@ -34,7 +34,7 @@ module Unparser
|
|
34
34
|
# @api private
|
35
35
|
#
|
36
36
|
def unless?
|
37
|
-
!if_branch
|
37
|
+
!if_branch && else_branch
|
38
38
|
end
|
39
39
|
|
40
40
|
# Return keyword
|
@@ -64,8 +64,13 @@ module Unparser
|
|
64
64
|
# @api private
|
65
65
|
#
|
66
66
|
def emit_if_branch
|
67
|
-
|
68
|
-
|
67
|
+
if if_branch
|
68
|
+
visit_indented(if_branch)
|
69
|
+
end
|
70
|
+
|
71
|
+
if !if_branch && !else_branch
|
72
|
+
nl
|
73
|
+
end
|
69
74
|
end
|
70
75
|
|
71
76
|
# Emit else branch
|
@@ -14,9 +14,11 @@ module Unparser
|
|
14
14
|
# @api private
|
15
15
|
#
|
16
16
|
def dispatch
|
17
|
-
|
18
|
-
|
19
|
-
|
17
|
+
maybe_parentheses(parent_type == :or || parent_type == :and) do
|
18
|
+
write(K_NEXT)
|
19
|
+
return if children.empty?
|
20
|
+
visit_parentheses(children.first)
|
21
|
+
end
|
20
22
|
end
|
21
23
|
|
22
24
|
end # Next
|
@@ -17,20 +17,8 @@ module Unparser
|
|
17
17
|
#
|
18
18
|
def dispatch
|
19
19
|
write(K_RETURN)
|
20
|
-
emit_argument
|
21
|
-
end
|
22
|
-
|
23
|
-
# Emit argument
|
24
|
-
#
|
25
|
-
# @return [undefined]
|
26
|
-
#
|
27
|
-
# @api private
|
28
|
-
#
|
29
|
-
def emit_argument
|
30
20
|
return unless argument
|
31
|
-
|
32
|
-
visit(argument)
|
33
|
-
end
|
21
|
+
visit_parentheses(argument)
|
34
22
|
end
|
35
23
|
|
36
24
|
end # Return
|
data/lib/unparser/emitter.rb
CHANGED
@@ -202,13 +202,42 @@ module Unparser
|
|
202
202
|
#
|
203
203
|
def visit_terminated(node)
|
204
204
|
emitter = emitter(node)
|
205
|
-
|
206
|
-
|
207
|
-
return
|
205
|
+
maybe_parentheses(!emitter.terminated?) do
|
206
|
+
emitter.write_to_buffer
|
208
207
|
end
|
209
208
|
emitter.write_to_buffer
|
210
209
|
end
|
211
210
|
|
211
|
+
# Visit within parentheses
|
212
|
+
#
|
213
|
+
# @param [Parser::AST::Node] node
|
214
|
+
#
|
215
|
+
# @return [undefined]
|
216
|
+
#
|
217
|
+
# @api private
|
218
|
+
#
|
219
|
+
def visit_parentheses(node, *arguments)
|
220
|
+
parentheses(*arguments) do
|
221
|
+
visit(node)
|
222
|
+
end
|
223
|
+
end
|
224
|
+
|
225
|
+
# Call block in optional parentheses
|
226
|
+
#
|
227
|
+
# @param [true, false] flag
|
228
|
+
#
|
229
|
+
# @return [undefined]
|
230
|
+
#
|
231
|
+
# @api private
|
232
|
+
#
|
233
|
+
def maybe_parentheses(flag)
|
234
|
+
if flag
|
235
|
+
parentheses { yield }
|
236
|
+
else
|
237
|
+
yield
|
238
|
+
end
|
239
|
+
end
|
240
|
+
|
212
241
|
# Return emitter for node
|
213
242
|
#
|
214
243
|
# @param [Parser::AST::Node] node
|
data/spec/unit/unparser_spec.rb
CHANGED
@@ -143,6 +143,7 @@ describe Unparser do
|
|
143
143
|
assert_generates '1..2', %q(1..2)
|
144
144
|
assert_source '(0.0 / 0.0)..1'
|
145
145
|
assert_source '1..(0.0 / 0.0)'
|
146
|
+
assert_source '(0.0 / 0.0)..100'
|
146
147
|
end
|
147
148
|
|
148
149
|
context 'erange' do
|
@@ -317,9 +318,67 @@ describe Unparser do
|
|
317
318
|
end
|
318
319
|
RUBY
|
319
320
|
|
320
|
-
|
321
|
+
assert_source <<-RUBY
|
322
|
+
foo.bar do
|
323
|
+
end.baz
|
324
|
+
RUBY
|
325
|
+
|
321
326
|
assert_source '(1..2).max'
|
327
|
+
assert_source '1..2.max'
|
322
328
|
assert_source '(a = b).bar'
|
329
|
+
assert_source '@ivar.bar'
|
330
|
+
assert_source '//.bar'
|
331
|
+
assert_source '$var.bar'
|
332
|
+
assert_source '"".bar'
|
333
|
+
assert_source 'defined?(@foo).bar'
|
334
|
+
|
335
|
+
assert_source <<-RUBY
|
336
|
+
begin
|
337
|
+
rescue
|
338
|
+
end.bar
|
339
|
+
RUBY
|
340
|
+
|
341
|
+
assert_source <<-RUBY
|
342
|
+
case foo
|
343
|
+
when bar
|
344
|
+
end.baz
|
345
|
+
RUBY
|
346
|
+
|
347
|
+
assert_source <<-RUBY
|
348
|
+
class << self
|
349
|
+
end.bar
|
350
|
+
RUBY
|
351
|
+
|
352
|
+
assert_source 'break.foo'
|
353
|
+
assert_source 'next.foo'
|
354
|
+
assert_source 'super(a).foo'
|
355
|
+
assert_source 'super.foo'
|
356
|
+
assert_source 'yield(a).foo'
|
357
|
+
assert_source 'yield.foo'
|
358
|
+
assert_source 'array[i].foo'
|
359
|
+
assert_source '(array[i] = 1).foo'
|
360
|
+
assert_source 'array[1..2].foo'
|
361
|
+
assert_source '(a.attribute ||= foo).bar'
|
362
|
+
|
363
|
+
assert_source <<-RUBY
|
364
|
+
class Foo
|
365
|
+
end.bar
|
366
|
+
RUBY
|
367
|
+
|
368
|
+
assert_source <<-RUBY
|
369
|
+
module Foo
|
370
|
+
end.bar
|
371
|
+
RUBY
|
372
|
+
|
373
|
+
assert_source <<-RUBY
|
374
|
+
if foo
|
375
|
+
end.baz
|
376
|
+
RUBY
|
377
|
+
|
378
|
+
assert_source <<-RUBY
|
379
|
+
local = 1
|
380
|
+
local.bar
|
381
|
+
RUBY
|
323
382
|
|
324
383
|
assert_source 'foo.bar(*args)'
|
325
384
|
assert_source 'foo.bar(*arga, foo, *argb)'
|
@@ -604,6 +663,11 @@ describe Unparser do
|
|
604
663
|
9
|
605
664
|
end
|
606
665
|
RUBY
|
666
|
+
|
667
|
+
assert_source <<-RUBY
|
668
|
+
if foo
|
669
|
+
end
|
670
|
+
RUBY
|
607
671
|
end
|
608
672
|
|
609
673
|
context 'def' do
|
@@ -900,6 +964,8 @@ describe Unparser do
|
|
900
964
|
assert_source "a #{operator} b"
|
901
965
|
assert_source "(a #{operator} b).foo"
|
902
966
|
end
|
967
|
+
|
968
|
+
assert_source 'left / right'
|
903
969
|
end
|
904
970
|
|
905
971
|
context 'nested binary operators' do
|
@@ -917,6 +983,7 @@ describe Unparser do
|
|
917
983
|
|
918
984
|
{ :or => :'||', :and => :'&&' }.each do |word, symbol|
|
919
985
|
assert_generates "a #{word} break foo", "a #{symbol} (break(foo))"
|
986
|
+
assert_generates "a #{word} next foo", "a #{symbol} (next(foo))"
|
920
987
|
end
|
921
988
|
|
922
989
|
context 'expansion of shortcuts' do
|
data/unparser.gemspec
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: unparser
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-09-
|
12
|
+
date: 2013-09-06 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: parser
|