to_source 0.2.15 → 0.2.16

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.
Files changed (76) hide show
  1. data/.gitignore +6 -6
  2. data/Changelog.md +4 -0
  3. data/README.md +1 -1
  4. data/bin/to_source +1 -0
  5. data/config/flay.yml +1 -1
  6. data/config/flog.yml +1 -1
  7. data/config/roodi.yml +4 -4
  8. data/config/site.reek +5 -2
  9. data/config/yardstick.yml +1 -1
  10. data/lib/to_source.rb +1 -1
  11. data/lib/to_source/command.rb +63 -1
  12. data/lib/to_source/emitter.rb +221 -32
  13. data/lib/to_source/emitter/access.rb +8 -0
  14. data/lib/to_source/emitter/actual_arguments.rb +51 -8
  15. data/lib/to_source/emitter/alias.rb +11 -2
  16. data/lib/to_source/emitter/assignment.rb +62 -22
  17. data/lib/to_source/emitter/attribute_assignment.rb +11 -2
  18. data/lib/to_source/emitter/begin.rb +34 -14
  19. data/lib/to_source/emitter/binary_operator.rb +34 -17
  20. data/lib/to_source/emitter/binary_operator_method.rb +36 -17
  21. data/lib/to_source/emitter/block.rb +7 -0
  22. data/lib/to_source/emitter/block_argument.rb +7 -0
  23. data/lib/to_source/emitter/block_pass.rb +7 -0
  24. data/lib/to_source/emitter/case.rb +85 -0
  25. data/lib/to_source/emitter/class.rb +21 -6
  26. data/lib/to_source/emitter/concat_arguments.rb +11 -3
  27. data/lib/to_source/emitter/default_arguments.rb +7 -0
  28. data/lib/to_source/emitter/define.rb +46 -8
  29. data/lib/to_source/emitter/defined.rb +7 -0
  30. data/lib/to_source/emitter/element_assignment.rb +10 -1
  31. data/lib/to_source/emitter/element_reference.rb +10 -1
  32. data/lib/to_source/emitter/empty_body.rb +9 -0
  33. data/lib/to_source/emitter/ensure.rb +34 -7
  34. data/lib/to_source/emitter/ensure_body.rb +14 -5
  35. data/lib/to_source/emitter/execute_string.rb +9 -0
  36. data/lib/to_source/emitter/formal_arguments.rb +131 -39
  37. data/lib/to_source/emitter/if.rb +59 -14
  38. data/lib/to_source/emitter/iter.rb +10 -3
  39. data/lib/to_source/emitter/keyword_value.rb +28 -4
  40. data/lib/to_source/emitter/literal.rb +99 -14
  41. data/lib/to_source/emitter/literal/dynamic.rb +88 -13
  42. data/lib/to_source/emitter/loop.rb +14 -11
  43. data/lib/to_source/emitter/match3.rb +7 -0
  44. data/lib/to_source/emitter/module.rb +10 -3
  45. data/lib/to_source/emitter/multiple_assignment.rb +19 -0
  46. data/lib/to_source/emitter/nth_ref.rb +7 -0
  47. data/lib/to_source/emitter/op_assign1.rb +7 -0
  48. data/lib/to_source/emitter/op_assign2.rb +7 -0
  49. data/lib/to_source/emitter/pattern_arguments.rb +7 -0
  50. data/lib/to_source/emitter/rescue.rb +10 -4
  51. data/lib/to_source/emitter/rescue_condition.rb +40 -3
  52. data/lib/to_source/emitter/scope.rb +7 -0
  53. data/lib/to_source/emitter/scope_name.rb +7 -0
  54. data/lib/to_source/emitter/scoped_name.rb +7 -0
  55. data/lib/to_source/emitter/send.rb +71 -5
  56. data/lib/to_source/emitter/send_with_arguments.rb +53 -7
  57. data/lib/to_source/emitter/singleton_class.rb +17 -5
  58. data/lib/to_source/emitter/splat.rb +7 -0
  59. data/lib/to_source/emitter/splat_when.rb +7 -0
  60. data/lib/to_source/emitter/static.rb +19 -0
  61. data/lib/to_source/emitter/super.rb +49 -4
  62. data/lib/to_source/emitter/to_array.rb +7 -0
  63. data/lib/to_source/emitter/to_string.rb +7 -0
  64. data/lib/to_source/emitter/toplevel.rb +9 -0
  65. data/lib/to_source/emitter/unary_operator_method.rb +7 -0
  66. data/lib/to_source/emitter/unless.rb +23 -4
  67. data/lib/to_source/emitter/util.rb +8 -0
  68. data/lib/to_source/emitter/when.rb +28 -3
  69. data/lib/to_source/emitter/yield.rb +9 -0
  70. data/lib/to_source/emitter/z_super.rb +7 -0
  71. data/lib/to_source/state.rb +95 -11
  72. data/spec/unit/to_source/class_methods/run_spec.rb +13 -0
  73. data/to_source.gemspec +1 -1
  74. metadata +3 -4
  75. data/lib/to_source/buffer.rb +0 -40
  76. data/lib/to_source/emitter/receiver_case.rb +0 -35
