unparser 0.2.8 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (83) hide show
  1. checksums.yaml +4 -4
  2. data/.circleci/config.yml +17 -0
  3. data/.rubocop.yml +1 -0
  4. data/Changelog.md +4 -0
  5. data/Gemfile +2 -0
  6. data/Gemfile.lock +70 -64
  7. data/Rakefile +1 -0
  8. data/bin/unparser +2 -0
  9. data/config/devtools.yml +1 -1
  10. data/config/flay.yml +1 -1
  11. data/config/reek.yml +136 -136
  12. data/config/rubocop.yml +14 -1
  13. data/lib/unparser.rb +2 -0
  14. data/lib/unparser/ast.rb +8 -6
  15. data/lib/unparser/ast/local_variable_scope.rb +3 -1
  16. data/lib/unparser/buffer.rb +5 -1
  17. data/lib/unparser/cli.rb +8 -1
  18. data/lib/unparser/cli/color.rb +2 -0
  19. data/lib/unparser/cli/differ.rb +2 -0
  20. data/lib/unparser/cli/source.rb +7 -1
  21. data/lib/unparser/comments.rb +5 -0
  22. data/lib/unparser/constants.rb +8 -6
  23. data/lib/unparser/dsl.rb +2 -0
  24. data/lib/unparser/emitter.rb +18 -9
  25. data/lib/unparser/emitter/alias.rb +2 -0
  26. data/lib/unparser/emitter/argument.rb +3 -0
  27. data/lib/unparser/emitter/assignment.rb +5 -2
  28. data/lib/unparser/emitter/begin.rb +3 -1
  29. data/lib/unparser/emitter/binary.rb +2 -0
  30. data/lib/unparser/emitter/block.rb +3 -0
  31. data/lib/unparser/emitter/case.rb +4 -0
  32. data/lib/unparser/emitter/cbase.rb +2 -0
  33. data/lib/unparser/emitter/class.rb +3 -0
  34. data/lib/unparser/emitter/def.rb +3 -0
  35. data/lib/unparser/emitter/defined.rb +2 -0
  36. data/lib/unparser/emitter/empty.rb +2 -0
  37. data/lib/unparser/emitter/ensure.rb +2 -0
  38. data/lib/unparser/emitter/flipflop.rb +2 -0
  39. data/lib/unparser/emitter/flow_modifier.rb +3 -1
  40. data/lib/unparser/emitter/for.rb +2 -0
  41. data/lib/unparser/emitter/hookexe.rb +2 -0
  42. data/lib/unparser/emitter/if.rb +5 -0
  43. data/lib/unparser/emitter/literal.rb +2 -0
  44. data/lib/unparser/emitter/literal/array.rb +5 -4
  45. data/lib/unparser/emitter/literal/dynamic.rb +2 -0
  46. data/lib/unparser/emitter/literal/dynamic_body.rb +2 -0
  47. data/lib/unparser/emitter/literal/execute_string.rb +2 -0
  48. data/lib/unparser/emitter/literal/hash.rb +3 -2
  49. data/lib/unparser/emitter/literal/primitive.rb +3 -1
  50. data/lib/unparser/emitter/literal/range.rb +2 -0
  51. data/lib/unparser/emitter/literal/regexp.rb +2 -0
  52. data/lib/unparser/emitter/literal/singleton.rb +2 -0
  53. data/lib/unparser/emitter/match.rb +2 -0
  54. data/lib/unparser/emitter/meta.rb +2 -0
  55. data/lib/unparser/emitter/module.rb +2 -0
  56. data/lib/unparser/emitter/op_assign.rb +2 -0
  57. data/lib/unparser/emitter/redo.rb +2 -0
  58. data/lib/unparser/emitter/repetition.rb +4 -1
  59. data/lib/unparser/emitter/resbody.rb +4 -24
  60. data/lib/unparser/emitter/rescue.rb +5 -2
  61. data/lib/unparser/emitter/retry.rb +2 -0
  62. data/lib/unparser/emitter/root.rb +2 -0
  63. data/lib/unparser/emitter/send.rb +3 -0
  64. data/lib/unparser/emitter/send/arguments.rb +2 -0
  65. data/lib/unparser/emitter/send/attribute_assignment.rb +2 -0
  66. data/lib/unparser/emitter/send/binary.rb +2 -0
  67. data/lib/unparser/emitter/send/conditional.rb +2 -0
  68. data/lib/unparser/emitter/send/index.rb +2 -0
  69. data/lib/unparser/emitter/send/regular.rb +3 -0
  70. data/lib/unparser/emitter/send/unary.rb +5 -3
  71. data/lib/unparser/emitter/splat.rb +2 -0
  72. data/lib/unparser/emitter/super.rb +2 -0
  73. data/lib/unparser/emitter/undef.rb +2 -0
  74. data/lib/unparser/emitter/variable.rb +3 -0
  75. data/lib/unparser/emitter/yield.rb +3 -0
  76. data/lib/unparser/finalize.rb +2 -0
  77. data/lib/unparser/node_helpers.rb +6 -0
  78. data/lib/unparser/preprocessor.rb +3 -0
  79. data/spec/integrations.yml +3 -1
  80. data/spec/unit/unparser_spec.rb +3 -7
  81. data/unparser.gemspec +3 -3
  82. metadata +6 -6
  83. data/circle.yml +0 -6
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Unparser
2
4
  class Emitter
