to_source 0.2.0 → 0.2.1

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.
data/Changelog.md CHANGED
@@ -1,3 +1,8 @@
1
+ # v0.2.1 2012-12-07
2
+
3
+ * [fixed] Emit of def on splat with block
4
+ * [fixed] Emit of pattern args
5
+
1
6
  # v0.2.0 2012-12-07
2
7
 
3
8
  * [BRAKING CHANGE] Remove core extension Rubinius::AST::Node#to_source (mbj)
data/Gemfile CHANGED
@@ -2,7 +2,6 @@ source :rubygems
2
2
 
3
3
  gemspec
4
4
 
5
- gem 'mutant-melbourne', :git => 'https://github.com/mbj/melbourne.git'
6
5
  gem 'devtools', :git => 'https://github.com/mbj/devtools.git', :branch => 'rspec-2-mutant'
7
6
 
8
7
  eval File.read('Gemfile.devtools')
data/Gemfile.devtools CHANGED
@@ -47,6 +47,5 @@ group :metrics do
47
47
  platforms :rbx do
48
48
  gem 'pelusa', '~> 0.2.1'
49
49
  gem 'anima', '~> 0.0.1', :git => 'https://github.com/mbj/anima.git'
50
- gem 'mutant', '~> 0.0.1', :git => 'https://github.com/mbj/mutant.git'
51
50
  end
52
51
  end
data/README.md CHANGED
@@ -1,9 +1,9 @@
1
1
  to_source
2
2
  =========
3
3
 
