unparser 0.4.8 → 0.5.3

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 (143) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +20 -5
  3. data/bin/unparser +1 -1
  4. data/lib/unparser.rb +115 -61
  5. data/lib/unparser/ast.rb +0 -1
  6. data/lib/unparser/ast/local_variable_scope.rb +6 -76
  7. data/lib/unparser/buffer.rb +19 -16
  8. data/lib/unparser/cli.rb +26 -39
  9. data/lib/unparser/color.rb +0 -3
  10. data/lib/unparser/comments.rb +0 -26
  11. data/lib/unparser/constants.rb +4 -53
  12. data/lib/unparser/diff.rb +0 -17
  13. data/lib/unparser/dsl.rb +0 -32
  14. data/lib/unparser/emitter.rb +23 -425
  15. data/lib/unparser/emitter/alias.rb +2 -8
  16. data/lib/unparser/emitter/args.rb +45 -0
  17. data/lib/unparser/emitter/argument.rb +8 -166
  18. data/lib/unparser/emitter/array.rb +27 -0
  19. data/lib/unparser/emitter/array_pattern.rb +29 -0
  20. data/lib/unparser/emitter/assignment.rb +36 -127
  21. data/lib/unparser/emitter/begin.rb +9 -84
  22. data/lib/unparser/emitter/binary.rb +7 -20
  23. data/lib/unparser/emitter/block.rb +57 -41
  24. data/lib/unparser/emitter/case.rb +6 -48
  25. data/lib/unparser/emitter/case_guard.rb +27 -0
  26. data/lib/unparser/emitter/case_match.rb +40 -0
  27. data/lib/unparser/emitter/cbase.rb +1 -3
  28. data/lib/unparser/emitter/class.rb +6 -26
  29. data/lib/unparser/emitter/const_pattern.rb +24 -0
  30. data/lib/unparser/emitter/def.rb +7 -51
  31. data/lib/unparser/emitter/defined.rb +2 -12
  32. data/lib/unparser/emitter/dstr.rb +22 -0
  33. data/lib/unparser/emitter/dsym.rb +41 -0
  34. data/lib/unparser/emitter/flipflop.rb +11 -10
  35. data/lib/unparser/emitter/float.rb +29 -0
  36. data/lib/unparser/emitter/flow_modifier.rb +8 -55
  37. data/lib/unparser/emitter/for.rb +5 -19
  38. data/lib/unparser/emitter/hash.rb +74 -0
  39. data/lib/unparser/emitter/hash_pattern.rb +67 -0
  40. data/lib/unparser/emitter/hookexe.rb +5 -11
  41. data/lib/unparser/emitter/if.rb +9 -73
  42. data/lib/unparser/emitter/in_match.rb +21 -0
  43. data/lib/unparser/emitter/in_pattern.rb +34 -0
  44. data/lib/unparser/emitter/index.rb +21 -88
  45. data/lib/unparser/emitter/kwbegin.rb +31 -0
  46. data/lib/unparser/emitter/lambda.rb +0 -8
  47. data/lib/unparser/emitter/masgn.rb +20 -0
  48. data/lib/unparser/emitter/match.rb +3 -17
  49. data/lib/unparser/emitter/match_alt.rb +23 -0
  50. data/lib/unparser/emitter/match_as.rb +21 -0
  51. data/lib/unparser/emitter/match_rest.rb +26 -0
  52. data/lib/unparser/emitter/match_var.rb +19 -0
  53. data/lib/unparser/emitter/mlhs.rb +40 -0
  54. data/lib/unparser/emitter/module.rb +3 -9
  55. data/lib/unparser/emitter/op_assign.rb +12 -27
  56. data/lib/unparser/emitter/pin.rb +19 -0
  57. data/lib/unparser/emitter/primitive.rb +93 -0
  58. data/lib/unparser/emitter/range.rb +35 -0
  59. data/lib/unparser/emitter/regexp.rb +35 -0
  60. data/lib/unparser/emitter/repetition.rb +17 -57
  61. data/lib/unparser/emitter/rescue.rb +1 -97
  62. data/lib/unparser/emitter/root.rb +17 -1
  63. data/lib/unparser/emitter/send.rb +10 -219
  64. data/lib/unparser/emitter/simple.rb +33 -0
  65. data/lib/unparser/emitter/splat.rb +2 -18
  66. data/lib/unparser/emitter/super.rb +1 -29
  67. data/lib/unparser/emitter/undef.rb +1 -9
  68. data/lib/unparser/emitter/variable.rb +1 -31
  69. data/lib/unparser/emitter/xstr.rb +72 -0
  70. data/lib/unparser/emitter/yield.rb +1 -9
  71. data/lib/unparser/generation.rb +250 -0
  72. data/lib/unparser/node_details.rb +21 -0
  73. data/lib/unparser/node_details/send.rb +62 -0
  74. data/lib/unparser/node_helpers.rb +45 -6
  75. data/lib/unparser/validation.rb +58 -35
  76. data/lib/unparser/writer.rb +15 -0
  77. data/lib/unparser/writer/binary.rb +99 -0
  78. data/lib/unparser/writer/dynamic_string.rb +233 -0
  79. data/lib/unparser/writer/resbody.rb +40 -0
  80. data/lib/unparser/writer/rescue.rb +39 -0
  81. data/lib/unparser/writer/send.rb +124 -0
  82. data/lib/unparser/{emitter → writer}/send/attribute_assignment.rb +11 -26
  83. data/lib/unparser/writer/send/binary.rb +27 -0
  84. data/lib/unparser/writer/send/conditional.rb +25 -0
  85. data/lib/unparser/writer/send/regular.rb +33 -0
  86. data/lib/unparser/{emitter → writer}/send/unary.rb +10 -17
  87. metadata +65 -89
  88. data/.github/workflows/ci.yml +0 -90
  89. data/.gitignore +0 -37
  90. data/.rspec +0 -4
  91. data/.rubocop.yml +0 -126
  92. data/Changelog.md +0 -162
  93. data/Gemfile +0 -9
  94. data/Gemfile.lock +0 -111
  95. data/LICENSE +0 -20
  96. data/Rakefile +0 -22
  97. data/config/devtools.yml +0 -2
  98. data/config/flay.yml +0 -3
  99. data/config/flog.yml +0 -2
  100. data/config/mutant.yml +0 -6
  101. data/config/reek.yml +0 -98
  102. data/config/yardstick.yml +0 -2
  103. data/lib/unparser/emitter/empty.rb +0 -23
  104. data/lib/unparser/emitter/ensure.rb +0 -37
  105. data/lib/unparser/emitter/literal.rb +0 -10
  106. data/lib/unparser/emitter/literal/array.rb +0 -29
  107. data/lib/unparser/emitter/literal/dynamic.rb +0 -53
  108. data/lib/unparser/emitter/literal/dynamic_body.rb +0 -132
  109. data/lib/unparser/emitter/literal/execute_string.rb +0 -38
  110. data/lib/unparser/emitter/literal/hash.rb +0 -156
  111. data/lib/unparser/emitter/literal/primitive.rb +0 -145
  112. data/lib/unparser/emitter/literal/range.rb +0 -36
  113. data/lib/unparser/emitter/literal/regexp.rb +0 -114
  114. data/lib/unparser/emitter/literal/singleton.rb +0 -26
  115. data/lib/unparser/emitter/meta.rb +0 -16
  116. data/lib/unparser/emitter/redo.rb +0 -25
  117. data/lib/unparser/emitter/resbody.rb +0 -76
  118. data/lib/unparser/emitter/retry.rb +0 -25
  119. data/lib/unparser/emitter/send/binary.rb +0 -57
  120. data/lib/unparser/emitter/send/conditional.rb +0 -40
  121. data/lib/unparser/emitter/send/regular.rb +0 -40
  122. data/lib/unparser/preprocessor.rb +0 -159
  123. data/spec/integration/unparser/corpus_spec.rb +0 -125
  124. data/spec/integrations.yml +0 -92
  125. data/spec/spec_helper.rb +0 -42
  126. data/spec/unit/unparser/buffer/append_spec.rb +0 -24
  127. data/spec/unit/unparser/buffer/append_without_prefix_spec.rb +0 -23
  128. data/spec/unit/unparser/buffer/capture_content_spec.rb +0 -17
  129. data/spec/unit/unparser/buffer/content_spec.rb +0 -38
  130. data/spec/unit/unparser/buffer/fresh_line_spec.rb +0 -20
  131. data/spec/unit/unparser/buffer/indent_spec.rb +0 -20
  132. data/spec/unit/unparser/buffer/nl_spec.rb +0 -16
  133. data/spec/unit/unparser/buffer/unindent_spec.rb +0 -20
  134. data/spec/unit/unparser/color_spec.rb +0 -40
  135. data/spec/unit/unparser/comments/consume_spec.rb +0 -22
  136. data/spec/unit/unparser/comments/take_all_spec.rb +0 -19
  137. data/spec/unit/unparser/comments/take_before_spec.rb +0 -46
  138. data/spec/unit/unparser/comments/take_eol_comments_spec.rb +0 -32
  139. data/spec/unit/unparser/diff_spec.rb +0 -189
  140. data/spec/unit/unparser/emitter/class_methods/handle_spec.rb +0 -17
  141. data/spec/unit/unparser/validation_spec.rb +0 -327
  142. data/spec/unit/unparser_spec.rb +0 -1920
  143. data/unparser.gemspec +0 -35
