unparser 0.5.2 → 0.5.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +19 -4
- data/lib/unparser.rb +3 -11
- data/lib/unparser/emitter/flow_modifier.rb +11 -2
- data/lib/unparser/emitter/hash.rb +1 -31
- data/lib/unparser/emitter/if.rb +8 -0
- data/lib/unparser/emitter/in_pattern.rb +2 -0
- data/lib/unparser/emitter/kwargs.rb +13 -0
- data/lib/unparser/emitter/match_pattern.rb +22 -0
- data/lib/unparser/emitter/pair.rb +33 -0
- data/lib/unparser/emitter/splat.rb +11 -1
- data/lib/unparser/generation.rb +3 -1
- data/lib/unparser/node_helpers.rb +1 -0
- data/lib/unparser/writer/dynamic_string.rb +3 -11
- data/lib/unparser/writer/rescue.rb +4 -0
- data/lib/unparser/writer/send.rb +1 -14
- metadata +23 -17
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 32c3cddc21f0047b0a97da52b3e897bc58a07128322cf3ebf9191e37a775a694
|
4
|
+
data.tar.gz: 40c3e5f35f04aa0f53450aa3753d056d41fd0057c3deafbe21b6d2a822347e2f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 05dec75daa6a074277a407862b37f1e1ded8989e8e1978b468363f4e413015f158311620dbfb8547ed1b179ad6a81a161620509ec0e1e26772115c0f07e3921d
|
7
|
+
data.tar.gz: 6794a56773e8ee22f2868d23ca48969d73c75e0c877ddeb001511261e919ddcb1e3026de91b816703209445d359fb41e95e338625fe1a8f3362dd06e772ec1bb
|
data/README.md
CHANGED
@@ -4,7 +4,7 @@ unparser
|
|
4
4
|
![CI](https://github.com/mbj/unparser/workflows/CI/badge.svg)
|
5
5
|
[![Gem Version](https://img.shields.io/gem/v/unparser.svg)](https://rubygems.org/gems/unparser)
|
6
6
|
|
7
|
-
Generate equivalent source for ASTs from
|
7
|
+
Generate equivalent source for ASTs from [parser](https://github.com/whitequark/parser).
|
8
8
|
|
9
9
|
The following constraints apply:
|
10
10
|
|
@@ -12,8 +12,13 @@ The following constraints apply:
|
|
12
12
|
* Only support for the [modern AST](https://github.com/whitequark/parser/#usage) format
|
13
13
|
* Only support for Ruby >= 2.5
|
14
14
|
|
15
|
-
|
16
|
-
|
15
|
+
Notable Users:
|
16
|
+
|
17
|
+
* [mutant](https://github.com/mbj/mutant) - Code review engine via mutation testing.
|
18
|
+
* [ruby-next](https://github.com/ruby-next/ruby-next) - Ruby Syntax Backports.
|
19
|
+
* May other [reverse-dependencies](https://rubygems.org/gems/unparser/reverse_dependencies).
|
20
|
+
|
21
|
+
(if you want your tool to be mentioned here please PR the addition with a TLDR of your use case).
|
17
22
|
|
18
23
|
Public API:
|
19
24
|
-----------
|
@@ -86,12 +91,22 @@ RUBY
|
|
86
91
|
|
87
92
|
generated = Unparser.unparse(node) # ["foo", "bar"], NOT %w[foo bar] !
|
88
93
|
|
89
|
-
code == generated
|
94
|
+
code == generated # false, not identical code
|
90
95
|
Unparser.parse(generated) == node # true, but identical AST
|
91
96
|
```
|
92
97
|
|
93
98
|
Summary: unparser does not reproduce your source! It produces equivalent source.
|
94
99
|
|
100
|
+
Ruby Versions:
|
101
|
+
--------------
|
102
|
+
|
103
|
+
Unparsers primay reason for existance is mutant and its
|
104
|
+
supported [Ruby-Versions](https://github.com/mbj/mutant#ruby-versions).
|
105
|
+
|
106
|
+
Basically: All non EOL MRI releases.
|
107
|
+
|
108
|
+
If you need to generate Ruby Syntax outside of this band feel free to contact me (email in gemspec).
|
109
|
+
|
95
110
|
Testing:
|
96
111
|
--------
|
97
112
|
|
data/lib/unparser.rb
CHANGED
@@ -117,21 +117,10 @@ module Unparser
|
|
117
117
|
Parser::CurrentRuby.new(Builder.new).tap do |parser|
|
118
118
|
parser.diagnostics.tap do |diagnostics|
|
119
119
|
diagnostics.all_errors_are_fatal = true
|
120
|
-
diagnostics.consumer = method(:consume_diagnostic)
|
121
120
|
end
|
122
121
|
end
|
123
122
|
end
|
124
123
|
|
125
|
-
# Consume diagnostic
|
126
|
-
#
|
127
|
-
# @param [Parser::Diagnostic] diagnostic
|
128
|
-
#
|
129
|
-
# @return [undefined]
|
130
|
-
def self.consume_diagnostic(diagnostic)
|
131
|
-
Kernel.warn(diagnostic.render)
|
132
|
-
end
|
133
|
-
private_class_method :consume_diagnostic
|
134
|
-
|
135
124
|
# Construct a parser buffer from string
|
136
125
|
#
|
137
126
|
# @param [String] source
|
@@ -209,6 +198,9 @@ require 'unparser/emitter/undef'
|
|
209
198
|
require 'unparser/emitter/variable'
|
210
199
|
require 'unparser/emitter/xstr'
|
211
200
|
require 'unparser/emitter/yield'
|
201
|
+
require 'unparser/emitter/kwargs'
|
202
|
+
require 'unparser/emitter/pair'
|
203
|
+
require 'unparser/emitter/match_pattern'
|
212
204
|
require 'unparser/writer'
|
213
205
|
require 'unparser/writer/binary'
|
214
206
|
require 'unparser/writer/dynamic_string'
|
@@ -14,13 +14,22 @@ module Unparser
|
|
14
14
|
|
15
15
|
handle(*MAP.keys)
|
16
16
|
|
17
|
+
def emit_heredoc_reminders
|
18
|
+
children.each do |node|
|
19
|
+
emitter(node).emit_heredoc_reminders
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
17
23
|
private
|
18
24
|
|
19
25
|
def dispatch
|
20
26
|
write(MAP.fetch(node.type))
|
21
27
|
|
22
|
-
|
23
|
-
|
28
|
+
if children.one? && n_if?(children.first)
|
29
|
+
ws
|
30
|
+
emitter(children.first).emit_ternary
|
31
|
+
else
|
32
|
+
emit_arguments unless children.empty?
|
24
33
|
end
|
25
34
|
end
|
26
35
|
|
@@ -4,10 +4,6 @@ module Unparser
|
|
4
4
|
class Emitter
|
5
5
|
# Emitter for Hash literals
|
6
6
|
class Hash < self
|
7
|
-
BAREWORD = /\A[A-Za-z_][A-Za-z_0-9]*[?!]?\z/.freeze
|
8
|
-
|
9
|
-
private_constant(*constants(false))
|
10
|
-
|
11
7
|
handle :hash
|
12
8
|
|
13
9
|
def emit_last_argument_hash
|
@@ -41,34 +37,8 @@ module Unparser
|
|
41
37
|
end
|
42
38
|
|
43
39
|
def emit_hash_body
|
44
|
-
delimited(children
|
45
|
-
end
|
46
|
-
|
47
|
-
def emit_hash_member(node)
|
48
|
-
if n_kwsplat?(node)
|
49
|
-
visit(node)
|
50
|
-
else
|
51
|
-
emit_pair(node)
|
52
|
-
end
|
40
|
+
delimited(children)
|
53
41
|
end
|
54
|
-
|
55
|
-
def emit_pair(pair)
|
56
|
-
key, value = *pair.children
|
57
|
-
|
58
|
-
if colon?(key)
|
59
|
-
write(key.children.first.to_s, ': ')
|
60
|
-
else
|
61
|
-
visit(key)
|
62
|
-
write(' => ')
|
63
|
-
end
|
64
|
-
|
65
|
-
visit(value)
|
66
|
-
end
|
67
|
-
|
68
|
-
def colon?(key)
|
69
|
-
n_sym?(key) && BAREWORD.match?(key.children.first)
|
70
|
-
end
|
71
|
-
|
72
42
|
end # Hash
|
73
43
|
end # Emitter
|
74
44
|
end # Unparser
|
data/lib/unparser/emitter/if.rb
CHANGED
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Unparser
|
4
|
+
class Emitter
|
5
|
+
# Emitter for in pattern nodes
|
6
|
+
class MatchPattern < self
|
7
|
+
|
8
|
+
handle :match_pattern
|
9
|
+
handle :match_pattern_p
|
10
|
+
|
11
|
+
children :target, :pattern
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def dispatch
|
16
|
+
visit(target)
|
17
|
+
write(' in ')
|
18
|
+
visit(pattern)
|
19
|
+
end
|
20
|
+
end # InPattern
|
21
|
+
end # Emitter
|
22
|
+
end # Unparser
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Unparser
|
4
|
+
class Emitter
|
5
|
+
# Emitter for key value pairs in hash literals or kwargs
|
6
|
+
class Pair < self
|
7
|
+
BAREWORD = /\A[A-Za-z_][A-Za-z_0-9]*[?!]?\z/.freeze
|
8
|
+
|
9
|
+
private_constant(*constants(false))
|
10
|
+
|
11
|
+
handle :pair
|
12
|
+
|
13
|
+
children :key, :value
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def dispatch
|
18
|
+
if colon?(key)
|
19
|
+
write(key.children.first.to_s, ': ')
|
20
|
+
else
|
21
|
+
visit(key)
|
22
|
+
write(' => ')
|
23
|
+
end
|
24
|
+
|
25
|
+
visit(value)
|
26
|
+
end
|
27
|
+
|
28
|
+
def colon?(key)
|
29
|
+
n_sym?(key) && BAREWORD.match?(key.children.first)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -22,12 +22,22 @@ module Unparser
|
|
22
22
|
|
23
23
|
children :subject
|
24
24
|
|
25
|
+
def emit_mlhs
|
26
|
+
write('*')
|
27
|
+
subject_emitter.emit_mlhs if subject
|
28
|
+
end
|
29
|
+
|
25
30
|
private
|
26
31
|
|
27
32
|
def dispatch
|
28
33
|
write('*')
|
29
|
-
|
34
|
+
subject_emitter.write_to_buffer
|
35
|
+
end
|
36
|
+
|
37
|
+
def subject_emitter
|
38
|
+
emitter(subject)
|
30
39
|
end
|
40
|
+
memoize :subject_emitter
|
31
41
|
end
|
32
42
|
end
|
33
43
|
end # Unparser
|
data/lib/unparser/generation.rb
CHANGED
@@ -204,7 +204,9 @@ module Unparser
|
|
204
204
|
end
|
205
205
|
|
206
206
|
def emit_rescue_postcontrol(node)
|
207
|
-
writer_with(Writer::Rescue, node)
|
207
|
+
writer = writer_with(Writer::Rescue, node)
|
208
|
+
writer.emit_postcontrol
|
209
|
+
writer.emit_heredoc_reminders
|
208
210
|
end
|
209
211
|
|
210
212
|
def emit_rescue_regular(node)
|
@@ -20,7 +20,7 @@ module Unparser
|
|
20
20
|
]
|
21
21
|
)
|
22
22
|
|
23
|
-
FLAT_INTERPOLATION = %i[ivar cvar gvar].to_set.freeze
|
23
|
+
FLAT_INTERPOLATION = %i[ivar cvar gvar nth_ref].to_set.freeze
|
24
24
|
|
25
25
|
private_constant(*constants(false))
|
26
26
|
|
@@ -42,7 +42,7 @@ module Unparser
|
|
42
42
|
private
|
43
43
|
|
44
44
|
def heredoc_header
|
45
|
-
|
45
|
+
'<<-HEREDOC'
|
46
46
|
end
|
47
47
|
|
48
48
|
def heredoc?
|
@@ -55,11 +55,7 @@ module Unparser
|
|
55
55
|
|
56
56
|
def emit_heredoc_body
|
57
57
|
nl
|
58
|
-
|
59
|
-
emit_squiggly_heredoc_body
|
60
|
-
else
|
61
|
-
emit_normal_heredoc_body
|
62
|
-
end
|
58
|
+
emit_normal_heredoc_body
|
63
59
|
end
|
64
60
|
|
65
61
|
def emit_heredoc_footer
|
@@ -119,10 +115,6 @@ module Unparser
|
|
119
115
|
n_str?(last) && last.children.first[-1].eql?("\n")
|
120
116
|
end
|
121
117
|
|
122
|
-
def need_squiggly?
|
123
|
-
children.any?(s(:str, ''))
|
124
|
-
end
|
125
|
-
|
126
118
|
def emit_squiggly_heredoc_body
|
127
119
|
buffer.indent
|
128
120
|
children.each do |child|
|
data/lib/unparser/writer/send.rb
CHANGED
@@ -75,20 +75,7 @@ module Unparser
|
|
75
75
|
end
|
76
76
|
|
77
77
|
def emit_normal_arguments
|
78
|
-
parentheses
|
79
|
-
arguments.each_with_index do |node, index|
|
80
|
-
write(', ') unless index.zero?
|
81
|
-
emit_argument(node, index.succ.equal?(arguments.length))
|
82
|
-
end
|
83
|
-
end
|
84
|
-
end
|
85
|
-
|
86
|
-
def emit_argument(argument, last)
|
87
|
-
if n_hash?(argument) && last
|
88
|
-
writer_with(Emitter::Hash, argument).emit_last_argument_hash
|
89
|
-
else
|
90
|
-
visit(argument)
|
91
|
-
end
|
78
|
+
parentheses { delimited(arguments) }
|
92
79
|
end
|
93
80
|
|
94
81
|
def emit_heredoc_reminder(argument)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: unparser
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Markus Schirp
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-01-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: abstract_type
|
@@ -114,14 +114,14 @@ dependencies:
|
|
114
114
|
requirements:
|
115
115
|
- - ">="
|
116
116
|
- !ruby/object:Gem::Version
|
117
|
-
version:
|
117
|
+
version: 3.0.0
|
118
118
|
type: :runtime
|
119
119
|
prerelease: false
|
120
120
|
version_requirements: !ruby/object:Gem::Requirement
|
121
121
|
requirements:
|
122
122
|
- - ">="
|
123
123
|
- !ruby/object:Gem::Version
|
124
|
-
version:
|
124
|
+
version: 3.0.0
|
125
125
|
- !ruby/object:Gem::Dependency
|
126
126
|
name: procto
|
127
127
|
requirement: !ruby/object:Gem::Requirement
|
@@ -142,28 +142,28 @@ dependencies:
|
|
142
142
|
requirements:
|
143
143
|
- - "~>"
|
144
144
|
- !ruby/object:Gem::Version
|
145
|
-
version: 0.
|
145
|
+
version: 0.10.22
|
146
146
|
type: :development
|
147
147
|
prerelease: false
|
148
148
|
version_requirements: !ruby/object:Gem::Requirement
|
149
149
|
requirements:
|
150
150
|
- - "~>"
|
151
151
|
- !ruby/object:Gem::Version
|
152
|
-
version: 0.
|
152
|
+
version: 0.10.22
|
153
153
|
- !ruby/object:Gem::Dependency
|
154
154
|
name: mutant-rspec
|
155
155
|
requirement: !ruby/object:Gem::Requirement
|
156
156
|
requirements:
|
157
157
|
- - "~>"
|
158
158
|
- !ruby/object:Gem::Version
|
159
|
-
version: 0.
|
159
|
+
version: 0.10.22
|
160
160
|
type: :development
|
161
161
|
prerelease: false
|
162
162
|
version_requirements: !ruby/object:Gem::Requirement
|
163
163
|
requirements:
|
164
164
|
- - "~>"
|
165
165
|
- !ruby/object:Gem::Version
|
166
|
-
version: 0.
|
166
|
+
version: 0.10.22
|
167
167
|
- !ruby/object:Gem::Dependency
|
168
168
|
name: rspec
|
169
169
|
requirement: !ruby/object:Gem::Requirement
|
@@ -198,42 +198,42 @@ dependencies:
|
|
198
198
|
requirements:
|
199
199
|
- - "~>"
|
200
200
|
- !ruby/object:Gem::Version
|
201
|
-
version: 1.
|
201
|
+
version: 1.3.0
|
202
202
|
type: :development
|
203
203
|
prerelease: false
|
204
204
|
version_requirements: !ruby/object:Gem::Requirement
|
205
205
|
requirements:
|
206
206
|
- - "~>"
|
207
207
|
- !ruby/object:Gem::Version
|
208
|
-
version: 1.
|
208
|
+
version: 1.3.0
|
209
209
|
- !ruby/object:Gem::Dependency
|
210
210
|
name: rubocop
|
211
211
|
requirement: !ruby/object:Gem::Requirement
|
212
212
|
requirements:
|
213
213
|
- - "~>"
|
214
214
|
- !ruby/object:Gem::Version
|
215
|
-
version:
|
215
|
+
version: '1.7'
|
216
216
|
type: :development
|
217
217
|
prerelease: false
|
218
218
|
version_requirements: !ruby/object:Gem::Requirement
|
219
219
|
requirements:
|
220
220
|
- - "~>"
|
221
221
|
- !ruby/object:Gem::Version
|
222
|
-
version:
|
222
|
+
version: '1.7'
|
223
223
|
- !ruby/object:Gem::Dependency
|
224
224
|
name: rubocop-packaging
|
225
225
|
requirement: !ruby/object:Gem::Requirement
|
226
226
|
requirements:
|
227
227
|
- - "~>"
|
228
228
|
- !ruby/object:Gem::Version
|
229
|
-
version: 0.5
|
229
|
+
version: '0.5'
|
230
230
|
type: :development
|
231
231
|
prerelease: false
|
232
232
|
version_requirements: !ruby/object:Gem::Requirement
|
233
233
|
requirements:
|
234
234
|
- - "~>"
|
235
235
|
- !ruby/object:Gem::Version
|
236
|
-
version: 0.5
|
236
|
+
version: '0.5'
|
237
237
|
description: Generate equivalent source for parser gem AST nodes
|
238
238
|
email: mbj@schirp-dso.com
|
239
239
|
executables:
|
@@ -285,17 +285,20 @@ files:
|
|
285
285
|
- lib/unparser/emitter/in_match.rb
|
286
286
|
- lib/unparser/emitter/in_pattern.rb
|
287
287
|
- lib/unparser/emitter/index.rb
|
288
|
+
- lib/unparser/emitter/kwargs.rb
|
288
289
|
- lib/unparser/emitter/kwbegin.rb
|
289
290
|
- lib/unparser/emitter/lambda.rb
|
290
291
|
- lib/unparser/emitter/masgn.rb
|
291
292
|
- lib/unparser/emitter/match.rb
|
292
293
|
- lib/unparser/emitter/match_alt.rb
|
293
294
|
- lib/unparser/emitter/match_as.rb
|
295
|
+
- lib/unparser/emitter/match_pattern.rb
|
294
296
|
- lib/unparser/emitter/match_rest.rb
|
295
297
|
- lib/unparser/emitter/match_var.rb
|
296
298
|
- lib/unparser/emitter/mlhs.rb
|
297
299
|
- lib/unparser/emitter/module.rb
|
298
300
|
- lib/unparser/emitter/op_assign.rb
|
301
|
+
- lib/unparser/emitter/pair.rb
|
299
302
|
- lib/unparser/emitter/pin.rb
|
300
303
|
- lib/unparser/emitter/primitive.rb
|
301
304
|
- lib/unparser/emitter/range.rb
|
@@ -331,7 +334,10 @@ files:
|
|
331
334
|
homepage: http://github.com/mbj/unparser
|
332
335
|
licenses:
|
333
336
|
- MIT
|
334
|
-
metadata:
|
337
|
+
metadata:
|
338
|
+
bug_tracker_uri: https://github.com/mbj/unparser/issues
|
339
|
+
changelog_uri: https://github.com/mbj/unparser/blob/master/Changelog.md
|
340
|
+
funding_uri: https://github.com/sponsors/mbj
|
335
341
|
post_install_message:
|
336
342
|
rdoc_options: []
|
337
343
|
require_paths:
|
@@ -340,14 +346,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
340
346
|
requirements:
|
341
347
|
- - ">="
|
342
348
|
- !ruby/object:Gem::Version
|
343
|
-
version: '
|
349
|
+
version: '2.5'
|
344
350
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
345
351
|
requirements:
|
346
352
|
- - ">="
|
347
353
|
- !ruby/object:Gem::Version
|
348
354
|
version: '0'
|
349
355
|
requirements: []
|
350
|
-
rubygems_version: 3.
|
356
|
+
rubygems_version: 3.2.3
|
351
357
|
signing_key:
|
352
358
|
specification_version: 4
|
353
359
|
summary: Generate equivalent source for parser gem AST nodes
|