3
5
 
@@ -36,7 +38,7 @@ module Unparser
36
38
  children.empty?
37
39
  end
38
40
 
39
- TERMINATING_PARENT = %i(root interpolated dyn_str_body).to_set.freeze
41
+ TERMINATING_PARENT = %i[root interpolated dyn_str_body].to_set.freeze
40
42
 
41
43
  private
42
44
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Unparser
2
4
  class Emitter
3
5
  # Base class for binary emitters
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Unparser
2
4
  class Emitter
3
5
 
@@ -34,6 +36,7 @@ module Unparser
34
36
  #
35
37
  def emit_block_arguments
36
38
  return if arguments.children.empty?
39
+
37
40
  ws
38
41
  visit_parentheses(arguments, T_PIPE, T_PIPE)
39
42
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Unparser
2
4
  class Emitter
3
5
  # Emitter for case nodes
@@ -34,6 +36,7 @@ module Unparser
34
36
  def emit_else
35
37
  else_branch = children.last
36
38
  return unless else_branch
39
+
37
40
  write(K_ELSE)
38
41
  visit_indented(else_branch)
39
42
  end
@@ -57,6 +60,7 @@ module Unparser
57
60
  #
58
61
  def emit_condition
59
62
  return unless condition
63
+
60
64
  write(WS)
61
65
  visit(condition)
62
66
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Unparser
2
4
  class Emitter
3
5
  # Emitter for toplevel constant reference nodes
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Unparser
2
4
  class Emitter
3
5
  # Emitter for class nodes
@@ -32,6 +34,7 @@ module Unparser
32
34
  #
33
35
  def emit_superclass
34
36
  return unless superclass
37
+
35
38
  write(WS, T_LT, WS)
36
39
  visit(superclass)
37
40
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Unparser
2
4
  class Emitter
3
5
  # Emitter for def node
@@ -47,6 +49,7 @@ module Unparser
47
49
  #
48
50
  def emit_arguments
49
51
  return if arguments.children.empty?
52
+
50
53
  visit_parentheses(arguments)
51
54
  end
52
55
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Unparser
2
4
  class Emitter
3
5
  # Emitter for defined? nodes
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Unparser
2
4
  class Emitter
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Unparser
2
4
  class Emitter
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Unparser
2
4
  class Emitter
3
5
  # Emitter for flip flops
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Unparser
2
4
  class Emitter
3
5
  # Emitter control flow modifiers
@@ -50,7 +52,7 @@ module Unparser
50
52
  end
51
53
  end
52
54
 
53
- PARENS = [:if, :case, :begin].to_set.freeze
55
+ PARENS = %i[if case begin].to_set.freeze
54
56
 
55
57
  # Emit argument
56
58
  #
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Unparser
2
4
  class Emitter
3
5
  # Emitter for for nodes
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Unparser
2
4
  class Emitter
3
5
  # Base class for pre and postexe emitters
@@ -1,6 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Unparser
2
4
  class Emitter
3
5
  # Emitter if nodes
6
+ #
7
+ # ignore :reek:RepeatedConditional
4
8
  class If < self
5
9
  handle :if
6
10
 
@@ -122,6 +126,7 @@ module Unparser
122
126
  #
123
127
  def emit_else_branch
124
128
  return unless else_branch
129
+
125
130
  write(K_ELSE) unless unless?
126
131
  visit_indented(else_branch)
127
132
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Unparser
2
4
  class Emitter
3
5
  # Namespace class for literal emiters
@@ -1,12 +1,13 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Unparser
2
4
  class Emitter
3
5
  class Literal
4
6
 
5
7
  # Array literal emitter
6
8
  class Array < self
7
- OPEN = '['.freeze
8
- CLOSE = ']'.freeze
9
- DELIMITER = ', '.freeze
9
+ OPEN = '['.freeze
10
+ CLOSE = ']'.freeze
10
11
 
11
12
  handle :array
12
13
 
@@ -20,7 +21,7 @@ module Unparser
20
21
  #
21
22
  def dispatch
22
23
  parentheses(OPEN, CLOSE) do
23
- delimited(children, DELIMITER)
24
+ delimited(children)
24
25
  end
25
26
  end
26
27
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Unparser
2
4
  class Emitter
3
5
  class Literal
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Unparser
2
4
  class Emitter
3
5
  class Literal
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Unparser
2
4
  class Emitter
3
5
  class Literal
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Unparser
2
4
  class Emitter