@@ -3,8 +3,6 @@
3
3
  module Unparser
4
4
 
5
5
  # Buffer used to emit into
6
- #
7
- # ignore :reek:TooManyMethods
8
6
  class Buffer
9
7
 
10
8
  NL = "\n".freeze
@@ -82,6 +80,13 @@ module Unparser
82
80
  self
83
81
  end
84
82
 
83
+ def root_indent
84
+ before = @indent
85
+ @indent = 0
86
+ yield
87
+ @indent = before
88
+ end
89
+
85
90
  # Test for a fresh line
86
91
  #
87
92
  # @return [Boolean]
@@ -114,26 +119,24 @@ module Unparser
114
119
  @content[size_before..-1]
115
120
  end
116
121
 
117
- private
118
-
119
- INDENT_SPACE = ' '.freeze
120
-
121
- # Write prefix
122
- #
123
- # @return [String]
122
+ # Write raw fragment to buffer
124
123
  #
125
- # @api private
126
- #
127
- def prefix
128
- write(INDENT_SPACE * @indent)
129
- end
130
-
131
- # Write to content buffer
124
+ # Does not do indentation logic.
132
125
  #
133
126
  # @param [String] fragment
134
127
  #
128
+ # @return [self]
135
129
  def write(fragment)
136
130
  @content << fragment
