unparser 0.6.5 → 0.6.15

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bd425c8abea3ba512e8652c7f913b5ed09b5254fa868358f000f3af1fcd3ed82
4
- data.tar.gz: caf2e58c2f862bd94f63d3290876b61620cf100a22c88a07d3916d87f253c0c0
3
+ metadata.gz: 9d1d0033d21b1c64de6affcc9c555797edf5206dc2947484ff8391e3c8e739ce
4
+ data.tar.gz: 60732ffbcc8a43cd2f04a6cba76e955b7e764ada9d201cd51a0a624d093b3659
5
5
  SHA512:
6
- metadata.gz: f45782eed90b778fbde30cdbd90f388a4b9f649e0080c26618e9243014618774d3afd9eb2076c7a47d0a0d43e137d1940382c85d0dea0915afa232f87d9e950d
7
- data.tar.gz: 2fda013558e05453f9ea1540b64bb51a9bd9269f6166395bd92f775aed3523abc5878b1c92c5ba4f0352842b6b4fb532f317f8a5d54aa108822f94dfcb7d9e9a
6
+ metadata.gz: 77db441f3e4da2b584065c597796d4adbb08b07e481b43ee85c80218375cbf8932bae847727afa9abee0be02ee5dea1c2effd5caa70216a5bafa34007d0c3820
7
+ data.tar.gz: c180d28c4ac14fe5d6b5756350592b472cd084ce932309fd6cc87f042dffb1f1870dd194260e71b53dbac71fa648bd359c7b4d3c8860c7508f7cc854c9d2ef25
data/README.md CHANGED
@@ -10,13 +10,13 @@ The following constraints apply:
10
10
 
11
11
  * No support for macruby extensions
