unparser 0.5.0 → 0.5.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c326e601dd3bcfdc7363e456e07d1b15ef323ae095e39026e094205de5206119
4
- data.tar.gz: a839f69163fab291b893a7e0702771848fc4632161414dff4b4528ad12568573
3
+ metadata.gz: 824d898a010fcf41a3a7d243d3f6c30dc551751fccc8a7d592e73fe6685dab8c
4
+ data.tar.gz: ef5e5fdc7df12831465d4ed15d35da615ff10eafb342d2a32495c42d88c7b415
5
5
  SHA512:
6
- metadata.gz: b6821013d903d32988111efbea734cc1daaeb02d5fc4b3073c215365f365fd86d52a40c4b495175610d30c30a467670ea2c37357acbb8191623940673119cfae
7
- data.tar.gz: 924afece12fe65ab3c4783420ca6ce200db08249830eb0a0cdd16b69ddb05f7377dc0900e832abd0957c97b0d4fe259eaa0c320c67e714d9daeed033575bd235
6
+ metadata.gz: c902fd66c8d5195ba76c3f53d028112e41e4bff18e62d851e207949ea332d5c1e2a352a2eb478b626a6fee2c4d908303c595c94364c03539bb4aa8a72d53b64d
7
+ data.tar.gz: 647e6ac18916a4e2454cd1c018932da6c464598ac548d84da242d6b8d8b19c06a5ee430d9fa35338e3576bb860ac3acbde3814e6d891259db1506b9004c612cc
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 whitequarks [parser](https://github.com/whitequark/parser).
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
- It serves well for [mutant](https://github.com/mbj/mutant) mutators and the in-memory vendoring for self hosting,
16
- and other tooling.
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 # false, not identical code
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
 
@@ -50,6 +50,23 @@ module Unparser
50
50
  end.content
51
51
  end
52
52
 
53
+ # Unparse with validation
54
+ #
55
+ # @param [Parser::AST::Node, nil] node
56
+ # @param [Array] comment_array
57
+ #
58
+ # @return [Either<Validation,String>]
59
+ def self.unparse_validate(node, comment_array = [])
60
+ generated = unparse(node, comment_array)
61
+ validation = Validation.from_string(generated)
62
+
63
+ if validation.success?
64
+ MPrelude::Either::Right.new(generated)
65
+ else
66
+ MPrelude::Either::Left.new(validation)
67
+ end
68
+ end
69
+
53
70
  # Unparse capturing errors
54
71
  #
55
72
  # This is mostly useful for writing testing tools against unparser.
@@ -19,8 +19,11 @@ module Unparser
19
19
  def dispatch
20
20
  write(MAP.fetch(node.type))
21
21
 
22
- unless children.empty?
23
- emit_arguments
22
+ if children.one? && n_if?(children.first)
23
+ ws
24
+ emitter(children.first).emit_ternary
25
+ else
26
+ emit_arguments unless children.empty?
24
27
  end
25
28
  end
26
29
 
@@ -8,6 +8,14 @@ module Unparser
8
8
 
9
9
  children :condition, :if_branch, :else_branch
10
10
 
11
+ def emit_ternary
12
+ visit(condition)
13
+ write(' ? ')
14
+ visit(if_branch)
15
+ write(' : ')
16
+ visit(else_branch)
17
+ end
18
+
11
19
  private
12
20
 
13
21
  def dispatch
@@ -27,6 +27,8 @@ module Unparser
27
27
  ws
28
28
  write('then')
29
29
  emit_body(branch)
30
+ else
31
+ nl
30
32
  end
31
33
  end
32
34
  end # InPattern
@@ -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
- visit(subject) if subject
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
@@ -204,7 +204,9 @@ module Unparser
204
204
  end
205
205
 
206
206
  def emit_rescue_postcontrol(node)
207
- writer_with(Writer::Rescue, node).emit_postcontrol
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)
@@ -45,6 +45,7 @@ module Unparser
45
45
  ensure
46
46
  hash
47
47
  hash_pattern
48
+ if
48
49
  in_pattern
49
50
  int
50
51
  kwsplat
@@ -71,6 +71,27 @@ module Unparser
71
71
  )
