to_source 0.2.4 → 0.2.5

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.
@@ -1,3 +1,12 @@
1
+ # v0.2.5 2012-12-14
2
+
3
+ * [fixed] Emit unary operators correctly
4
+ * [fixed] Define with optional splat and block argument
5
+ * [fixed] Emit arguments to break keyword
6
+ * [change] Uglify output of binary operators with unneded paranteses. Correct output > nice output.
7
+ * [fixed] Emit nested binary operators correctly.
8
+ * [fixed] Emit element reference on self correctly. self[foo].
9
+
1
10
  # v0.2.4 2012-12-07
2
11
 
3
12
  * [feature] Allow to emit pattern variables as root node
data/Gemfile CHANGED
@@ -2,6 +2,6 @@ source :rubygems
2
2
 
3
3
  gemspec
4
4
 
5
- gem 'devtools', :git => 'https://github.com/mbj/devtools.git', :branch => 'rspec-2-mutant'
5
+ gem 'devtools', :git => 'https://github.com/datamapper/devtools.git'
6
6
 
7
7
  eval File.read('Gemfile.devtools')
@@ -1,5 +1,5 @@
1
- group :development do
2
- gem 'rake', '~> 0.9.2'
1
+ group :development do
2
+ gem 'rake', '~> 10.0'
3
3
  gem 'rspec', '~> 2.12.0'
4
4
  gem 'yard', '~> 0.8.3'
5
5
  end
@@ -26,26 +26,19 @@ group :metrics do
26
26
  gem 'flog', '~> 2.5.1'
27
27
  gem 'reek', '~> 1.2.8', :git => 'https://github.com/dkubb/reek.git'
28
28
  gem 'roodi', '~> 2.1.0'
29
- gem 'yardstick', '~> 0.7.0'
30
- gem 'simplecov'
29
+ gem 'yardstick', '~> 0.8.0'
30
+ gem 'mutant', '~> 0.2.5'
31
31
 
32
32
  platforms :ruby_18, :ruby_19 do
33
33
  # this indirectly depends on ffi which does not build on ruby-head
34
34
  gem 'yard-spellcheck', '~> 0.1.5'
35
35
  end
36
36
 
37
- platforms :mri_18 do
38
- gem 'arrayfields', '~> 4.7.4' # for metric_fu
39
- gem 'fattr', '~> 2.2.0' # for metric_fu
40
- gem 'json', '~> 1.7.3' # for metric_fu rake task
41
- gem 'map', '~> 6.0.1' # for metric_fu
42
- gem 'metric_fu', '~> 2.1.1'
43
- gem 'mspec', '~> 1.5.17'
44
- gem 'rcov', '~> 1.0.0'
37
+ platforms :mri_19 do
38
+ gem 'simplecov', '~> 0.7'
45
39
  end
46
40
 
47
41
  platforms :rbx do
48
42
  gem 'pelusa', '~> 0.2.1'
49
- gem 'anima', '~> 0.0.1', :git => 'https://github.com/mbj/anima.git'
50
43
  end
51
44
  end
@@ -148,6 +148,10 @@ module ToSource
148
148
  #
149
149
  def break(node)
150
150
  emit('break')
151
+ if node.value.class != Rubinius::AST::NilLiteral
152
+ emit(' ')
153
+ dispatch(node.value)
154
+ end
151
155
  end
152
156
 
153
157
  # Emit next
@@ -1047,6 +1051,39 @@ module ToSource
1047
1051
  end
1048
1052
  end
1049
1053
 
1054
+ UNARY_OPERATORS = %w(
1055
+ ! ~ -@ +@
1056
+ ).map(&:to_sym).to_set.freeze
1057
+
1058
+ UNARY_MAPPING = {
1059
+ :-@ => :-,
1060
+ :+@ => :+,
1061
+ }.freeze
1062
+
1063
+ # Emit unary operator
1064
+ #
1065
+ # @param [Rubinius::AST::Node] node
1066
+ #
1067
+ # @return [true]
1068
+ # if node was emitted
1069
+ #
1070
+ # @return [false]
1071
+ # otherwise
1072
+ #
1073
+ # @api private
1074
+ #
1075
+ def unary_operator(node)
1076
+ name = node.name
1077
+
1078
+ if UNARY_OPERATORS.include?(name)
1079
+ emit(UNARY_MAPPING.fetch(name, name))
1080
+ dispatch(node.receiver)
1081
+ return true
1082
+ end
1083
+
1084
+ false
1085
+ end
1086
+
1050
1087
  # Emit send node
1051
1088
  #
1052
1089
  # @param [Rubinius::AST::Node] node
@@ -1056,11 +1093,7 @@ module ToSource
1056
1093
  # @api private
1057
1094
  #
1058
1095
  def send(node)
1059
- if node.name == :'!'
1060
- emit('!')
1061
- dispatch(node.receiver)
1062
- return
1063
- end
1096
+ return if unary_operator(node)
1064
1097
 
1065
1098
  if receiver(node)
1066
1099
  emit('.')
@@ -1141,10 +1174,7 @@ module ToSource
1141
1174
  # @api private
1142
1175
  #
1143
1176
  def element_reference(node)
1144
- unless node.receiver.is_a?(Rubinius::AST::Self)
1145
- dispatch(node.receiver)
1146
- end
1147
-
1177
+ dispatch(node.receiver)
1148
1178
  arguments(node,'[',']')
1149
1179
  end
1150
1180
 
@@ -1378,9 +1408,11 @@ module ToSource
1378
1408
  # @api private
1379
1409
  #
1380
1410
  def and(node)
1411
+ emit('(')
1381
1412
  dispatch(node.left)
1382
1413
  emit(' && ')
1383
1414
  dispatch(node.right)