131
+ self
132
+ end
133
+
134
+ private
135
+
136
+ INDENT_SPACE = ' '.freeze
137
+
138
+ def prefix
139
+ write(INDENT_SPACE * @indent)
137
140
  end
138
141
 
139
142
  end # Buffer
@@ -1,13 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'unparser'
4
- require 'optparse'
5
-
6
3
  module Unparser
7
4
  # Unparser CLI implementation
8
- #
9
- # :reek:InstanceVariableAssumption
10
- # :reek:TooManyInstanceVariables
11
5
  class CLI
12
6
 
13
7
  EXIT_SUCCESS = 0
@@ -26,6 +20,13 @@ module Unparser
26
20
  def validation
27
21
  Validation.from_path(path)
28
22
  end
23
+
24
+ # Literal for this target
25
+ #
26
+ # @return [Validation]
27
+ def literal_validation
28
+ Validation::Literal.from_path(path)
29
+ end
29
30
  end
30
31
 
31
32
  # String target
@@ -38,6 +39,13 @@ module Unparser
38
39
  def validation
39
40
  Validation.from_string(string)
40
41
  end
42
+
43
+ # Literal for this target
44
+ #
45
+ # @return [Validation]
46
+ def literal_validation
47
+ Validation::Literal.from_string(path)
48
+ end
41
49
  end # String
