unparser 0.4.8 → 0.5.3

Sign up to get free protection for your applications and to get access to all the features.
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
@@ -1,38 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Unparser
4
- class Emitter
5
- class Literal
6
- # Emitter for execute strings (xstr) nodes
7
- class ExecuteString < self
8
-
9
- OPEN = CLOSE = '`'.freeze
10
-
11
- handle :xstr
12
-
13
- private
14
-
15
- # Perform dispatch
16
- #
17
- # @return [undefined]
18
- #
19
- # @api private
20
- #
21
- def dispatch
22
- visit_parentheses(dynamic_body, OPEN, CLOSE)
23
- end
24
-
25
- # Return dynamic body
26
- #
27
- # @return [Parser::AST::Node]
28
- #
29
- # @api private
30
- #
31
- def dynamic_body
32
- Parser::AST::Node.new(:dyn_xstr_body, children)
33
- end
34
-
35
- end # ExecuteString
36
- end # Literal
37
- end # Emitter
38
- end # Unparser
@@ -1,156 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Unparser
4
- class Emitter
5
- class Literal
6
-
7
- # Abstract namespace class for hash pair emitters
8
- class HashPair < self
9
-
10
- children :key, :value
11
-
12
- private
13
-
14
- # Emit value
15
- #
16
- # @return [undefined]
17
- #
18
- # @api private
19
- #
20
- def emit_value
21
- value_type = value.type
22
- conditional_parentheses(value_type.equal?(:if)) do
23
- visit(value)
24
- end
25
- end
26
-
27
- # Pair emitter that emits hash-rocket separated key values
28
- class Rocket < self
29
- HASHROCKET = ' => '.freeze
30
-
31
- handle :pair_rocket, :pair
32
-
33
- private
34
-
35
- # Perform dispatch
36
- #
37
- # @return [undefined]
38
- #
39
- # @api private
40
- #
41
- def dispatch
42
- visit(key)
43
- write(HASHROCKET)
44
- emit_value
45
- end
46
-
47
- end # Rocket
48
-
49
- # Pair emitter that emits colon separated key values
50
- class Colon < self
51
- COLON = ': '.freeze
52
-
53
- handle :pair_colon
54
-
55
- private
56
-
57
- # Perform dispatch
58
- #
59
- # @return [undefined]
60
- #
61
- # @api private
62
- #
63
- def dispatch
64
- write(key.children.first.to_s, COLON)
65
- emit_value
66
- end
67
-
68
- end # Colon
69
-
70
- end # HashPair
71
-
72
- # Emitter for hash bodies
73
- class HashBody < self
74
-
75
- BAREWORD = /\A[A-Za-z_][A-Za-z_0-9]*[?!]?\z/.freeze
76
-
77
- handle :hash_body
78
-
79
- private
80
-
81
- # Perform dispatch
82
- #
83
- # @return [undefined]
84
- #
85
- # @api private
86
- #
87
- def dispatch
88
- delimited(effective_body)
89
- end
90
-
91
- # Return effective body
92
- #
93
- # @return [Enumerable<Parser::AST::Node>]
94
- #
95
- # @api private
96
- #
97
- def effective_body
98
- children.map do |pair|
99
- if pair.type.equal?(:kwsplat)
100
- pair
101
- else
102
- key, _value = *pair
103
- if key.type.equal?(:sym) && key.children.first.to_s =~ BAREWORD
104
- n(:pair_colon, pair.children)
105
- else
106
- n(:pair_rocket, pair.children)
107
- end
108
- end
109
- end
110
- end
111
-
112
- end # HashBody
113
-
114
- # Emitter for Hash literals
115
- class Hash < self
116
-
117
- DELIMITER = ', '.freeze
118
- OPEN = '{'.freeze
119
- CLOSE = '}'.freeze
120
-
121
- handle :hash
122
-
123
- private
124
-
125
- # Perform dispatch
126
- #
127
- # @return [undefined]
128
- #
129
- # @api private
130
- #
131
- def dispatch
132
- if children.empty?
133
- write(OPEN, CLOSE)
134
- else
135
- emit_hash_body
136
- end
137
- end
138
-
139
- # Emit hash body
140
- #
141
- # @return [undefined]
142
- #
143
- # @api private
144
- #
145
- def emit_hash_body
146
- parentheses(OPEN, CLOSE) do
147
- write(WS)
148
- run(HashBody)
149
- write(WS)
150
- end
151
- end
152
-
153
- end # Hash
154
- end # Literal
155
- end # Emitter
156
- end # Unparser
@@ -1,145 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Unparser
4
- class Emitter
5
- class Literal
6
-
7
- # Base class for primitive emitters
8
- class Primitive < self
9
-
10
- children :value
11
-
12
- # Emitter for primitives based on Object#inspect
13
- class Inspect < self
14
-
15
- handle :sym, :str
16
-
17
- private
18
-
19
- # Dispatch value
20
- #
21
- # @return [undefined]
22
- #
23
- # @api private
24
- #
25
- def dispatch
26
- write(value.inspect)
27
- end
28
-
29
- end # Inspect
30
-
31
- # Emitter for complex literals
32
- class Complex < self
33
-
34
- handle :complex
35
-
36
- RATIONAL_FORMAT = 'i'.freeze
37
-
38
- MAP =
39
- if 0.class.equal?(Integer)
40
- IceNine.deep_freeze(
41
- Float => :float,
42
- Rational => :rational,
43
- Integer => :int
44
- )
45
- else
46
- IceNine.deep_freeze(
47
- Float => :float,
48
- Rational => :rational,
49
- Fixnum => :int,
50
- Bignum => :int
51
- )
52
- end
53
-
54
- private
55
-
56
- # Dispatch value
57
- #
58
- # @return [undefined]
59
- #
60
- def dispatch
61
- emit_imaginary
62
- write(RATIONAL_FORMAT)
63
- end
64
-
65
- # Emit imaginary component
66
- #
67
- # @return [undefined]
68
- #
69
- # @api private
70
- #
71
- def emit_imaginary
72
- visit(imaginary_node)
73
- end
74
-
75
- # Return imaginary node
76
- #
77
- # @return [Parser::AST::Node]
78
- #
79
- # @api private
80
- #
81
- def imaginary_node
82
- imaginary = value.imaginary
83
- s(MAP.fetch(imaginary.class), imaginary)
84
- end
85
-
86
- end # Rational
87
-
88
- # Emitter for rational literals
89
- class Rational < self
90
-
91
- handle :rational
92
-
93
- RATIONAL_FORMAT = 'r'.freeze
94
-
95
- private
96
-
97
- # Dispatch value
98
- #
99
- # @return [undefined]
100
- #
101
- def dispatch
102
- integer = value.to_i
103
- float = value.to_f
104
-
105
- write_rational(integer.to_f.eql?(float) ? integer : float)
106
- end
107
-
108
- # Write rational format
109
- #
110
- # @param [#to_s] value
111
- #
112
- # @return [undefined]
113
- #
114
- # @api private
115
- #
116
- def write_rational(value)
117
- write(value.to_s, RATIONAL_FORMAT)
118
- end
119
-
120
- end # Rational
121
-
122
- # Emiter for numeric literals
123
- class Numeric < self
124
-
125
- handle :int, :float
126
-
127
- private
128
-
129
- # Dispatch value
130
- #
131
- # @return [undefined]
132
- #
133
- # @api private
134
- #
135
- def dispatch
136
- conditional_parentheses(parent.is_a?(Send) && value.negative?) do
137
- write(value.inspect)
138
- end
139
- end
140
-
141
- end # Numeric
142
- end # Primitive
143
- end # Literal
144
- end # Emitter
145
- end # Unparser
@@ -1,36 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Unparser
4
- class Emitter
5
- class Literal
6
- # Abstract base class for literal range emitter
7
- class Range < self
8
- include Unterminated
9
-
10
- TOKENS = IceNine.deep_freeze(
11
- irange: '..',
12
- erange: '...'
13
- )
14
-
15
- handle(*TOKENS.keys)
16
-
17
- children :begin_node, :end_node
18
-
19
- private
20
-
21
- # Perform dispatch
22
- #
23
- # @return [undefined]
24
- #
25
- # @api private
26
- #
27
- def dispatch
28
- visit(begin_node)
29
- write(TOKENS.fetch(node.type))
30
- visit(end_node) if end_node
31
- end
32
-
33
- end # Range
34
- end # Literal
35
- end # Emitter
36
- end # Unparser
@@ -1,114 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Unparser
4
- class Emitter
5
- class Literal
6
-
7
- # Emitter for regexp literals
8
- class Regexp < self
9
- DELIMITER = '/'.freeze
10
- ESCAPED_DELIMITER = '\/'.freeze
11
-
12
- handle :regexp
13
-
14
- private
15
-
16
- # Perform dispatch
17
- #
18
- # @return [undefined]
19
- #
20
- # @api private
21
- #
22
- def dispatch
23
- parentheses(DELIMITER, DELIMITER) do
24
- body.each(&method(:write_body))
25
- end
26
- visit(children.last)
27
- end
28
-
29
- # Return non regopt children
30
- #
31
- # @return [Array<Parser::AST::Node>]
32
- #
33
- # @api private
34
- #
35
- def body
36
- children[0..-2]
37
- end
38
-
39
- # Write specific body component
40
- #
41
- # @param [Parser::AST::Node] node
42
- #
43
- # @return [undefined]
44
- #
45
- # @api private
46
- #
47
- def write_body(node)
48
- case node.type
49
- when :str
50
- buffer.append_without_prefix(escape(node).children.first)
51
- else
52
- visit(s(:interpolated, node))
53
- end
54
- end
55
-
56
- # Return dynamic body
57
- #
58
- # @return [undefined]
59
- #
60
- # @api private
61
- #
62
- def dynamic_body
63
- Parser::AST::Node.new(:dyn_regexp_body, dynamic_body_children)
64
- end
65
-
66
- # Return dynamic body children
67
- #
68
- # @return [Enumerable<Parser::AST::Node>]
69
- #
70
- # @api private
71
- #
72
- def dynamic_body_children
73
- children[0..-2].map do |child|
74
- escape(child)
75
- end
76
- end
77
-
78
- # Return escaped child
79
- #
80
- # @param [Parser::AST::Node] child
81
- #
82
- # @return [Parser::AST::Node]
83
- #
84
- # @api private
85
- #
86
- def escape(child)
87
- source = child.children.first
88
- s(:str, source.gsub(DELIMITER, ESCAPED_DELIMITER))
89
- end
90
-
91
- end # Regexp
92
-
93
- # Emitter for regexp options
94
- class Regopt < self
95
-
96
- handle :regopt
97
-
98
- private
99
-
100
- # Perform dispatch
101
- #
102
- # @return [undefined]
103
- #
104
- # @api private
105
- #
106
- def dispatch
107
- write(children.map(&:to_s).join)
108
- end
109
-
110
- end # Regopt
111
-
112
- end # Literal
113
- end # Emitter
114
- end # Unparser