4
- [![Build Status](https://secure.travis-ci.org/mbj/to_source.png?branch=master)](http://travis-ci.org/mbj/to_source)
5
- [![Dependency Status](https://gemnasium.com/mbj/to_source.png)](https://gemnasium.com/mbj/to_source)
6
- [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/mbj/to_source)
4
+ [![Build Status](https://secure.travis-ci.org/txus/to_source.png?branch=master)](http://travis-ci.org/txus/to_source)
5
+ [![Dependency Status](https://gemnasium.com/txus/to_source.png)](https://gemnasium.com/txus/to_source)
6
+ [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/txus/to_source)
7
7
 
8
8
  Reverse parser to generate source code from the Rubinius AST. Also works well under MRI.
9
9
 
data/TODO CHANGED
@@ -3,3 +3,5 @@
3
3
  * Do a metric driven refactor once flay flog and friends work.
4
4
  * Report bug about missing Rubinius::AST::SClass#body
5
5
  * Decide about to support 18-mode or not
6
+ * Handle Rubinius::AST::PatternArguments
7
+ * Fix emit of nested boolean uperators and parantheses!
@@ -1,3 +1,3 @@
1
1
  module ToSource
2
- VERSION = '0.2.0'
2
+ VERSION = '0.2.1'
3
3
  end
@@ -16,6 +16,28 @@ module ToSource
16
16
  new(node).output
17
17
  end
18
18
 
19
+ # Return handler registry
20
+ #
21
+ # @return [Hash]
22
+ #
23
+ # @api private
24
+ #
25
+ def self.registry
26
+ @registry ||= {}
27
+ end
28
+
29
+ # Register handler class
30
+ #
31
+ # @param [Class:Rubinius::AST::Node] node_klass
32
+ #
33
+ # @return [undefined]
34
+ #
35
+ # @api private
36
+ #
37
+ def self.register(node_klass)
38
+ registry[node_klass]=self
39
+ end
40
+
19
41
  # Return the source code of AST
20
42
  #
21
43
  # @return [String]
@@ -51,9 +73,14 @@ module ToSource
51
73
  # @api private
52
74
  #
53
75
  def dispatch(node)
54
- name = node.node_name
55
- name = "#{name}_def" if %w[ class module ].include?(name)
56
- __send__(name, node)
76
+ handler = self.class.registry.fetch(node.class) do
77
+ name = node.node_name
78
+ name = "#{name}_def" if %w(class module).include?(name)
79
+ __send__(name, node)
80
+ nil
81
+ end
82
+
83
+ handler.run(self, node) if handler
57
84
  end
58
85
 
59
86
  # Emit file
@@ -1522,6 +1549,24 @@ module ToSource
1522
1549
  kend
1523
1550
  end
1524
1551
 
1552
+ # Emit pattern arguments
1553
+ #
1554
+ # @param [Rubinius::AST::PatternArguments] node
1555
+ #
1556
+ # @return [undefined]
1557
+ #
1558
+ # @api private
1559
+ #
1560
+ def pattern_arguments(node)
1561
+ emit('(')
1562
+ arguments = node.arguments.body
1563
+ arguments.each_with_index do |argument, index|
1564
+ emit(argument.name)
1565
+ emit(', ') unless index == arguments.size - 1
1566
+ end
1567
+ emit(')')
1568
+ end
1569
+
1525
1570
  # Emit formal arguments as shared between ruby18 and ruby19 mode
1526
1571
  #
1527
1572
  # @param [Rubinius::AST::Node] node
@@ -1535,7 +1580,15 @@ module ToSource
1535
1580
  required, defaults, splat = node.required, node.defaults, node.splat
1536
1581
 
1537
1582
  emit(open)
1538
- emit(required.join(', '))
1583
+
1584
+ required.each_with_index do |node, index|
1585
+ if node.kind_of?(Rubinius::AST::Node)
1586
+ dispatch(node)
1587
+ else
1588
+ emit(node)
1589
+ end
1590
+ emit(', ') unless index == required.size-1
1591
+ end
1539
1592
 
1540
1593
  empty = required.empty?
1541
1594
 
@@ -1547,6 +1600,7 @@ module ToSource
1547
1600
  if splat
1548
1601
  emit(', ') unless empty
1549
1602
  emit('*')
1603
+ empty = false
1550
1604
  unless splat == :@unnamed_splat
1551
1605
  emit(splat)
1552
1606
  end
@@ -20,7 +20,7 @@ describe ToSource::Visitor,'.run' do
20
20
  end
21
21
 
22
22
  def self.assert_source(source)
23
- let(:node) { source.to_ast }
23
+ let(:node) { puts source; source.to_ast }
24
24
  let(:source) { source }
25
25
  let(:expected_source) { source }
26
26
 
@@ -398,6 +398,14 @@ describe ToSource::Visitor,'.run' do
398
398
  end
399
399
  end
400
400
 
401
+ context 'with block that takes pattern arguments' do
402
+ assert_source <<-RUBY
403
+ foo.bar do |(a, b), c|
404
+ d
405
+ end
406
+ RUBY
407
+ end
408
+
401
409
  context 'with block that takes arguments' do
402
410
  assert_source <<-RUBY
403
411
  foo.bar do |a|
@@ -427,6 +435,13 @@ describe ToSource::Visitor,'.run' do
427
435
  RUBY
428
436
  end
429
437
 
438
+ context 'with splat and block argument' do
439
+ assert_source <<-RUBY
440
+ def foo.bar(*args, &block)
441
+ some_stuff
442
+ end
443
+ RUBY
444
+ end
430
445
 
431
446
  context 'with passing block argument' do
432
447
  assert_source 'foo.bar(&baz)'
@@ -524,6 +539,11 @@ describe ToSource::Visitor,'.run' do
524
539
  context "on calls #{operator}" do
525
540
  assert_source "a #{operator} b"
526
541
  end
542
+
543
+ end
544
+
545
+ pending 'nested binary operators' do
546
+ assert_source "(a or b) || c"
527
547
  end
528
548
  end
529
549
 
@@ -565,6 +585,22 @@ describe ToSource::Visitor,'.run' do
565
585
  context 'double negation' do
566
586
  assert_source '!!1'
567
587
  end
588
+
589
+ pending 'unary match' do
590
+ assert_source '~a'
591
+ end
592
+
593
+ pending 'unary match' do
594
+ assert_source '~a'
595
+ end
596
+
597
+ pending 'unary minus' do
598
+ assert_source '-a'
599
+ end
600
+
601
+ pending 'unary plus' do
602
+ assert_source '+a'
603
+ end
568
604
  end
569
605
 
570
606
  context 'if statement' do
data/to_source.gemspec CHANGED
@@ -18,5 +18,5 @@ Gem::Specification.new do |s|
18
18
  s.require_paths = ['lib']
19
19
 
20
20
  s.add_dependency('adamantium', '~> 0.0.3')
21
- s.add_dependency('mutant-melbourne', '~> 2.0.1')
21
+ s.add_dependency('mutant-melbourne', '~> 2.0.3')
22
22
  end
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.0
4
+ version: 0.2.1
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-07 00:00:00.000000000 Z
13
+ date: 2012-12-08 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: adamantium
@@ -35,7 +35,7 @@ dependencies:
35
35
  requirements:
36
36
  - - ~>
37
37
  - !ruby/object:Gem::Version
38
- version: 2.0.1
38
+ version: 2.0.3
39
39
  type: :runtime
40
40
  prerelease: false
41
41
  version_requirements: !ruby/object:Gem::Requirement
@@ -43,7 +43,7 @@ dependencies:
43
43
  requirements:
44
44
  - - ~>
45
45
  - !ruby/object:Gem::Version
46
- version: 2.0.1
46
+ version: 2.0.3
47
47
  description: Transform your Rubinius AST nodes back to source code. Reverse parsing!
48
48
  email:
49
49
  - josep.m.bach@gmail.com