42
50
  end # Target
43
51
 
@@ -47,7 +55,7 @@ module Unparser
47
55
  #
48
56
  # @param [Array<String>] arguments
49
57
  #
50
- # @return [Fixnum]
58
+ # @return [Integer]
51
59
  # the exit status
52
60
  #
53
61
  # @api private
@@ -63,15 +71,14 @@ module Unparser
63
71
  # @return [undefined]
64
72
  #
65
73
  # @api private
66
- #
67
- # ignore :reek:TooManyStatements
68
74
  def initialize(arguments)
69
75
  @ignore = Set.new
70
76
  @targets = []
71
77
 
72
- @success = true
73
- @fail_fast = false
74
- @verbose = false
78
+ @fail_fast = false
79
+ @success = true
80
+ @validation = :validation
81
+ @verbose = false
75
82
 
76
83
  opts = OptionParser.new do |builder|
77
84
  add_options(builder)
@@ -90,7 +97,7 @@ module Unparser
90
97
  #
91
98
  # @api private
92
99
  #
93
- # ignore :reek:TooManyStatements
100
+ # rubocop:disable Metrics/MethodLength
94
101
  def add_options(builder)
95
102
  builder.banner = 'usage: unparse [options] FILE [FILE]'
96
103
  builder.separator('')
@@ -103,6 +110,9 @@ module Unparser
103
110
  builder.on('-v', '--verbose') do
104
111
  @verbose = true
105
112
  end
113
+ builder.on('-l', '--literal') do
114
+ @validation = :literal_validation
115
+ end
106
116
  builder.on('--ignore FILE') do |file|
107
117
  @ignore.merge(targets(file))
108
118
  end
@@ -110,10 +120,11 @@ module Unparser
110
120
  @fail_fast = true
111
121
  end
112
122
  end
123
+ # rubocop:enable Metrics/MethodLength
113
124
 
114
125
  # Return exit status
115
126
  #
116
- # @return [Fixnum]
127
+ # @return [Integer]
117
128
  #
118
129
  # @api private
119
130
  #
@@ -128,16 +139,8 @@ module Unparser
128
139
 
129
140
  private
130
141
 
131
- # Process target
132
- #
133
- # @param [Target] target
134
- #
135
- # @return [undefined]
136
- #
137
- # @api private
138
- #
139
142
  def process_target(target)
140
- validation = target.validation
143
+ validation = target.public_send(@validation)
141
144
  if validation.success?
142
145
  puts validation.report if @verbose
143
146
  puts "Success: #{validation.identification}"
@@ -148,12 +151,6 @@ module Unparser
148
151
  end
149
152
  end
150
153
 
151
- # Return effective targets
152
- #
153
- # @return [Enumerable<Target>]
154
- #
155
- # @api private
156
- #
157
154
  def effective_targets
158
155
  if @start_with
159
156
  reject = true
@@ -169,15 +166,6 @@ module Unparser
169
166
  end.reject(&@ignore.method(:include?))
170
167
  end
171
168
 
172
- # Return targets for file name
173
- #
174
- # @param [String] file_name
175
- #
176
- # @return [Enumerable<Target>]
177
- #
178
- # @api private
179
- #
180
- # ignore :reek:UtilityFunction
181
169
  def targets(file_name)
182
170
  if File.directory?(file_name)
