to_source 0.2.12 → 0.2.13

Sign up to get free protection for your applications and to get access to all the features.
data/Changelog.md CHANGED
@@ -1,4 +1,10 @@
1
- # v0.2.11 2013-01-9
1
+ # v0.2.13 2013-01-9
2
+
3
+ * [fixed] Emit send with arguments and body correctly
4
+
5
+ [Compare v0.2.12..v0.2.13](https://github.com/mbj/to_source/compare/v0.2.12...v0.2.13)
6
+
7
+ # v0.2.12 2013-01-9
2
8
 
3
9
  * [fixed] Emit edge cases with dynamic literals correctly
4
10
 
data/config/flay.yml CHANGED
@@ -1,3 +1,3 @@
1
1
  ---
2
- threshold: 18
3
- total_score: 296
2
+ threshold: 40
3
+ total_score: 619
data/config/flog.yml CHANGED
@@ -1,2 +1,2 @@
1
1
  ---
2
- threshold: 14.4
2
+ threshold: 22.2
data/config/site.reek CHANGED
@@ -8,9 +8,8 @@ UncommunicativeParameterName:
8
8
  - !ruby/regexp /[0-9]$/
9
9
  - !ruby/regexp /[A-Z]/
10
10
  LargeClass:
11
- max_methods: 10
12
- exclude:
13
- - "Mutant::Matcher::Method" # 13 methods
11
+ max_methods: 11
12
+ exclude: []
14
13
  enabled: true
15
14
  max_instance_variables: 3
16
15
  UncommunicativeMethodName:
@@ -23,8 +22,7 @@ UncommunicativeMethodName:
23
22
  - !ruby/regexp /[A-Z]/
24
23
  LongParameterList:
25
24
  max_params: 2
26
- exclude:
27
- - "Mutant::Context::Constant#initialize" # 3 params
25
+ exclude: []
28
26
  enabled: true
29
27
  overrides: {}
30
28
  FeatureEnvy:
@@ -38,9 +36,12 @@ BooleanParameter:
38
36
  enabled: true
39
37
  IrresponsibleModule:
40
38
  exclude: []
41
- enabled: true
39
+ enabled: false
42
40
  UncommunicativeModuleName:
43
- accept: []
41
+ accept:
42
+ - ToSource::Emitter::Match3
43
+ - ToSource::Emitter::OpAssign1
44
+ - ToSource::Emitter::OpAssign2
44
45
  exclude: []
45
46
  enabled: true
46
47
  reject:
@@ -52,7 +53,7 @@ NestedIterators:
52
53
  enabled: true
53
54
  max_allowed_nesting: 1
54
55
  LongMethod:
55
- max_statements: 6
56
+ max_statements: 7
56
57
  exclude: []
57
58
  enabled: true
58
59
  Duplication:
data/config/yardstick.yml CHANGED
@@ -1,2 +1,2 @@
1
1
  ---
2
- threshold: 100
2
+ threshold: 63.4
data/lib/to_source.rb CHANGED
@@ -30,9 +30,9 @@ require 'to_source/emitter/scoped_name'
30
30
  require 'to_source/emitter/send'
31
31
  require 'to_source/emitter/send_with_arguments'
32
32
  require 'to_source/emitter/block_pass'
33
+ require 'to_source/emitter/block_argument'
33
34
  require 'to_source/emitter/iter'
34
35
  require 'to_source/emitter/pattern_arguments'
35
- require 'to_source/emitter/block_argument'
36
36
  require 'to_source/emitter/unary_operator_method'
37
37
  require 'to_source/emitter/binary_operator_method'
38
38
  require 'to_source/emitter/binary_operator'
@@ -43,13 +43,12 @@ require 'to_source/emitter/defined'
43
43
  require 'to_source/emitter/attribute_assignment'
44
44
  require 'to_source/emitter/element_assignment'
45
45
  require 'to_source/emitter/if'
46
- require 'to_source/emitter/while'
46
+ require 'to_source/emitter/loop'
47
47
  require 'to_source/emitter/ensure'
48
48
  require 'to_source/emitter/receiver_case'
49
49
  require 'to_source/emitter/when'
50
50
  require 'to_source/emitter/splat_when'
51
51
  require 'to_source/emitter/unless'
52
- require 'to_source/emitter/until'
53
52
  require 'to_source/emitter/class'
54
53
  require 'to_source/emitter/module'
55
54
  require 'to_source/emitter/op_assign2'
@@ -65,6 +65,19 @@ module ToSource
65
65
  emitter.new(node, buffer)
66
66
  end
67
67
 
68
+ def self.delegate(*names)
69
+ names.each do |name|
70
+ define_delegator(name)
71
+ end
72
+ end
73
+
74
+ def self.define_delegator(name)
75
+ define_method(name) do
76
+ node.public_send(name)
77
+ end
78
+ private name
79
+ end
80
+
68
81
  attr_reader :buffer
69
82
 
70
83
  def initialize(node, buffer)
@@ -29,9 +29,10 @@ module ToSource
29
29
  private
30
30
 
31
31
  def dispatch
32
- visit(node.start)
32
+ util = node
33
+ visit(util.start)
33
34
  emit(token)
34
- visit(node.finish)
35
+ visit(util.finish)
35
36
  end
36
37
 
37
38
  def token
@@ -54,26 +55,26 @@ module ToSource
54
55
 
55
56
  handle(Rubinius::AST::HashLiteral)
56
57
 
57
- def dispatch
58
- body = node.array.each_slice(2).to_a
59
-
60
- max = body.length - 1
58
+ private
61
59
 
60
+ def dispatch
62
61
  emit '{'
62
+ emit_body
63
+ emit '}'
64
+ end
63
65
 
64
- body.each_with_index do |slice, index|
65
- key, value = slice
66
-
66
+ def emit_body
67
+ body = node.array.each_slice(2).to_a
68
+ max = body.length - 1
69
+ body.each_with_index do |(key, value), index|
67
70
  visit(key)
68
- emit ' => '
71
+ emit(' => ')
69
72
  visit(value)
70
73
 
71
74
  if index < max
72
- emit ', '
75
+ emit(', ')
73
76
  end
74
77
  end
75
-
76
- emit '}'
77
78
  end
78
79
 
79
80
  end
@@ -0,0 +1,37 @@
1
+ module ToSource
2
+ class Emitter
3
+ class Loop < self
4
+
5
+ private
6
+
7
+ def dispatch
8
+ emit(self.class::KEYWORD)
9
+ space
10
+ visit(condition)
11
+ indent
12
+ visit(body)
13
+ unindent
14
+ emit_end
15
+ end
16
+
17
+ def condition
18
+ node.condition
19
+ end
20
+
21
+ def body
22
+ node.body
23
+ end
24
+
25
+ class While < self
26
+ handle(Rubinius::AST::While)
27
+ KEYWORD = :while
28
+ end
29
+
30
+ class Until < self
31
+ handle(Rubinius::AST::Until)
32
+ KEYWORD = :until
33
+ end
34
+
35
+ end
36
+ end
37
+ end
@@ -7,9 +7,10 @@ module ToSource
7
7
  private
8
8
 
9
9
  def dispatch
10
- visit(node.value)
10
+ util = node
11
+ visit(util.value)
11
12
  emit(' =~ ')
12
- visit(node.pattern)
13
+ visit(util.pattern)
13
14
  end
14
15
 
15
16
  end
@@ -7,11 +7,11 @@ module ToSource
7
7
  private
8
8
 
9
9
  def dispatch
10
- emit(:module)
11
- space
12
- visit(node.name)
10
+ util = node
11
+ emit('module ')
12
+ visit(util.name)
13
13
  indent
14
- visit(node.body)
14
+ visit(util.body)
15
15
  unindent
16
16
  emit_end
17
17
  end
@@ -7,11 +7,18 @@ module ToSource
7
7
  private
8
8
 
9
9
  def dispatch
10
- body = node.left.body
11
- run(Util::DelimitedBody, node.left.body)
10
+ emit_left
12
11
  emit(' = ')
12
+ emit_right
13
+ end
14
+
15
+ def emit_left
16
+ run(Util::DelimitedBody, node.left.body)
17
+ end
18
+
19
+ def emit_right
13
20
  right = node.right
14
- if node.right.kind_of?(Rubinius::AST::ArrayLiteral)
21
+ if right.kind_of?(Rubinius::AST::ArrayLiteral)
15
22
  run(Util::DelimitedBody, right.body)
16
23
  else
17
24
  visit(right)
@@ -7,11 +7,12 @@ module ToSource
7
7
  private
8
8
 
9
9
  def dispatch
10
- visit(node.receiver)
10
+ util = node
11
+ visit(util.receiver)
11
12
  emit('[')
12
- visit(node.arguments.array.first)
13
+ visit(util.arguments.array.first)
13
14
  emit('] ||= ')
14
- visit(node.value)
15
+ visit(util.value)
15
16
  end
16
17
 
17
18
  end
@@ -7,11 +7,12 @@ module ToSource
7
7
  private
8
8
 
9
9
  def dispatch
10
- visit(node.receiver)
10
+ util = node
11
+ visit(util.receiver)
11
12
  emit('.')
12
- emit(node.name)
13
+ emit(util.name)
13
14
  emit(' |= ')
14
- visit(node.value)
15
+ visit(util.value)
15
16
  end
16
17
 
17
18
  end
@@ -8,11 +8,12 @@ module ToSource
8
8
  private
9
9
 
10
10
  def dispatch
11
+ util = node
11
12
  emit('begin')
12
13
  indent
13
- visit(node.body)
14
+ visit(util.body)
14
15
  unindent
15
- visit(node.rescue)
16
+ visit(util.rescue)
16
17
  emit_end
17
18
  end
18
19
 
@@ -8,7 +8,7 @@ module ToSource
8
8
 
9
9
  def dispatch
10
10
  emit('rescue')
11
- emit_condition
11
+ emit_conditions
12
12
  emit_splat
13
13
  emit_assignment
14
14
  emit_body
@@ -21,23 +21,23 @@ module ToSource
21
21
  unindent
22
22
  end
23
23
 
24
- def emit_condition
24
+ def emit_conditions
25
25
  conditions = node.conditions || return
26
-
27
26
  body = conditions.body
28
27
 
29
28
  first = body.first
30
29
  unless body.one? and first.kind_of?(Rubinius::AST::ConstantAccess) and first.name == :StandardError
31
- emit(' ')
30
+ space
32
31
  run(Util::DelimitedBody, body)
33
32
  end
34
33
  end
35
34
 
36
35
  def emit_splat
37
- splat = node.splat || return
38
- emit(',') if node.conditions
39
- emit(' ')
40
- visit(node.splat)
36
+ util = node
37
+ splat = util.splat || return
38
+ emit(',') if util.conditions
39
+ space
40
+ visit(splat)
41
41
  end
42
42
 
43
43
  def emit_assignment
@@ -47,8 +47,8 @@ module ToSource
47
47
  end
48
48
 
49
49
  def emit_next
50
- condition = node.next || return
51
- visit(node.next)
50
+ next_rescue = node.next || return
51
+ visit(next_rescue)
52
52
  end
53
53
 
54
54
  end
@@ -6,10 +6,14 @@ module ToSource
6
6
  handle(Rubinius::AST::ScopedModuleName)
7
7
  handle(Rubinius::AST::ScopedConstant)
8
8
 
9
+ private
10
+
11
+ delegate(:parent, :name)
12
+
9
13
  def dispatch
10
- visit(node.parent)
14
+ visit(parent)
11
15
  emit('::')
12
- emit(node.name)
16
+ emit(name)
13
17
  end
14
18
 
15
19
  end
@@ -24,22 +24,42 @@ module ToSource
24
24
  end
25
25
 
26
26
  def normal_dispatch
27
- unless node.privately
28
- visit(node.receiver)
29
- emit('.')
30
- end
31
-
32
- emit(node.name)
27
+ emit_receiver
28
+ emit_name
29
+ emit_block_pass
33
30
  emit_block
34
31
  end
35
32
 
33
+ def block?
34
+ !!block
35
+ end
36
+
37
+ delegate(:block, :name, :receiver)
38
+
39
+ def block_pass?
40
+ block? && block.kind_of?(Rubinius::AST::BlockPass19)
41
+ end
42
+
43
+ def emit_name
44
+ emit(name)
45
+ end
46
+
47
+ def emit_receiver
48
+ return if node.privately
49
+ visit(receiver)
50
+ emit('.')
51
+ end
52
+
36
53
  def emit_block
37
- block = node.block
38
- return unless block
39
- pass = block.kind_of?(Rubinius::AST::BlockPass19)
40
- emit('(') if pass
41
- visit(node.block)
42
- emit(')') if pass
54
+ return unless block? and !block_pass?
55
+ visit(block)
56
+ end
57
+
58
+ def emit_block_pass
59
+ return unless block_pass?
60
+ emit('(')
61
+ visit(block)
62
+ emit(')')
43
63
  end
44
64
 
45
65
  end
@@ -1,22 +1,26 @@
1
1
  module ToSource
2
2
  class Emitter
3
- class SendWithArguments < self
3
+ class SendWithArguments < Send
4
4
 
5
5
  handle(Rubinius::AST::SendWithArguments)
6
6
 
7
7
  private
8
8
 
9
9
  def dispatch
10
- if node.name == :[]
11
- run(ElementReference, node)
10
+ if element_reference?
11
+ run(ElementReference)
12
12
  return
13
13
  end
14
14
  if binary_operator_method?
15
- run(BinaryOperatorMethod, node)
15
+ run(BinaryOperatorMethod)
16
16
  return
17
17
  end
18
18
  normal_dispatch
19
19
  end
20
+
21
+ def element_reference?
22
+ name == :[]
23
+ end
20
24
 
21
25
  BINARY_OPERATORS = %w(
22
26
  + - * / & | && || << >> ==
@@ -28,17 +32,11 @@ module ToSource
28
32
  BINARY_OPERATORS.include?(node.name)
29
33
  end
30
34
 
31
- def emit_receiver
32
- unless node.privately
33
- visit(node.receiver)
34
- emit('.')
35
- end
36
- end
37
-
38
35
  def normal_dispatch
39
36
  emit_receiver
40
- emit(node.name)
37
+ emit_name
41
38
  emit_arguments
39
+ emit_block
42
40
  end
43
41
 
44
42
  def emit_arguments
@@ -49,12 +47,9 @@ module ToSource
49
47
  end
50
48
 
51
49
  def emit_block_pass(emitter)
52
- block = node.block
53
-
54
- if block && block.kind_of?(Rubinius::AST::BlockPass19)
55
- emit(', ') if emitter.any?
56
- visit(block)
57
- end
50
+ return unless block_pass?
51
+ emit(', ') if emitter.any?
52
+ visit(block)
58
53
  end
59
54
 
60
55
  end
@@ -5,17 +5,20 @@ module ToSource
5
5
 
6
6
  handle(Rubinius::AST::SClass)
7
7
 
8
+ private
9
+
8
10
  def dispatch
9
- emit(:class)
10
- space
11
- emit(:<<)
12
- space
11
+ emit('class << ')
13
12
  visit(node.receiver)
13
+ emit_body
14
+ emit_end
15
+ end
16
+
17
+ def emit_body
14
18
  indent
15
19
  # FIXME: attr_reader missing on Rubinius::AST::SClass
16
20
  visit(node.instance_variable_get(:@body))
17
21
  unindent
18
- emit_end
19
22
  end
20
23
 
21
24
  end
@@ -1,6 +1,6 @@
1
1
  module ToSource
2
2
  class Emitter
3
- class UnaryOperatorMethod < self
3
+ class UnaryOperatorMethod < Send
4
4
 
5
5
  UNARY_MAPPING = {
6
6
  :-@ => :-,
@@ -12,7 +12,7 @@ module ToSource
12
12
  def dispatch
13
13
  name = node.name
14
14
  emit(UNARY_MAPPING.fetch(name, name))
15
- visit(node.receiver)
15
+ visit(receiver)
16
16
  end
17
17
 
18
18
  end
@@ -1,16 +1,26 @@
1
1
  module ToSource
2
2
  class Emitter
3
3
  class Unless < self
4
+
5
+ private
4
6
 
5
7
  def dispatch
6
8
  emit('unless ')
7
- visit(node.condition)
9
+ visit(condition)
8
10
  indent
9
- visit(node.else)
11
+ visit(else_body)
10
12
  unindent
11
13
  emit_end
12
14
  end
13
15
 
16
+ def condition
17
+ node.condition
18
+ end
19
+
20
+ def else_body
21
+ node.else
22
+ end
23
+
14
24
  end
15
25
  end
16
26
  end
@@ -6,8 +6,9 @@ module ToSource
6
6
  private
7
7
 
8
8
  def dispatch
9
- max = node.length - 1
10
- node.each_with_index do |member, index|
9
+ body = node
10
+ max = body.length - 1
11
+ body.each_with_index do |member, index|
11
12
  visit(member)
12
13
  emit(', ') if index < max
13
14
  end
@@ -9,7 +9,7 @@ module ToSource
9
9
  arguments = node.arguments
10
10
  unless arguments.array.empty?
11
11
  emit('(')
12
- visit(node.arguments)
12
+ visit(arguments)
13
13
  emit(')')
14
14
  end
15
15
  end
@@ -439,15 +439,6 @@ describe ToSource,'.to_source' do
439
439
  assert_source 'foo.bar(:baz, :yeah)'
440
440
  end
441
441
 
442
- context 'with block' do
443
- assert_source <<-RUBY
444
- foo.bar do
445
- 3
446
- 4
447
- end
448
- RUBY
449
- end
450
-
451
442
  context 'to self' do
452
443
 
453
444
  context 'explicitly' do
@@ -473,6 +464,15 @@ describe ToSource,'.to_source' do
473
464
  end
474
465
  end
475
466
 
467
+ context 'with block' do
468
+ assert_source <<-RUBY
469
+ foo.bar do
470
+ 3
471
+ 4
472
+ end
473
+ RUBY
474
+ end
475
+
476
476
  context 'with block that takes pattern and formal arguments' do
477
477
  assert_source <<-RUBY
478
478
  foo.bar do |(a, b), c|
@@ -499,6 +499,14 @@ describe ToSource,'.to_source' do
499
499
  RUBY
500
500
  end
501
501
 
502
+ context 'with arguments and block' do
503
+ assert_source <<-RUBY
504
+ foo.bar(baz) do
505
+ 4
506
+ end
507
+ RUBY
508
+ end
509
+
502
510
  context 'with splat argument' do
503
511
  assert_source 'foo.bar(*args)'
504
512
  end
@@ -835,6 +843,14 @@ describe ToSource,'.to_source' do
835
843
  end
836
844
  end
837
845
 
846
+ context 'loop' do
847
+ assert_source <<-RUBY
848
+ loop do
849
+ foo
850
+ end
851
+ RUBY
852
+ end
853
+
838
854
  context 'while' do
839
855
  context 'single statement in body' do
840
856
  assert_source <<-RUBY
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.12'
4
+ s.version = '0.2.13'
5
5
  s.authors = ['Markus Schirp']
6
6
  s.email = ['mbj@seonic.net']
7
7
  s.homepage = 'http://github.com/mbj/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.12
4
+ version: 0.2.13
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -135,6 +135,7 @@ files:
135
135
  - lib/to_source/emitter/keyword_value.rb
136
136
  - lib/to_source/emitter/literal.rb
137
137
  - lib/to_source/emitter/literal/dynamic.rb
138
+ - lib/to_source/emitter/loop.rb
138
139
  - lib/to_source/emitter/match3.rb
139
140
  - lib/to_source/emitter/module.rb
140
141
  - lib/to_source/emitter/multiple_assignment.rb
@@ -160,10 +161,8 @@ files:
160
161
  - lib/to_source/emitter/toplevel.rb
161
162
  - lib/to_source/emitter/unary_operator_method.rb
162
163
  - lib/to_source/emitter/unless.rb
163
- - lib/to_source/emitter/until.rb
164
164
  - lib/to_source/emitter/util.rb
165
165
  - lib/to_source/emitter/when.rb
166
- - lib/to_source/emitter/while.rb
167
166
  - lib/to_source/emitter/yield.rb
168
167
  - lib/to_source/emitter/z_super.rb
169
168
  - lib/to_source/state.rb
@@ -1,20 +0,0 @@
1
- module ToSource
2
- class Emitter
3
- class Until < self
4
-
5
- handle(Rubinius::AST::Until)
6
-
7
- private
8
-
9
- def dispatch
10
- emit('until ')
11
- visit(node.condition)
12
- indent
13
- visit(node.body)
14
- unindent
15
- emit_end
16
- end
17
-
18
- end
19
- end
20
- end
@@ -1,20 +0,0 @@
1
- module ToSource
2
- class Emitter
3
- class While < self
4
-
5
- handle(Rubinius::AST::While)
6
-
7
- private
8
-
9
- def dispatch
10
- emit('while ')
11
- visit(node.condition)
12
- indent
13
- visit(node.body)
14
- unindent
15
- emit_end
16
- end
17
-
18
- end
19
- end
20
- end