@@ -4,8 +4,20 @@ module ToSource
4
4
 
5
5
  handle(Rubinius::AST::SendWithArguments)
6
6
 
7
+ BINARY_OPERATORS = %w(
8
+ + - * / & | && || << >> ==
9
+ === != <= < <=> > >= =~ !~ ^
10
+ **
11
+ ).map(&:to_sym).to_set
12
+
7
13
  private
8
14
 
15
+ # Perform dispatch
16
+ #
17
+ # @return [undefined]
18
+ #
19
+ # @api private
20
+ #
9
21
  def dispatch
10
22
  if element_reference?
11
23
  run(ElementReference)
@@ -18,20 +30,40 @@ module ToSource
18
30
  normal_dispatch
19
31
  end
20
32
 
33
+ # Test for element reference
34
+ #
35
+ # @return [true]
36
+ # if node is an element reference
37
+ #
38
+ # @return [false]
39
+ # otherwise
40
+ #
41
+ # @api private
42
+ #
21
43
  def element_reference?
22
44
  name == :[]
23
45
  end
24
46
 
25
- BINARY_OPERATORS = %w(
26
- + - * / & | && || << >> ==
27
- === != <= < <=> > >= =~ !~ ^
28
- **
29
- ).map(&:to_sym).to_set
30
-
47
+ # Test for binary operator method
48
+ #
49
+ # @return [true]
50
+ # if node is a binary operator method
51
+ #
52
+ # @return [false]
53
+ # otherwise
54
+ #
55
+ # @api private
56
+ #
31
57
  def binary_operator_method?
32
- BINARY_OPERATORS.include?(node.name)
58
+ BINARY_OPERATORS.include?(name)
33
59
  end
34
60
 
61
+ # Perform normal dispatch
62
+ #
63
+ # @return [undefined]
64
+ #
65
+ # @api private
66
+ #
35
67
  def normal_dispatch
36
68
  emit_receiver
37
69
  emit_name
@@ -39,6 +71,12 @@ module ToSource
39
71
  emit_block
40
72
  end
41
73
 
74
+ # Emit arguments
75
+ #
76
+ # @return [undefined]
77
+ #
78
+ # @api private
79
+ #
42
80
  def emit_arguments
43
81
  emit('(')
44
82
  emitter = visit(node.arguments)
@@ -46,6 +84,14 @@ module ToSource
46
84
  emit(')')
47
85
  end
48
86
 
87
+ # Emit block pass
88
+ #
89
+ # @param [Class:Emitter] emitter
90
+ #
91
+ # @return [undefined]
92
+ #
93
+ # @api private
94
+ #
49
95
  def emit_block_pass(emitter)
50
96
  return unless block_pass?
51
97
  emit(', ') if emitter.any?
@@ -1,12 +1,18 @@
1
1
  module ToSource
2
2
  class Emitter
3
-
3
+ # Emitter for singleton class nodes
4
4
  class SingletonClass < self
5
5
 
6
6
  handle(Rubinius::AST::SClass)
7
7
 
8
8
  private
9
9
 
10
+ # Perform dispatch
11
+ #
12
+ # @return [undefined]
13
+ #
14
+ # @api private
15
+ #
10
16
  def dispatch
11
17
  emit('class << ')
12
18
  visit(node.receiver)
@@ -14,11 +20,17 @@ module ToSource
14
20
  emit_end
15
21
  end
16
22
 
23
+ # Emit body
24
+ #
25
+ # @return [undefined]
26
+ #
27
+ # @api private
28
+ #
17
29
  def emit_body