1415
+ emit(')')
1384
1416
  end
1385
1417
 
1386
1418
  # Emit or
@@ -1392,9 +1424,11 @@ module ToSource
1392
1424
  # @api private
1393
1425
  #
1394
1426
  def or(node)
1427
+ emit('(')
1395
1428
  dispatch(node.left)
1396
1429
  emit(' || ')
1397
1430
  dispatch(node.right)
1431
+ emit(')')
1398
1432
  end
1399
1433
 
1400
1434
  # Emit and operation with assignment
@@ -1611,6 +1645,7 @@ module ToSource
1611
1645
  if defaults
1612
1646
  emit(', ') unless empty
1613
1647
  dispatch(node.defaults)
1648
+ empty = false
1614
1649
  end
1615
1650
 
1616
1651
  if splat
@@ -1807,12 +1842,13 @@ module ToSource
1807
1842
 
1808
1843
  operand = node.arguments.array[0]
1809
1844
 
1845
+ emit('(')
1810
1846
  dispatch(node.receiver)
1811
1847
 
1812
1848
  emit(" #{name.to_s} ")
1813
1849
  dispatch(operand)
1814
1850
 
1815
- self
1851
+ emit(')')
1816
1852
  end
1817
1853
  end
1818
1854
  end
@@ -361,6 +361,10 @@ describe ToSource::Visitor,'.run' do
361
361
  assert_source 'foo[index]'
362
362
  end
363
363
 
364
+ context 'as element reference on self' do
365
+ assert_source 'self[foo]'
366
+ end
367
+
364
368
  context 'without arguments' do
365
369
  assert_source 'foo.bar'
366
370
  end
@@ -501,6 +505,10 @@ describe ToSource::Visitor,'.run' do
501
505
  assert_source 'break'
502
506
  end
503
507
 
508
+ context 'break with arguments' do
509
+ assert_source 'break a'
510
+ end
511
+
504
512
  context 'next' do
505
513
  assert_source 'next'
506
514
  end
@@ -534,43 +542,43 @@ describe ToSource::Visitor,'.run' do
534
542
  context 'binary operators' do
535
543
  %w(+ - * / & | && || << >> == === != <= < <=> > >= =~ !~ ^ **).each do |operator|
536
544
  context "on literals #{operator}" do
537
- assert_source "1 #{operator} 2"
545
+ assert_source "(1 #{operator} 2)"
538
546
  end
539
547
 
540
548
  context "on self #{operator}" do
541
- assert_source "self #{operator} b"
549
+ assert_source "(self #{operator} b)"
542
550
  end
543
551
 
544
552
  context "on calls #{operator}" do
545
- assert_source "a #{operator} b"
553
+ assert_source "(a #{operator} b)"
546
554
  end
547
555
 
548
556
  end
549
557
 
550
- pending 'nested binary operators' do
551
- assert_source "(a or b) || c"
558
+ context 'nested binary operators' do
559
+ assert_source '(a || (b || c))'
552
560
  end
553
561
  end
554
562
 
555
563
  context 'expansion of shortcuts' do
556
564
  context 'on += operator' do
557
- assert_converts 'a = a + 2', 'a += 2'
565
+ assert_converts 'a = (a + 2)', 'a += 2'
558
566
  end
559
567
 
560
568
  context 'on -= operator' do
561
- assert_converts 'a = a - 2', 'a -= 2'
569
+ assert_converts 'a = (a - 2)', 'a -= 2'
562
570
  end
563
571
 
564
572
  context 'on **= operator' do
565
- assert_converts 'a = a ** 2', 'a **= 2'
573
+ assert_converts 'a = (a ** 2)', 'a **= 2'
566
574
  end
567
575
 
568
576
  context 'on *= operator' do
569
- assert_converts 'a = a * 2', 'a *= 2'
577
+ assert_converts 'a = (a * 2)', 'a *= 2'
570
578
  end
571
579
 
572
580
  context 'on /= operator' do
573
- assert_converts 'a = a / 2', 'a /= 2'
581
+ assert_converts 'a = (a / 2)', 'a /= 2'
574
582
  end
575
583
 
576
584
  context 'on &&= operator' do
@@ -591,19 +599,15 @@ describe ToSource::Visitor,'.run' do
591
599
  assert_source '!!1'
592
600
  end
593
601
 
594
- pending 'unary match' do
595
- assert_source '~a'
596
- end
597
-
598
- pending 'unary match' do
602
+ context 'unary match' do
599
603
  assert_source '~a'
600
604
  end
601
605
 
602
- pending 'unary minus' do
606
+ context 'unary minus' do
603
607
  assert_source '-a'
604
608
  end
605
609
 
606
- pending 'unary plus' do
610
+ context 'unary plus' do
607
611
  assert_source '+a'
608
612
  end
609
613
  end
@@ -994,6 +998,22 @@ describe ToSource::Visitor,'.run' do
994
998
  RUBY
995
999
  end
996
1000
 
1001
+ context 'with optional and splat argument' do
1002
+ assert_source <<-RUBY
1003
+ def foo(baz = true, *bor)
1004
+ bar
1005
+ end
1006
+ RUBY
1007
+ end
1008
+
1009
+ context 'with optional and splat and block argument' do
1010
+ assert_source <<-RUBY
1011
+ def foo(baz = true, *bor, &block)
1012
+ bar
1013
+ end
1014
+ RUBY
1015
+ end
1016
+
997
1017
  context 'with required optional and splat argument' do
998
1018
  assert_source <<-RUBY
999
1019
  def foo(bar, baz = true, *bor)
@@ -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'
4
+ s.version = '0.2.5'
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
4
+ version: 0.2.5
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: 2012-12-10 00:00:00.000000000 Z
13
+ date: 2012-12-14 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: adamantium