12
12
  * Only support for the [modern AST](https://github.com/whitequark/parser/#usage) format
13
- * Only support for Ruby >= 2.7
13
+ * Only support for Ruby >= 3.1
14
14
 
15
15
  Notable Users:
16
16
 
17
17
  * [mutant](https://github.com/mbj/mutant) - Code review engine via mutation testing.
18
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).
19
+ * Many other [reverse-dependencies](https://rubygems.org/gems/unparser/reverse_dependencies).
20
20
 
21
21
  (if you want your tool to be mentioned here please PR the addition with a TLDR of your use case).
22
22
 
@@ -187,10 +187,15 @@ Contributing
187
187
  * Make your feature addition or bug fix.
188
188
  * Add tests for it. This is important so I don't break it in a
189
189
  future version unintentionally.
190
- * Commit, do not mess with Rakefile or version
190
+ * Commit, do not mess with version
191
191
  (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
192
192
  * Send me a pull request. Bonus points for topic branches.
193
193
 
194
+ Known Users
195
+ -------------
196
+
197
+ * [RailsRocket](https://www.railsrocket.app) - A no-code app builder that creates Rails apps
198
+
194
199
  License
195
200
  -------
196
201
 
data/bin/unparser CHANGED
@@ -2,7 +2,7 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  trap('INT') do |status|
5
- exit! 128 + status
5
+ exit! status + 128
6
6
  end
7
7
 
8
8
  require 'unparser'
@@ -20,9 +20,11 @@ module Unparser
20
20
  # @return [Object]
21
21
  #
22
22
  # @api public
23
+ #
24
+ # mutant:disable
23
25
  def freeze
24
26
  memoized_method_cache
25
- super()
27
+ super
26
28
  end
27
29
 
28
30
  private
@@ -5,7 +5,7 @@ module Unparser
5
5
 
6
6
  # Calculated local variable scope for a given node
7
7
  class LocalVariableScope
8
- include Enumerable, Adamantium, Concord.new(:node)
8
+ include Enumerable, Adamantium
9
9
 
10
10
  # Initialize object
11
11
  #
@@ -21,7 +21,6 @@ module Unparser
21
21
  items << scope
22
22
  end
23
23
  @items = items
24
- super(node)
25
24
  end
26
25
 
27
26
  # Test if local variable was first at given assignment
@@ -44,7 +44,6 @@ module Unparser
44
44
  #
45
45
  def append_without_prefix(string)
46
46
  write(string)
47
- self
48
47
  end
49
48
 
50
49
  # Increase indent
@@ -77,7 +76,6 @@ module Unparser
77
76
  #
78
77
  def nl
79
78
  write(NL)
80
- self
81
79
  end
82
80
 
83
81
  def root_indent
@@ -107,18 +105,6 @@ module Unparser
107
105
  @content.dup.freeze
108
106
  end
109
107
 
110
- # Capture the content written to the buffer within the block
111
- #
112
- # @return [String]
113
- #
114
- # @api private
115
- #
116
- def capture_content
117
- size_before = content.size
118
- yield
119
- content[size_before..]
120
- end
121
-
122
108
  # Write raw fragment to buffer
123
109
  #
124
110
  # Does not do indentation logic.
data/lib/unparser/cli.rb CHANGED
@@ -169,11 +169,11 @@ module Unparser
169
169
 
170
170
  def targets(file_name)
171
171
  if File.directory?(file_name)
172
- Dir.glob(File.join(file_name, '**/*.rb')).sort
172
+ Dir.glob(File.join(file_name, '**/*.rb'))
173
173
  elsif File.file?(file_name)
174
174
  [file_name]
175
175
  else
176
- Dir.glob(file_name).sort
176
+ Dir.glob(file_name)
177
177
  end.map { |file| Target::Path.new(Pathname.new(file)) }
178
178
  end
179
179
  end # CLI
@@ -28,7 +28,12 @@ module Unparser
28
28
 
29
29
  private
30
30
 
31
+ # Well rubocop you are static so you do not have a clue here ;)
32
+ # rubocop:disable Style/RedundantInitialize
33
+ # rubocop:disable Style/MissingSuper
31
34
  def initialize; end
35
+ # rubocop:enable Style/RedundantInitialize
36
+ # rubocop:enable Style/MissingSuper
32
37
 
33
38
  end.new
34
39
 
@@ -2,11 +2,13 @@
2
2
 
3
3
  module Unparser
4
4
  class Emitter
5
- # Emitter for block and kwrestarg arguments
6
- class Morearg < self
5
+ # Emitter for forwarding arguments
6
+ class ForwardArg < self
7
7
  MAP = {
8
- blockarg: '&',
9
- kwrestarg: '**'
8
+ blockarg: '&',
9
+ forwarded_kwrestarg: '**',
10
+ forwarded_restarg: '*',
11
+ kwrestarg: '**'
10
12
  }.freeze
11
13
 
12
14
  handle(*MAP.keys)
@@ -25,9 +25,14 @@ module Unparser
25
25
  private
26
26
 
27
27
  def dispatch
28
- visit(left)
28
+ visit(left) if left
29
29
  write(MAP.fetch(node.type))
30
- visit(right)
30
+
31
+ if right
32
+ visit(right)
33
+ else
34
+ write(';')
35
+ end
31
36
  end
32
37
  end # FlipFLop
33
38
  end # Emitter
@@ -6,14 +6,6 @@ module Unparser
6
6
  class Hash < self
7
7
  handle :hash
8
8
 
9
- def emit_last_argument_hash
10
- if children.empty?
11
- write('{}')
12
- else
13
- emit_hash_body
14
- end
15
- end
16
-
17
9
  def emit_heredoc_reminders
18
10
  children.each(&method(:emit_heredoc_reminder_member))
19
11
  end
@@ -33,7 +25,7 @@ module Unparser
33
25
  end
34
26
 
35
27
  def emit_heredoc_reminder_member(node)
36
- emitter(node.children.last).emit_heredoc_reminders
28
+ emitter(node.children.last).emit_heredoc_reminders if n_pair?(node)
37
29
  end
38
30
 
39
31
  def emit_hash_body
@@ -9,20 +9,11 @@ module Unparser
9
9
 
10
10
  children :target, :pattern
11
11
 
12
- # Modern ast format emits `match_pattern`
13
- # node on single line pre 3.0, but 3.0+ uses `match_pattern_p`
14
- SYMBOL =
15
- if RUBY_VERSION < '3.0'
16
- ' in '
17
- else
18
- ' => '
19
- end
20
-
21
12
  private
22
13
 
23
14
  def dispatch
24
15
  visit(target)
25
- write(SYMBOL)
16
+ write(' => ')
26
17
  visit(pattern)
27
18
  end
28
19
  end # MatchPattern
@@ -35,6 +35,11 @@ module Unparser
35
35
 
36
36
  children :target, :operator, :value
37
37
 
38
+ def emit_heredoc_reminders
39
+ emitter(target).emit_heredoc_reminders
40
+ emitter(value).emit_heredoc_reminders
41
+ end
42
+
38
43
  private
39
44
 
40
45
  def dispatch
@@ -15,19 +15,45 @@ module Unparser
15
15
  private
16
16
 
17
17
  def dispatch
18
- if colon?(key)
19
- write(key.children.first.to_s, ': ')
18
+ if colon?
19
+ emit_colon
20
+ unless implicit_value_lvar? || implicit_value_send?
21
+ write(' ')
22
+ visit(value)
23
+ end
20
24
  else
21
25
  visit(key)
22
26
  write(' => ')
27
+ visit(value)
23
28
  end
24
-
25
- visit(value)
26
29
  end
27
30
 
28
- def colon?(key)
31
+ def colon?
29
32
  n_sym?(key) && BAREWORD.match?(key.children.first)
30
33
  end
34
+
35
+ def emit_colon
36
+ write(key.children.first.to_s, ':')
37
+ end
38
+
39
+ def key_value
40
+ key.children.first
41
+ end
42
+
43
+ def implicit_value_lvar?
44
+ n_lvar?(value) && value.children.first.equal?(key_value)
45
+ end
46
+
47
+ def implicit_value_send?
48
+ children = value.children
49
+
50
+ n_send?(value) \
51
+ && !key_value.end_with?('?') \
52
+ && !key_value.end_with?('!') \
53
+ && children.fetch(0).nil? \
54
+ && children.fetch(1).equal?(key_value) \
55
+ && children.at(2).nil?
56
+ end
31
57
  end
32
58
  end
33
59
  end
@@ -10,7 +10,7 @@ module Unparser
10
10
  # Emitter for primitives based on Object#inspect
11
11
  class Inspect < self
12
12
 
13
- handle :sym, :str
13
+ handle :str
14
14
 
15
15
  private
16
16
 
@@ -20,6 +20,32 @@ module Unparser
20
20
 
21
21
  end # Inspect
22
22
 
23
+ class Symbol < self
24
+
25
+ handle :sym
26
+
27
+ private
28
+
29
+ # mutant:disable
30
+ def dispatch
31
+ if inspect_breaks_parsing?
32
+ write(":#{value.name.inspect}")
33
+ else
34
+ write(value.inspect)
35
+ end
36
+ end
37
+
38
+ # mutant:disable
39
+ def inspect_breaks_parsing?
40
+ return false unless RUBY_VERSION < '3.2.'
41
+
42
+ Unparser.parse(value.inspect)
43
+ false
44
+ rescue Parser::SyntaxError
45
+ true
46
+ end
47
+ end # Symbol
48
+
23
49
  # Emitter for complex literals
24
50
  class Complex < self
25
51
 
@@ -121,25 +121,29 @@ module Unparser
121
121
  end
122
122
 
123
123
  def emit_body(node, indent: true)
124
- if indent
125
- buffer.indent
126
- nl
127
- end
128
-
129
- if n_begin?(node)
130
- if node.children.one?
131
- visit_deep(node)
124
+ with_indent(indent: indent) do
125
+ if n_begin?(node)
126
+ if node.children.empty?
127
+ write('()')
128
+ elsif node.children.one?
129
+ visit_deep(node)
130
+ else
131
+ emit_body_inner(node)
132
+ end
132
133
  else
133
- emit_body_inner(node)
134
+ visit_deep(node)
134
135
  end
135
- else
136
- visit_deep(node)
137
136
  end
137
+ end
138
138
 
139
- if indent
140
- buffer.unindent
141
- nl
142
- end
139
+ def with_indent(indent:)
140
+ return yield unless indent
141
+
142
+ buffer.indent
143
+ nl
144
+ yield
145
+ buffer.unindent
146
+ nl
143
147
  end
144
148
 
145
149
  def emit_body_inner(node)
@@ -19,9 +19,10 @@ module Unparser
19
19
  end
20
20
 
21
21
  def binary_syntax_allowed?
22
- selector_binary_operator? \
23
- && arguments.one? \
24
- && !n_splat?(arguments.first) \
22
+ selector_binary_operator? \
23
+ && n_send?(node) \
24
+ && arguments.one? \
25
+ && !n_splat?(arguments.first) \
25
26
  && !n_kwargs?(arguments.first)
26
27
  end
27
28
 
@@ -52,6 +52,7 @@ module Unparser
52
52
  kwargs
53
53
  kwsplat
54
54
  lambda
55
+ lvar
55
56
  match_rest
56
57
  pair
57
58
  rescue
@@ -17,6 +17,7 @@ module Unparser
17
17
  #
18
18
  # @api private
19
19
  #
20
+ # rubocop:disable Style/OperatorMethodCall
20
21
  def success?
21
22
  [
22
23
  original_source,
@@ -25,6 +26,7 @@ module Unparser
25
26
  generated_node
26
27
  ].all?(&:right?) && generated_node.from_right.==(original_node.from_right)
27
28
  end
29
+ # rubocop:enable Style/OperatorMethodCall
28
30
 
29
31
  # Return error report
30
32
  #
@@ -113,18 +113,6 @@ module Unparser
113
113
  n_str?(last) && last.children.first[-1].eql?("\n")
114
114
  end
115
115
 
116
- def emit_squiggly_heredoc_body
117
- buffer.indent
118
- children.each do |child|
119
- if n_str?(child)
120
- write(escape_dynamic(child.children.first))
121
- else
122
- emit_dynamic(child)
123
- end
124
- end
125
- buffer.unindent
126
- end
127
-
128
116
  def emit_normal_heredoc_body
129
117
  buffer.root_indent do
130
118
  children.each do |child|
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.6.5
4
+ version: 0.6.15
5
5
  platform: ruby
6
6
  authors:
7
7
  - Markus Schirp
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-04-18 00:00:00.000000000 Z
11
+ date: 2024-06-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: diff-lcs
@@ -30,42 +30,42 @@ dependencies:
30
30
  requirements:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: 3.1.0
33
+ version: 3.3.0
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: 3.1.0
40
+ version: 3.3.0
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: mutant
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: 0.11.4
47
+ version: 0.12.2
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: 0.11.4
54
+ version: 0.12.2
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: mutant-rspec
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: 0.11.4
61
+ version: 0.12.2
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: 0.11.4
68
+ version: 0.12.2
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rspec
71
71
  requirement: !ruby/object:Gem::Requirement
@@ -251,6 +251,8 @@ metadata:
251
251
  bug_tracker_uri: https://github.com/mbj/unparser/issues
252
252
  changelog_uri: https://github.com/mbj/unparser/blob/master/Changelog.md
253
253
  funding_uri: https://github.com/sponsors/mbj
254
+ source_code_uri: https://github.com/mbj/unparser
255
+ rubygems_mfa_required: 'true'
254
256
  post_install_message:
255
257
  rdoc_options: []
256
258
  require_paths:
@@ -259,14 +261,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
259
261
  requirements:
260
262
  - - ">="
261
263
  - !ruby/object:Gem::Version
262
- version: '2.7'
264
+ version: '3.1'
263
265
  required_rubygems_version: !ruby/object:Gem::Requirement
264
266
  requirements:
265
267
  - - ">="
266
268
  - !ruby/object:Gem::Version
267
269
  version: '0'
268
270
  requirements: []
269
- rubygems_version: 3.1.6
271
+ rubygems_version: 3.5.9
270
272
  signing_key:
271
273
  specification_version: 4
272
274
  summary: Generate equivalent source for parser gem AST nodes