to_source 0.2.7 → 0.2.8
Sign up to get free protection for your applications and to get access to all the features.
- data/Changelog.md +9 -0
- data/TODO +2 -0
- data/lib/to_source/visitor.rb +54 -23
- data/spec/unit/to_source/visitor/class_methods/run_spec.rb +24 -12
- data/to_source.gemspec +1 -1
- metadata +2 -2
data/Changelog.md
CHANGED
@@ -1,3 +1,12 @@
|
|
1
|
+
# v0.2.8 2013-01-3
|
2
|
+
|
3
|
+
* [Changed] Emit many times more ugly code, but correctnes > beautifulnes
|
4
|
+
* [fixed] Emit break with parantheses
|
5
|
+
* [fixed] Emit op assign and as "&&="
|
6
|
+
* [fixed] Emit op assign or as "||="
|
7
|
+
|
8
|
+
[Compare v0.2.7..v0.2.8](https://github.com/mbj/to_source/compare/v0.2.7...v0.2.8)
|
9
|
+
|
1
10
|
# v0.2.7 2013-01-2
|
2
11
|
|
3
12
|
* [fixed] Emit super with blocks correctly
|
data/TODO
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
* Do not emit parantheses around non keywords in binary operations (this will beautify source again)
|
2
|
+
* Rewrite this library it is ultra hacky and adhoc
|
1
3
|
* Fix parsing errors from ruby parser
|
2
4
|
It breaks on def $keyword.
|
3
5
|
* Do a metric driven refactor once flay flog and friends work.
|
data/lib/to_source/visitor.rb
CHANGED
@@ -149,8 +149,9 @@ module ToSource
|
|
149
149
|
def break(node)
|
150
150
|
emit('break')
|
151
151
|
if node.value.class != Rubinius::AST::NilLiteral
|
152
|
-
emit('
|
152
|
+
emit('(')
|
153
153
|
dispatch(node.value)
|
154
|
+
emit(')')
|
154
155
|
end
|
155
156
|
end
|
156
157
|
|
@@ -1415,13 +1416,54 @@ module ToSource
|
|
1415
1416
|
# @api private
|
1416
1417
|
#
|
1417
1418
|
def and(node)
|
1419
|
+
binary(node, '&&')
|
1420
|
+
end
|
1421
|
+
|
1422
|
+
# Call block while emitting parantheses
|
1423
|
+
#
|
1424
|
+
# @return [undefined]
|
1425
|
+
#
|
1426
|
+
# @api private
|
1427
|
+
#
|
1428
|
+
def parantheses
|
1418
1429
|
emit('(')
|
1419
|
-
|
1420
|
-
emit(' && ')
|
1421
|
-
dispatch(node.right)
|
1430
|
+
yield
|
1422
1431
|
emit(')')
|
1423
1432
|
end
|
1424
1433
|
|
1434
|
+
# Emit binary operation
|
1435
|
+
#
|
1436
|
+
# @param [Rubnius::AST::Node] node
|
1437
|
+
#
|
1438
|
+
# @param [Symbol] symbol
|
1439
|
+
# the operation symbol
|
1440
|
+
#
|
1441
|
+
# @api private
|
1442
|
+
#
|
1443
|
+
def binary(node, symbol)
|
1444
|
+
parantheses do
|
1445
|
+
parantheses { dispatch(node.left) }
|
1446
|
+
emit(" #{symbol} ")
|
1447
|
+
parantheses { dispatch(node.right) }
|
1448
|
+
end
|
1449
|
+
end
|
1450
|
+
|
1451
|
+
# Emit binary shortcut
|
1452
|
+
#
|
1453
|
+
# @param [Rubinius::AST::Node] node
|
1454
|
+
#
|
1455
|
+
# @param [Symbol] symbol
|
1456
|
+
#
|
1457
|
+
# @api private
|
1458
|
+
#
|
1459
|
+
def binary_shortcut(node, symbol)
|
1460
|
+
parantheses do
|
1461
|
+
dispatch(node.left)
|
1462
|
+
emit(" #{symbol} ")
|
1463
|
+
parantheses { dispatch(node.right.value) }
|
1464
|
+
end
|
1465
|
+
end
|
1466
|
+
|
1425
1467
|
# Emit or
|
1426
1468
|
#
|
1427
1469
|
# @param [Rubinius::AST::Node] node
|
@@ -1431,11 +1473,7 @@ module ToSource
|
|
1431
1473
|
# @api private
|
1432
1474
|
#
|
1433
1475
|
def or(node)
|
1434
|
-
|
1435
|
-
dispatch(node.left)
|
1436
|
-
emit(' || ')
|
1437
|
-
dispatch(node.right)
|
1438
|
-
emit(')')
|
1476
|
+
binary(node, '||')
|
1439
1477
|
end
|
1440
1478
|
|
1441
1479
|
# Emit and operation with assignment
|
@@ -1447,9 +1485,7 @@ module ToSource
|
|
1447
1485
|
# @api private
|
1448
1486
|
#
|
1449
1487
|
def op_assign_and(node)
|
1450
|
-
|
1451
|
-
emit(' && ')
|
1452
|
-
dispatch(node.right)
|
1488
|
+
binary_shortcut(node, :'&&=')
|
1453
1489
|
end
|
1454
1490
|
|
1455
1491
|
# Emit or operation with assignment
|
@@ -1461,9 +1497,7 @@ module ToSource
|
|
1461
1497
|
# @api private
|
1462
1498
|
#
|
1463
1499
|
def op_assign_or(node)
|
1464
|
-
|
1465
|
-
emit(' || ')
|
1466
|
-
dispatch(node.right)
|
1500
|
+
binary_shortcut(node, :'||=')
|
1467
1501
|
end
|
1468
1502
|
alias_method :op_assign_or19, :op_assign_or
|
1469
1503
|
|
@@ -1845,17 +1879,14 @@ module ToSource
|
|
1845
1879
|
def process_binary_operator(node)
|
1846
1880
|
name = node.name
|
1847
1881
|
return unless OPERATORS.include?(name)
|
1848
|
-
return if node.arguments.array.length != 1
|
1849
1882
|
|
1850
1883
|
operand = node.arguments.array[0]
|
1851
1884
|
|
1852
|
-
|
1853
|
-
|
1854
|
-
|
1855
|
-
|
1856
|
-
|
1857
|
-
|
1858
|
-
emit(')')
|
1885
|
+
parantheses do
|
1886
|
+
parantheses { dispatch(node.receiver) }
|
1887
|
+
emit(" #{name.to_s} ")
|
1888
|
+
parantheses { dispatch(operand) }
|
1889
|
+
end
|
1859
1890
|
end
|
1860
1891
|
end
|
1861
1892
|
end
|
@@ -17,6 +17,13 @@ describe ToSource::Visitor,'.run' do
|
|
17
17
|
it 'should create original source' do
|
18
18
|
should eql(compress(expected_source))
|
19
19
|
end
|
20
|
+
|
21
|
+
it 'should be able to round trip generated source also' do
|
22
|
+
generated = subject
|
23
|
+
ast = subject.to_ast
|
24
|
+
second = described_class.run(ast)
|
25
|
+
generated.should eql(second)
|
26
|
+
end
|
20
27
|
end
|
21
28
|
|
22
29
|
def self.assert_source(source)
|
@@ -542,7 +549,7 @@ describe ToSource::Visitor,'.run' do
|
|
542
549
|
end
|
543
550
|
|
544
551
|
context 'break with arguments' do
|
545
|
-
assert_source 'break
|
552
|
+
assert_source 'break(a)'
|
546
553
|
end
|
547
554
|
|
548
555
|
context 'next' do
|
@@ -578,51 +585,56 @@ describe ToSource::Visitor,'.run' do
|
|
578
585
|
context 'binary operators' do
|
579
586
|
%w(+ - * / & | && || << >> == === != <= < <=> > >= =~ !~ ^ **).each do |operator|
|
580
587
|
context "on literals #{operator}" do
|
581
|
-
assert_source "(1 #{operator} 2)"
|
588
|
+
assert_source "((1) #{operator} (2))"
|
582
589
|
end
|
583
590
|
|
584
591
|
context "on self #{operator}" do
|
585
|
-
assert_source "(self #{operator} b)"
|
592
|
+
assert_source "((self) #{operator} (b))"
|
586
593
|
end
|
587
594
|
|
588
595
|
context "on calls #{operator}" do
|
589
|
-
assert_source "(a #{operator} b)"
|
596
|
+
assert_source "((a) #{operator} (b))"
|
590
597
|
end
|
598
|
+
end
|
591
599
|
|
600
|
+
context 'binary operator and keywords' do
|
601
|
+
assert_source '((a) || (break(foo)))'
|
592
602
|
end
|
593
603
|
|
594
604
|
context 'nested binary operators' do
|
595
|
-
assert_source '(a || (b || c))'
|
605
|
+
assert_source '((a) || (((b) || (c))))'
|
596
606
|
end
|
597
607
|
end
|
598
608
|
|
599
609
|
context 'expansion of shortcuts' do
|
600
610
|
context 'on += operator' do
|
601
|
-
assert_converts 'a = (a + 2)', 'a += 2'
|
611
|
+
assert_converts 'a = ((a) + (2))', 'a += 2'
|
602
612
|
end
|
603
613
|
|
604
614
|
context 'on -= operator' do
|
605
|
-
assert_converts 'a = (a - 2)', 'a -= 2'
|
615
|
+
assert_converts 'a = ((a) - (2))', 'a -= 2'
|
606
616
|
end
|
607
617
|
|
608
618
|
context 'on **= operator' do
|
609
|
-
assert_converts 'a = (a ** 2)', 'a **= 2'
|
619
|
+
assert_converts 'a = ((a) ** (2))', 'a **= 2'
|
610
620
|
end
|
611
621
|
|
612
622
|
context 'on *= operator' do
|
613
|
-
assert_converts 'a = (a * 2)', 'a *= 2'
|
623
|
+
assert_converts 'a = ((a) * (2))', 'a *= 2'
|
614
624
|
end
|
615
625
|
|
616
626
|
context 'on /= operator' do
|
617
|
-
assert_converts 'a = (a / 2)', 'a /= 2'
|
627
|
+
assert_converts 'a = ((a) / (2))', 'a /= 2'
|
618
628
|
end
|
629
|
+
end
|
619
630
|
|
631
|
+
context 'shortcuts' do
|
620
632
|
context 'on &&= operator' do
|
621
|
-
|
633
|
+
assert_source '(a &&= (b))'
|
622
634
|
end
|
623
635
|
|
624
636
|
context 'on ||= operator' do
|
625
|
-
|
637
|
+
assert_source '(a ||= (2))'
|
626
638
|
end
|
627
639
|
end
|
628
640
|
|
data/to_source.gemspec
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
2
|
Gem::Specification.new do |s|
|
3
3
|
s.name = 'to_source'
|
4
|
-
s.version = '0.2.
|
4
|
+
s.version = '0.2.8'
|
5
5
|
s.authors = ['Josep M. Bach', 'Markus Schirp']
|
6
6
|
s.email = ['josep.m.bach@gmail.com', 'mbj@seonic.net']
|
7
7
|
s.homepage = 'http://github.com/txus/to_source'
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: to_source
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.8
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2013-01-
|
13
|
+
date: 2013-01-03 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: adamantium
|