72
72
  end
73
73
 
74
+ # Create validator from node
75
+ #
76
+ # @param [Parser::AST::Node] original_node
77
+ #
78
+ # @return [Validator]
79
+ def self.from_node(original_node)
80
+ generated_source = Unparser.unparse_either(original_node)
81
+
82
+ generated_node = generated_source
83
+ .lmap(&method(:const_unit))
84
+ .bind(&Unparser.public_method(:parse_either))
85
+
86
+ new(
87
+ identification: '(string)',
88
+ original_source: generated_source,
89
+ original_node: MPrelude::Either::Right.new(original_node),
90
+ generated_source: generated_source,
91
+ generated_node: generated_node
92
+ )
93
+ end
94
+
74
95
  # Create validator from file
75
96
  #
76
97
  # @param [Pathname] path
@@ -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
 
@@ -46,7 +46,7 @@ module Unparser
46
46
  end
47
47
 
48
48
  def heredoc?
49
- children.empty? || (nl_last_child? && heredoc_pattern?)
49
+ !children.empty? && (nl_last_child? && heredoc_pattern?)
50
50
  end
51
51
 
52
52
  def emit_heredoc_header
@@ -169,8 +169,12 @@ module Unparser
169
169
  end
170
170
 
171
171
  def emit_dstr
172
- segments.each_with_index do |children, index|
173
- emit_segment(children, index)
172
+ if children.empty?
173
+ write('%()')
174
+ else
175
+ segments.each_with_index do |children, index|
176
+ emit_segment(children, index)
177
+ end
174
178
  end
175
179
  end
176
180
 
@@ -20,6 +20,10 @@ module Unparser
20
20
  end
21
21
  end
22
22
 
23
+ def emit_heredoc_reminders
24
+ emitter(body).emit_heredoc_reminders
25
+ end
26
+
23
27
  def emit_postcontrol
24
28
  visit(body)
25
29
  writer_with(Resbody, rescue_body).emit_postcontrol
@@ -45,7 +45,7 @@ module Unparser
45
45
  def effective_writer_class
46
46
  if details.binary_syntax_allowed?
47
47
  Binary
48
- elsif details.selector_unary_operator?
48
+ elsif details.selector_unary_operator? && n_send?(node)
49
49
  Unary
50
50
  elsif write_as_attribute_assignment?
51
51
  AttributeAssignment
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.0
4
+ version: 0.5.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Markus Schirp
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-10-09 00:00:00.000000000 Z
11
+ date: 2020-12-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: abstract_type
@@ -142,28 +142,28 @@ dependencies:
142
142
  requirements:
143
143
  - - "~>"
144
144
  - !ruby/object:Gem::Version
145
- version: 0.9.13
145
+ version: 0.10.19
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.9.13
152
+ version: 0.10.19
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.9.13
159
+ version: 0.10.19
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.9.13
166
+ version: 0.10.19
167
167
  - !ruby/object:Gem::Dependency
168
168
  name: rspec
169
169
  requirement: !ruby/object:Gem::Requirement
@@ -198,28 +198,28 @@ dependencies:
198
198
  requirements:
199
199
  - - "~>"
200
200
  - !ruby/object:Gem::Version
201
- version: 1.2.0
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.2.0
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: 0.92.0
215
+ version: 1.6.1
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: 0.92.0
222
+ version: 1.6.1
223
223
  - !ruby/object:Gem::Dependency
224
224
  name: rubocop-packaging
225
225
  requirement: !ruby/object:Gem::Requirement
@@ -340,14 +340,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
340
340
  requirements:
341
341
  - - ">="
342
342
  - !ruby/object:Gem::Version
343
- version: '0'
343
+ version: '2.5'
344
344
  required_rubygems_version: !ruby/object:Gem::Requirement
345
345
  requirements:
346
346
  - - ">="
347
347
  - !ruby/object:Gem::Version
348
348
  version: '0'
349
349
  requirements: []
350
- rubygems_version: 3.2.0.rc.1
350
+ rubygems_version: 3.1.4
351
351
  signing_key:
352
352
  specification_version: 4
353
353
  summary: Generate equivalent source for parser gem AST nodes