18
- indent
19
- # FIXME: attr_reader missing on Rubinius::AST::SClass
20
- visit(node.instance_variable_get(:@body))
21
- unindent
30
+ indented do
31
+ # FIXME: attr_reader missing on Rubinius::AST::SClass
32
+ visit(node.instance_variable_get(:@body))
33
+ end
22
34
  end
23
35
 
24
36
  end
@@ -1,5 +1,6 @@
1
1
  module ToSource
2
2
  class Emitter
3
+ # Emitter for splat nodes
3
4
  class Splat < self
4
5
 
5
6
  handle(Rubinius::AST::SplatValue)
@@ -7,6 +8,12 @@ module ToSource
7
8
 
8
9
  private
9
10
 
11
+ # Perform dispatch
12
+ #
13
+ # @return [undefined]
14
+ #
15
+ # @api private
16
+ #
10
17
  def dispatch
11
18
  emit('*')
12
19
  visit(node.value)
@@ -1,11 +1,18 @@
1
1
  module ToSource
2
2
  class Emitter
3
+ # Emitter for spat when nodes
3
4
  class SplatWhen < self
4
5
 
5
6
  handle(Rubinius::AST::SplatWhen)
6
7
 
7
8
  private
8
9
 
10
+ # Perform dispatch
11
+ #
12
+ # @return [undefined]
13
+ #
14
+ # @api private
15
+ #
9
16
  def dispatch
10
17
  emit('*')
11
18
  visit(node.condition)
@@ -1,31 +1,50 @@
1
1
  module ToSource
2
2
  class Emitter
3
+ # Base class for emitters with static content
3
4
  class Static < self
4
5
 
5
6
  private
6
7
 
8
+ # Perform dispatch
9
+ #
10
+ # @return [undefined]
11
+ #
12
+ # @api private
13
+ #
7
14
  def dispatch
8
15
  emit(self.class::SYMBOL)
9
16
  end
10
17
 
18
+ # Emitter for next nodes
11
19
  class Next < self
20
+
12
21
  handle(Rubinius::AST::Next)
13
22
  SYMBOL = :next
23
+
14
24
  end
15
25
 
26
+ # Emitter for current exception node
16
27
  class CurrentException < self
28
+
17
29
  handle(Rubinius::AST::CurrentException)
18
30
  SYMBOL = :'$!'
31
+
19
32
  end
20
33
 
34
+ # Emitter for self node
21
35
  class Self < self
36
+
22
37
  handle(Rubinius::AST::Self)
23
38
  SYMBOL = :self
39
+
24
40
  end
25
41
 
42
+ # Emitter for file node
26
43
  class File < self
44
+
27
45
  handle(Rubinius::AST::File)
28
46
  SYMBOL = :__FILE__
47
+
29
48
  end
30
49
 
31
50
  end
@@ -1,17 +1,32 @@
1
1
  module ToSource
2
2
  class Emitter
3
+ # emitter for super node
3
4
  class Super < self
4
5
 
5
6
  handle(Rubinius::AST::Super)
6
7
 
7
8
  private
8
9
 
10
+ delegate(:block)
11
+
12
+ # Perform dispatch
13
+ #
14
+ # @return [undefined]
15
+ #
16
+ # @api private
17
+ #
9
18
  def dispatch
10
19
  emit('super')
11
20
  emit_arguments
12
21
  emit_block
13
22
  end
14
23
 
24
+ # Emit arguments
25
+ #
26
+ # @return [undefined]
27
+ #
28
+ # @api private
29
+ #
15
30
  def emit_arguments
16
31
  emit('(')
17
32
  emitter = visit(node.arguments)
@@ -19,24 +34,54 @@ module ToSource
19
34
  emit(')')
20
35
  end
21
36
 
22
- def block
23
- node.block
24
- end
25
-
37
+ # Test for block presence
38
+ #
39
+ # @return [true]
40
+ # if block is present
41
+ #
42
+ # @return [false]
43
+ # otherwise
44
+ #
45
+ # @api private
46
+ #
26
47
  def block?
27
48
  !!block
28
49
  end
29
50
 
51
+ # Test for block pass
52
+ #
53
+ # @return [true]
54
+ # if block pass is present
55
+ #
56
+ # @return [false]
57
+ # otherwise
58
+ #
59
+ # @api private
60
+ #
30
61
  def block_pass?
31
62
  block.kind_of?(Rubinius::AST::BlockPass19)