183
171
  Dir.glob(File.join(file_name, '**/*.rb')).sort
@@ -187,6 +175,5 @@ module Unparser
187
175
  Dir.glob(file_name).sort
188
176
  end.map { |file| Target::Path.new(Pathname.new(file)) }
189
177
  end
190
-
191
178
  end # CLI
192
179
  end # Unparser
@@ -28,9 +28,6 @@ module Unparser
28
28
 
29
29
  private
30
30
 
31
- # Initialize null color
32
- #
33
- # @return [undefined]
34
31
  def initialize; end
35
32
 
36
33
  end.new
@@ -3,8 +3,6 @@
3
3
  module Unparser
4
4
 
5
5
  # Holds the comments that remain to be emitted
6
- #
7
- # ignore :reek:RepeatedConditional
8
6
  class Comments
9
7
 
10
8
  # Proxy to singleton
@@ -113,39 +111,15 @@ module Unparser
113
111
 
114
112
  private
115
113
 
116
- # Take comments while the provided block returns true
117
- #
118
- # @yield [Parser::Source::Comment]
119
- #
120
- # @return [Array]
121
- #
122
- # @api private
123
- #
124
114
  def take_while
125
115
  number_to_take = @comments.index { |comment| !yield(comment) } || @comments.size
126
116
  @comments.shift(number_to_take)
127
117
  end
128
118
 
129
- # Take comments up to the line number
130
- #
131
- # @param [Fixnum] line
132
- #
133
- # @return [Array]
134
- #
135
- # @api private
136
- #
137
119
  def take_up_to_line(line)
138
120
  take_while { |comment| comment.location.expression.line <= line }
139
121
  end
140
122
 
141
- # Unshift document comments and return the rest
142
- #
143
- # @param [Array] comments
144
- #
145
- # @return [Array]
146
- #
147
- # @api private
148
- #
149
123
  def unshift_documents(comments)
150
124
  doc_comments, other_comments = comments.partition(&:document?)
151
125
  doc_comments.reverse_each { |comment| @comments.unshift(comment) }
@@ -2,66 +2,19 @@
2
2
 
3
3
  module Unparser
4
4
  # All unparser constants maybe included in other libraries.
5
- #
6
- # False positive since constants are frozen dynamically
7
- # to avoid duplication of `.freeze` calls
8
- #
9
- # :reek:TooManyConstants
10
5
  module Constants
11
6
 
12
- # Return frozen symbol set from enumerable
13
- #
14
- # @param [Enumerable] enumerable
15
- #
16
- # @return [Set<Symbol>]
17
- #
18
- # @api private
19
- #
20
- def self.symbol_set(enumerable)
21
- enumerable.map(&:to_sym).freeze
22
- end
23
- private_class_method :symbol_set
24
-
25
- BRACKETS_CURLY = IceNine.deep_freeze(%w[{ }])
26
- BRACKETS_ROUND = IceNine.deep_freeze(%w[( )])
27
- BRACKETS_SQUARE = IceNine.deep_freeze(%w([ ]))
28
-
29
7
  # All unary operators of the ruby language
30
- UNARY_OPERATORS = symbol_set %w[
8
+ UNARY_OPERATORS = %i[
31
9
  ! ~ -@ +@
32
- ]
10
+ ].to_set.freeze
33
11
 
34
12
  # All binary operators of the ruby language