3
5
  class Literal
@@ -70,7 +72,6 @@ module Unparser
70
72
  # Emitter for hash bodies
71
73
  class HashBody < self
72
74
 
73
- DELIMITER = ', '.freeze
74
75
  BAREWORD = /\A[A-Za-z_][A-Za-z_0-9]*[?!]?\z/.freeze
75
76
 
76
77
  handle :hash_body
@@ -84,7 +85,7 @@ module Unparser
84
85
  # @api private
85
86
  #
86
87
  def dispatch
87
- delimited(effective_body, DELIMITER)
88
+ delimited(effective_body)
88
89
  end
89
90
 
90
91
  # Return effective body
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Unparser
2
4
  class Emitter
3
5
  class Literal
@@ -131,7 +133,7 @@ module Unparser
131
133
  # @api private
132
134
  #
133
135
  def dispatch
134
- conditional_parentheses(parent.is_a?(Emitter::Send) && value < 0) do
136
+ conditional_parentheses(parent.is_a?(Send) && value.negative?) do
135
137
  write(value.inspect)
136
138
  end
137
139
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Unparser
2
4
  class Emitter
3
5
  class Literal
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Unparser
2
4
  class Emitter
3
5
  class Literal
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Unparser
2
4
  class Emitter
3
5
  class Literal
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Unparser
2
4
  class Emitter
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Unparser
2
4
  class Emitter
3
5
  # Namespace class for meta emitters
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Unparser
2
4
  class Emitter
3
5
  # Emitter for module nodes
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Unparser
2
4
  class Emitter
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Unparser
2
4
  class Emitter
3
5
  # Emitter for redo nodes
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Unparser
2
4
  class Emitter
3
5
 
@@ -65,7 +67,8 @@ module Unparser
65
67
  # @api private
66
68
  #
67
69
  def postcontrol?
68
- return false unless body
70
+ return nil unless body # greez from falsyness
71
+
69
72
  local_variable_scope.first_assignment_in_body_and_used_in_condition?(body, condition)
70
73
  end
71
74
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Unparser
2
4
  class Emitter
3
5
  # Emitter for rescue body nodes
@@ -20,30 +22,6 @@ module Unparser
20
22
  write(K_RESCUE, WS)
21
23
  visit_plain(body)
22
24
  end
23
-
24
- # Emit exception
25
- #
26
- # @return [undefined]
27
- #
28
- # @api private
29
- #
30
- def emit_exception
31
- return unless exception
32
- ws
33
- delimited(exception.children)
34
- end
35
-
36
- # Emit assignment
37
- #
38
- # @return [undefined]
39
- #
40
- # @api private
41
- #
42
- def emit_assignment
43
- return unless assignment
44
- write(WS, T_ASR, WS)
45
- visit(assignment)
46
- end
47
25
  end
48
26
 
49
27
  # Emitter for resbody in keyworkd-embedded form
@@ -74,6 +52,7 @@ module Unparser
74
52
  #
75
53
  def emit_exception
76
54
  return unless exception
55
+
77
56
  ws
78
57
  delimited(exception.children)
79
58
  end
@@ -86,6 +65,7 @@ module Unparser
86
65
  #
87
66
  def emit_assignment
88
67
  return unless assignment
68
+
89
69
  write(WS, T_ASR, WS)
90
70
  visit(assignment)
91
71
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Unparser
2
4
  class Emitter
3
5
  # Emitter for rescue nodes
@@ -10,9 +12,9 @@ module Unparser
10
12
 
11
13
  define_group :rescue_bodies, 1..-2
12
14
 
13
- EMBEDDED_TYPES = [:def, :defs, :kwbegin, :ensure].to_set.freeze
15
+ EMBEDDED_TYPES = %i[def defs kwbegin ensure].to_set.freeze
14
16
 
15
- NOINDENT_STANDALONE_RESCUE = [:root, :begin, :pair_rocket, :pair_colon, :lvasgn, :ivasgn].to_set.freeze
17
+ NOINDENT_STANDALONE_RESCUE = %i[root begin pair_rocket pair_colon lvasgn ivasgn].to_set.freeze
16
18
 
17
19
  private
18
20
 
@@ -86,6 +88,7 @@ module Unparser
86
88
  #
87
89
  def emit_else
88
90
  return unless else_branch
91
+
89
92
  write(K_ELSE)
90
93
  visit_indented(else_branch)
91
94
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Unparser
2
4
  class Emitter
3
5
  # Emitter for retry nodes
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Unparser
2
4
  class Emitter
3
5
  # Root emitter a special case
@@ -1,6 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Unparser
2
4
  class Emitter
3
5
  # Emitter for send
6
+ # ignore :reek:TooManyMethods
4
7
  class Send < self
5
8
 
6
9
  handle :send
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Unparser
2
4
  class Emitter
3
5
  class Send
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Unparser
2
4
  class Emitter
3
5
  class Send