32
63
  end
33
64
 
65
+ # Emit block pass
66
+ #
67
+ # @param [Class:Emitter] emitter
68
+ #
69
+ # @return [undefined]
70
+ #
71
+ # @api private
72
+ #
34
73
  def emit_block_pass(emitter)
35
74
  return unless block? and block_pass?
36
75
  emit(', ') if emitter.any?
37
76
  visit(block)
38
77
  end
39
78
 
79
+ # Emit block
80
+ #
81
+ # @return [undefined]
82
+ #
83
+ # @api private
84
+ #
40
85
  def emit_block
41
86
  return unless block? and !block_pass?
42
87
  visit(block)
@@ -1,11 +1,18 @@
1
1
  module ToSource
2
2
  class Emitter
3
+ # Emit to array node
3
4
  class ToArray < self
4
5
 
5
6
  handle(Rubinius::AST::ToArray)
6
7
 
7
8
  private
8
9
 
10
+ # Perform dispatch
11
+ #
12
+ # @return [undefined]
13
+ #
14
+ # @api private
15
+ #
9
16
  def dispatch
10
17
  visit(node.value)
11
18
  end
@@ -1,11 +1,18 @@
1
1
  module ToSource
2
2
  class Emitter
3
+ # Emit to string node
3
4
  class ToString < self
4
5
 
5
6
  handle(Rubinius::AST::ToString)
6
7
 
7
8
  private
8
9
 
10
+ # Perform dispatch
11
+ #
12
+ # @return [undefined]
13
+ #
14
+ # @api private
15
+ #
9
16
  def dispatch
10
17
  emit('#{')
11
18
  visit(node.value)
@@ -1,10 +1,19 @@
1
1
  module ToSource
2
2
  class Emitter
3
+ # Emitter for toplevel constants or classes
3
4
  class Toplevel < self
4
5
 
5
6
  handle(Rubinius::AST::ToplevelClassName)
6
7
  handle(Rubinius::AST::ToplevelConstant)
7
8
 
9
+ private
10
+
11
+ # Perform dispatch
12
+ #
13
+ # @return [undefined]
14
+ #
15
+ # @api private
16
+ #
8
17
  def dispatch
9
18
  emit('::')
10
19
  emit(node.name)
@@ -1,5 +1,6 @@
1
1
  module ToSource
2
2
  class Emitter
3
+ # Emitter for unary operator method
3
4
  class UnaryOperatorMethod < Send
4
5
 
5
6
  UNARY_MAPPING = {
@@ -9,6 +10,12 @@ module ToSource
9
10
 
10
11
  private
11
12
 
13
+ # Perform dispatch
14
+ #
15
+ # @return [undefined]
16
+ #
17
+ # @api private
18
+ #
12
19
  def dispatch
13
20
  name = node.name
14
21
  emit(UNARY_MAPPING.fetch(name, name))
@@ -1,22 +1,41 @@
1
1
  module ToSource
2
2
  class Emitter
3
+ # Emitter for unless
3
4
  class Unless < self
4
5
 
5
6
  private
6
-
7
+
8
+ # Perform dispatch
9
+ #
10
+ # @return [undefined]
11
+ #
12
+ # @api private
13
+ #
7
14
  def dispatch
8
15
  emit('unless ')
9
16
  visit(condition)
10
- indent
11
- visit(else_body)
12
- unindent
17
+ indented do
18
+ visit(else_body)
19
+ end
13
20
  emit_end
14
21
  end
15
22
 
23
+ # Emit condition
24
+ #
25
+ # @return [undefined]
26
+ #
27
+ # @api private
28
+ #
16
29
  def condition
17
30
  node.condition
18
31
  end
19
32
 
33
+ # Emit else
34
+ #
35
+ # @return [undefined]
36
+ #
37
+ # @api private
38
+ #
20
39
  def else_body
21
40
  node.else
22
41
  end
@@ -1,10 +1,18 @@
1
1
  module ToSource
2
2
  class Emitter
3
+ # base class for utility emitters
3
4
  class Util < self
5
+ # Emitter for delimited bodies
4
6
  class DelimitedBody < self
5
7
 
6
8
  private
7
9
 
10
+ # Perform dispatch
11
+ #
12
+ # @return [undefined]
13
+ #
14
+ # @api private
15
+ #
8
16
  def dispatch
9
17
  body = node
10
18
  max = body.length - 1