35
- BINARY_OPERATORS = symbol_set %w[
13
+ BINARY_OPERATORS = %i[
36
14
  + - * / & | && || << >> ==
37
15
  === != <= < <=> > >= =~ !~ ^
38
16
  ** %
39
- ]
40
-
41
- COMMENT = '#'
42
-
43
- WS = ' '
44
- NL = "\n"
45
- T_DOT = '.'
46
- T_LT = '<'
47
- T_DLT = '<<'
48
- T_AMP = '&'
49
- T_ASN = '='
50
- T_SPLAT = '*'
51
- T_DSPLAT = '**'
52
- T_ASR = '=>'
53
- T_PIPE = '|'
54
- T_DCL = '::'
55
- T_NEG = '!'
56
- T_OR = '||'
57
- T_AND = '&&'
58
- T_COLON = ':'
59
-
60
- M_PO = '('
61
- M_PC = ')'
62
-
63
- SNGL_QUOTE = "'"
64
- DBL_QUOTE = '"'
17
+ ].to_set.freeze
65
18
 
66
19
  # Keywords
67
20
  K_DO = 'do'
@@ -107,8 +60,6 @@ module Unparser
107
60
  K_FILE = '__FILE__'
108
61
  K_THEN = 'then'
109
62
 
110
- DEFAULT_DELIMITER = ', '.freeze
111
-
112
63
  KEYWORDS = constants.each_with_object([]) do |name, keywords|
113
64
  value = const_get(name).freeze
114
65
  next unless name.to_s.start_with?('K_')
@@ -59,25 +59,16 @@ module Unparser
59
59
 
60
60
  private
61
61
 
62
- # Diffs between old and new
63
- #
64
- # @return [Array<Array>]
65
62
  def diffs
66
63
  ::Diff::LCS.diff(old, new)
67
64
  end
68
65
 
69
- # Raw diff-lcs hunks
70
- #
71
- # @return [Array<Diff::LCS::Hunk>]
72
66
  def hunks
73
67
  diffs.map do |diff|
74
68
  ::Diff::LCS::Hunk.new(old.map(&:dup), new, diff, max_length, 0)
75
69
  end
76
70
  end
77
71
 
78
- # Minimized hunk
79
- #
80
- # @return Diff::LCS::Hunk
81
72
  def minimized_hunk
82
73
  head, *tail = hunks
83
74
 
@@ -87,18 +78,10 @@ module Unparser
87
78
  end
88
79
  end
89
80
 
90
- # Max length of source line in new and old
91
- #
92
- # @return [Integer]
93
81
  def max_length
94
82
  [old, new].map(&:length).max
95
83
  end
96
84
 
97
- # Colorized a unified diff line
98
- #
99
- # @param [String] line
100
- #
101
- # @return [String]
102
85
  def self.colorize_line(line)
103
86
  case line[0]
104
87
  when ADDITION
@@ -6,14 +6,6 @@ module Unparser
6
6
 
7
7
  private
8
8
 
9
- # Define remaining children
10
- #
11
- # @param [Enumerable<Symbol>] names
12
- #
13
- # @return [undefined]
14
- #
15
- # @api private
16
- #
17
9
  def define_remaining_children(names)
18
10
  range = names.length..-1
19
11
  define_method(:remaining_children) do
@@ -22,15 +14,6 @@ module Unparser
22
14
  private :remaining_children
23
15
  end
24
16
 
25
- # Define named child
26
- #
27
- # @param [Symbol] name
28
- # @param [Fixnum] index
29
- #
30
- # @return [undefined]
31
- #
32
- # @api private
33
- #
34
17
  def define_child(name, index)
35
18
  define_method(name) do
36
19
  children.at(index)
@@ -38,15 +21,6 @@ module Unparser
38
21
  private name
39
22
  end
40
23
 
41
- # Define a group of children
42
- #
43
- # @param [Symbol] name
44
- # @param [Range] range
45
- #
46
- # @return [undefined]
47
- #
48
- # @api private
49
- #
50
24
  def define_group(name, range)
51
25
  define_method(name) do
52
26
  children[range]
@@ -55,12 +29,6 @@ module Unparser
55
29
  memoize(name)
56
30
  end
57
31
 
58
- # Create name helpers
59
- #
60
- # @return [undefined]
61
- #
62
- # @api private
63
- #
64
32
  def children(*names)
65
33
  define_remaining_children(names)
66
34