prettyrb 0.3.1 → 0.4.0

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: '007694f712c84951a00b378847984d9c2d63c11657401cbf162d2990c7390145'
4
- data.tar.gz: cd6a21d3898fd52ad7564f4d23232a97190211a8a96c854b56d1cf05c0d34bf6
3
+ metadata.gz: cfaf933924416b3773afaed63ee03ca8c2ddaa9a3e36147c3db453f24245e375
4
+ data.tar.gz: b5fcfe41b9738e7bd628501b1aff497e7083cb7f6e061fa7e5821719c7c86bcf
5
5
  SHA512:
6
- metadata.gz: e7241d399abb72fde4d602824b7e57c8eecf223b4d79b69bac273d975a79ffd970fbd64163d44ac550fdf8c04f8e2b16cc7812a2966abf73dc74a644270a2280
7
- data.tar.gz: 15a570f713afaf041661fdb7f04f4098d7cb8093f7fdd36ee9bf568b50fbd0aff39f061f27a5393a5f91d950d573a38d32ab5700de64dcc4455c3aa9a43cf4ad
6
+ metadata.gz: de542dd1e0f5999aa4aec410a8329e387944e1ce370805c2c4aaa805f5020034287fb7db8e0bfe4d3c2627b722c6b0d3a3cfa0024dbc4255fba9e3161cea0a9f
7
+ data.tar.gz: 953349398a0107c7012336a3a69dd0ae5c700cde330fced7db07cd96876178cfe87457273fc4440b48caabd2f050b26f79b26e0ec8f2c40af279fe2c3246ab4a
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- prettyrb (0.3.0)
4
+ prettyrb (0.3.1)
5
5
  parser (~> 2.7.0.5)
6
6
  thor
7
7
 
@@ -9,11 +9,15 @@ GEM
9
9
  remote: https://rubygems.org/
10
10
  specs:
11
11
  ast (2.4.0)
12
- byebug (11.1.1)
12
+ coderay (1.1.2)
13
13
  docile (1.3.2)
14
+ method_source (1.0.0)
14
15
  minitest (5.14.0)
15
16
  parser (2.7.0.5)
16
17
  ast (~> 2.4.0)
18
+ pry (0.13.1)
19
+ coderay (~> 1.1)
20
+ method_source (~> 1.0)
17
21
  rake (12.3.3)
18
22
  simplecov (0.18.5)
19
23
  docile (~> 1.1)
@@ -25,12 +29,12 @@ PLATFORMS
25
29
  ruby
26
30
 
27
31
  DEPENDENCIES
28
- bundler (~> 1.17)
29
- byebug (~> 11.1)
32
+ bundler (~> 2.1.4)
30
33
  minitest (~> 5.0)
31
34
  prettyrb!
35
+ pry (~> 0.13)
32
36
  rake (~> 12.3.3)
33
37
  simplecov (~> 0.18)
34
38
 
35
39
  BUNDLED WITH
36
- 1.17.2
40
+ 2.1.4
data/README.md CHANGED
@@ -22,12 +22,24 @@ Or install it yourself as:
22
22
 
23
23
  ## Usage
24
24
 
25
+ CLI:
26
+
27
+ ```
28
+ $ prettyrb format FILE
29
+ ```
30
+
31
+ or to re-write the file:
32
+
33
+ ```
34
+ prettyrb format FILE --write
35
+ ```
36
+
37
+ In Ruby code:
38
+
25
39
  ```ruby
26
40
  PrettyRb.new(source_code).format
27
41
  ```
28
42
 
29
- CLI helper coming soon™
30
-
31
43
  ## License
32
44
 
33
45
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,15 @@
1
+ module Prettyrb
2
+ class Builder < Parser::Builders::Default
3
+ NODE_TYPES = {
4
+ if: Prettyrb::Nodes::IfNode,
5
+ str: Prettyrb::Nodes::StrNode,
6
+ dstr: Prettyrb::Nodes::DstrNode,
7
+ }.freeze
8
+
9
+ def n(type, children, source_map)
10
+ node_class = NODE_TYPES.fetch(type, Prettyrb::Nodes::BaseNode)
11
+
12
+ node_class.new(type, children, location: source_map)
13
+ end
14
+ end
15
+ end
data/lib/prettyrb/cli.rb CHANGED
@@ -2,10 +2,20 @@ require "thor"
2
2
 
3
3
  module Prettyrb
4
4
  class CLI < Thor
5
- desc "format FILE", "file to prettify"
6
- def format(file)
5
+ desc "format [FILE]", "Ruby file to prettify"
6
+ option :write, type: :boolean
7
+ method_option :files, type: :array
8
+ def format(*files)
7
9
  content = File.read(file)
8
- puts Prettyrb::Formatter.new(content).format
10
+ formatted_content = Prettyrb::Formatter.new(content).format
11
+
12
+ if options[:write]
13
+ File.open(file, 'w') do |f|
14
+ f.write(formatted_content)
15
+ end
16
+ else
17
+ puts formatted_content
18
+ end
9
19
  end
10
20
  end
11
21
  end
@@ -7,10 +7,21 @@ module Prettyrb
7
7
  end
8
8
 
9
9
  def format
10
- root_node, _comments = Parser::CurrentRuby.parse_with_comments(@code)
10
+ parser = Parser::CurrentRuby.new(Prettyrb::Builder.new)
11
+
12
+ parser.diagnostics.all_errors_are_fatal = true
13
+ parser.diagnostics.ignore_warnings = true
14
+
15
+ parser.diagnostics.consumer = lambda do |diagnostic|
16
+ $stderr.puts(diagnostic.render)
17
+ end
18
+
19
+ root_node, _comments = parser.parse_with_comments(
20
+ Parser::CurrentRuby.send(:setup_source_buffer, "file='(string)'", 1, @code, parser.default_encoding)
21
+ )
11
22
 
12
23
  visitor = Visitor.new
13
- visitor.visit(root_node, nil)
24
+ visitor.visit(root_node)
14
25
 
15
26
  visitor.output
16
27
  end
@@ -0,0 +1,30 @@
1
+ require "delegate"
2
+
3
+ module Prettyrb
4
+ module Nodes
5
+ class BaseNode < Parser::AST::Node
6
+ def initialize(type, children, properties)
7
+ @mutable = {}
8
+
9
+ super
10
+
11
+ children.each do |child|
12
+ next unless child.is_a?(BaseNode)
13
+ child.parent = self
14
+ end
15
+
16
+ self
17
+ end
18
+
19
+ def parent
20
+ @mutable[:parent]
21
+ end
22
+
23
+ protected
24
+
25
+ def parent=(parent)
26
+ @mutable[:parent] = parent
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,20 @@
1
+ module Prettyrb
2
+ module Nodes
3
+ class DstrNode < BaseNode
4
+ include StringHelper
5
+
6
+ HEREDOC_TYPE_REGEX = /<<(.)?/
7
+
8
+ def format
9
+ raw_content = loc.expression.source
10
+ content = raw_content[1...-1]
11
+
12
+ if raw_content[0] == "'"
13
+ content.gsub('"', '\\"').gsub('#{', '\\#{')
14
+ else
15
+ content.gsub("\\", "\\\\")
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,47 @@
1
+ module Prettyrb
2
+ module Nodes
3
+ class IfNode < BaseNode
4
+ def if_type
5
+ if is_elsif?
6
+ "elsif"
7
+ elsif unless_node?
8
+ "unless"
9
+ else
10
+ "if"
11
+ end
12
+ end
13
+
14
+ def conditions_node
15
+ children[0]
16
+ end
17
+
18
+ def body_node
19
+ if unless_node?
20
+ children[2]
21
+ else
22
+ children[1]
23
+ end
24
+ end
25
+
26
+ def else_body_node
27
+ if unless_node?
28
+ children[1]
29
+ else
30
+ children[2]
31
+ end
32
+ end
33
+
34
+ def has_elsif?
35
+ else_body_node&.type == :if && children[1]&.type != :if
36
+ end
37
+
38
+ def is_elsif?
39
+ parent&.type == :if && parent&.children[1]&.type != :if
40
+ end
41
+
42
+ def unless_node?
43
+ children[1].nil? && children[2] != :if
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,18 @@
1
+ module Prettyrb
2
+ module Nodes
3
+ class StrNode < BaseNode
4
+ include StringHelper
5
+
6
+ def format
7
+ raw_content = loc.expression.source
8
+ content = raw_content[1...-1]
9
+
10
+ if raw_content[0] == "'"
11
+ content.gsub('"', '\\"').gsub('#{', '\\#{')
12
+ else
13
+ content.gsub("\\", "\\\\")
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,27 @@
1
+ module Prettyrb
2
+ module Nodes
3
+ module StringHelper
4
+ HEREDOC_TYPE_REGEX = /<<(.)?/
5
+
6
+ def heredoc_identifier
7
+ loc.heredoc_end.source.strip
8
+ end
9
+
10
+ def heredoc_type
11
+ # Always use indentable ending heredoc type if no type was provided
12
+ #
13
+ # eg: <<RUBY becomes <<-RUBY since <<- allows the ending identifier
14
+ # to be indented
15
+ loc.expression.source.match(HEREDOC_TYPE_REGEX)[1] || "-"
16
+ end
17
+
18
+ def heredoc_body
19
+ loc.heredoc_body.source
20
+ end
21
+
22
+ def heredoc?
23
+ !!loc.respond_to?(:heredoc_body)
24
+ end
25
+ end
26
+ end
27
+ end
@@ -1,3 +1,3 @@
1
1
  module Prettyrb
2
- VERSION = "0.3.1"
2
+ VERSION = "0.4.0"
3
3
  end
@@ -104,25 +104,25 @@ module Prettyrb
104
104
  @current_line = ''
105
105
  end
106
106
 
107
- def write(input)
107
+ def write(input, skip_indent: false)
108
108
  if @newline
109
- @output << indents
110
- @current_line << indents
109
+ @output << indents unless skip_indent
110
+ @current_line << indents unless skip_indent
111
111
  end
112
112
  @newline = false
113
113
  @output << input
114
114
  @current_line << input
115
115
  end
116
116
 
117
- def visit(node, parent_node)
117
+ def visit(node)
118
118
  case node.type
119
119
  when :module
120
120
  write "module "
121
- visit node.children[0], node
121
+ visit node.children[0]
122
122
  newline
123
123
 
124
124
  indent do
125
- visit node.children[1], node
125
+ visit node.children[1]
126
126
  end
127
127
 
128
128
  write "end"
@@ -130,46 +130,46 @@ module Prettyrb
130
130
  when :class
131
131
  newline unless @previous_node.nil?
132
132
  write "class "
133
- visit node.children[0], node
133
+ visit node.children[0]
134
134
  newline
135
135
  # TODO handle children[1] which is inheritance
136
136
 
137
137
  indent do
138
- visit node.children[2], node
138
+ visit node.children[2] if node.children[2]
139
139
  end
140
140
 
141
141
  newline unless @output.end_with?("\n")
142
142
  write "end"
143
143
  newline
144
144
  when :const
145
- visit node.children[0], node if node.children[0]
145
+ visit node.children[0] if node.children[0]
146
146
  write node.children[1].to_s
147
147
  when :casgn
148
148
  write node.children[1].to_s
149
149
  write " = "
150
- visit node.children[2], node
150
+ visit node.children[2]
151
151
  when :block
152
152
  newline unless @previous_node.nil?
153
- visit node.children[0], node
153
+ visit node.children[0]
154
154
  write " do"
155
155
 
156
156
  if node.children[1].children.length > 0
157
157
  write " |"
158
- visit node.children[1], node
158
+ visit node.children[1]
159
159
  write "|"
160
160
  end
161
161
 
162
162
  newline
163
163
 
164
164
  indent do
165
- visit node.children[2], node
165
+ visit node.children[2]
166
166
  end
167
167
 
168
168
  newline
169
169
 
170
170
  write "end"
171
171
  when :send
172
- newline if parent_node&.type == :begin && @previous_node && @previous_node&.type != :send
172
+ newline if node.parent&.type == :begin && @previous_node && @previous_node&.type != :send
173
173
  if node.children[0] == nil
174
174
  write node.children[1].to_s
175
175
 
@@ -187,22 +187,24 @@ module Prettyrb
187
187
 
188
188
  newline if @previous_node&.type == :class
189
189
  elsif node.children[1] == :[]
190
- visit node.children[0], node
190
+ visit node.children[0]
191
191
  write "["
192
- visit node.children[2], node
192
+ visit node.children[2]
193
193
  write "]"
194
194
  elsif node.children[1] == :!
195
195
  write "!"
196
- visit node.children[0], node
196
+ visit node.children[0]
197
+ elsif node.children[1] == :-@ && node.children[2].nil?
198
+ write "-"
199
+ visit node.children[0]
197
200
  elsif !node.children[1].to_s.match?(/[a-zA-Z]/)
198
- # if [:!=, :==, :+, :-, :*, :/, :<<, :<].include?(node.children[1])
199
- visit node.children[0], node
201
+ visit node.children[0]
200
202
  write " "
201
203
  write node.children[1].to_s
202
204
  write " "
203
- visit node.children[2], node
205
+ visit node.children[2]
204
206
  else
205
- visit node.children[0], node
207
+ visit node.children[0]
206
208
  write "."
207
209
  write node.children[1].to_s
208
210
 
@@ -211,99 +213,58 @@ module Prettyrb
211
213
  if arguments.length > 0
212
214
  write "("
213
215
  arguments.each_with_index do |child_node, index|
214
- visit child_node, node
216
+ visit child_node
215
217
  write ", " unless index == arguments.length - 1
216
218
  end
217
219
  write ")"
218
220
  end
219
221
  end
220
222
  when :if
221
- newline if @previous_node && parent_node&.type != :if
223
+ newline if @previous_node && node.parent&.type != :if
224
+ conditions = node.conditions_node
222
225
 
223
- if parent_node&.type == :if
224
- conditions_node = node.children[0]
225
- body_node = node.children[1]
226
- else_body_node = node.children[2]
226
+ write node.if_type
227
227
 
228
- write "elsif"
229
-
230
- conditions = capture do
231
- visit conditions_node, node
232
- end
228
+ conditions = capture do
229
+ visit node.children[0]
230
+ end
233
231
 
232
+ indent do
234
233
  if !conditions.start_with?("\n")
235
234
  write(" ")
236
- end
237
-
238
- write conditions
239
- newline
240
-
241
- if body_node.type == :if
242
- visit body_node, node
235
+ write conditions
243
236
  else
244
- indent do
245
- visit body_node, node
237
+ visit node.conditions_node
238
+
239
+ dedent do
240
+ newline
241
+ write "then"
246
242
  end
247
243
  end
248
244
 
249
245
  newline
246
+ visit node.body_node
247
+ end
250
248
 
251
- if else_body_node&.type == :if
252
- visit else_body_node, node
253
- elsif else_body_node
254
- write "else"
255
- newline
256
- indent do
257
- visit else_body_node, node
258
- end
259
- end
260
- else
261
- is_unless = node.children[1].nil? && node.children[2]&.type != :if
262
- conditions = node.children[0]
249
+ newline
263
250
 
264
- if is_unless
265
- write "unless"
266
- body_node = node.children[2]
267
- else_body_node = nil
251
+ if node.else_body_node
252
+ if node.has_elsif?
253
+ visit node.else_body_node
268
254
  else
269
- write "if" unless parent_node&.type == :if
270
- body_node = node.children[1]
271
- else_body_node = node.children[2]
272
- end
273
-
274
- indent do
275
- conditions = capture do
276
- visit node.children[0], node
277
- end
278
-
279
- if !conditions.start_with?("\n")
280
- write(" ")
281
- end
282
-
283
- write conditions
255
+ write "else"
284
256
  newline
285
257
 
286
- if body_node
287
- visit body_node, node
288
- newline
258
+ indent do
259
+ visit node.else_body_node
289
260
  end
290
- end
291
261
 
292
- if else_body_node
293
- if else_body_node.type == :if
294
- visit else_body_node, node
295
- else
296
- write "else"
297
- newline
298
-
299
- indent do
300
- visit else_body_node, node
301
- end
302
- end
303
262
  newline
304
263
  end
264
+ end
305
265
 
306
- write "end" unless parent_node&.type == :if
266
+ if !node.is_elsif?
267
+ write "end"
307
268
  end
308
269
  when :true
309
270
  write "true"
@@ -313,39 +274,84 @@ module Prettyrb
313
274
  write "nil"
314
275
  when :int, :float
315
276
  write node.children[0].to_s
277
+ when :next
278
+ write "next"
279
+
280
+ if node.children[0]
281
+ write " "
282
+ visit node.children[0]
283
+ end
316
284
  when :array
317
- if node.children[0].type == :splat
318
- visit node.children[0], node
285
+ if node.children[0]&.type == :splat
286
+ visit node.children[0]
319
287
  else
320
- write "["
321
- indent do
322
- result = splittable_separated_map(node, node.children)
323
- newline if result == MULTI_LINE
288
+ write "[" unless node.parent&.type == :resbody
289
+ if node.children.length > 0
290
+ indent do
291
+ result = splittable_separated_map(node, node.children)
292
+ newline if result == MULTI_LINE
293
+ end
324
294
  end
325
- write "]"
295
+ write "]" unless node.parent&.type == :resbody
326
296
  end
327
297
  when :str
328
- write '"'
329
- write format_string(node)
330
- write '"'
298
+ if node.heredoc?
299
+ if node&.parent&.type == :send
300
+ write "("
301
+ end
302
+ write "<<"
303
+ write node.heredoc_type if node.heredoc_type
304
+ write node.heredoc_identifier
305
+ newline
306
+ write node.heredoc_body, skip_indent: true
307
+ @newline = true
308
+ write node.heredoc_identifier
309
+
310
+ if node&.parent&.type == :send
311
+ newline
312
+ write ")"
313
+ end
314
+ else
315
+ write '"'
316
+ write node.format
317
+ write '"'
318
+ end
331
319
  when :dstr
332
- write "\""
333
- node.children.map do |child|
334
- if child.type == :str
335
- write child.children[0] # TODO better handling
336
- else
337
- write '#{'
338
- visit child, node
339
- write '}'
320
+ if node.heredoc?
321
+ if node&.parent&.type == :send
322
+ write "("
323
+ end
324
+ write "<<"
325
+ write node.heredoc_type if node.heredoc_type
326
+ write node.heredoc_identifier
327
+ newline
328
+ write node.heredoc_body, skip_indent: true
329
+ @newline = true
330
+ write node.heredoc_identifier
331
+
332
+ if node&.parent&.type == :send
333
+ newline
334
+ write ")"
335
+ end
336
+ else
337
+ write "\""
338
+ node.children.map do |child|
339
+ if child.type == :str
340
+ write child.format
341
+ else
342
+ write '#{'
343
+ visit child
344
+ write '}'
345
+ end
340
346
  end
347
+ write "\""
341
348
  end
342
- write "\""
343
349
  when :begin
344
350
  if @previous_node&.type == :or || @previous_node&.type == :and
345
351
  write "("
346
352
  @previous_node = nil
347
353
  node.children.map do |child|
348
- visit child, node
354
+ visit child
349
355
  @previous_node = child
350
356
  end
351
357
  @previous_node = nil
@@ -353,22 +359,22 @@ module Prettyrb
353
359
  else
354
360
  @previous_node = nil
355
361
  node.children.each_with_index do |child, index|
356
- visit child, node
362
+ visit child
357
363
  newline unless index == node.children.length - 1
358
364
  @previous_node = child
359
365
  end
360
366
  @previous_node = nil
361
367
  end
362
368
  when :or, :and
363
- write "(" if parent_node&.type == :begin
369
+ write "(" if node.parent&.type == :begin
364
370
  possible_output = capture do
365
- visit node.children[0], node
371
+ visit node.children[0]
366
372
  if node.type == :or
367
373
  write " || "
368
374
  elsif node.type == :and
369
375
  write " && "
370
376
  end
371
- visit node.children[1], node
377
+ visit node.children[1]
372
378
  end
373
379
  if @multiline_conditional_level > 0 # TODO track and check currently level
374
380
  write_multiline_conditional(node)
@@ -377,15 +383,10 @@ module Prettyrb
377
383
  newline
378
384
  write_multiline_conditional(node)
379
385
  end
380
-
381
- dedent do
382
- newline
383
- write "then"
384
- end
385
386
  else
386
387
  write possible_output
387
388
  end
388
- write ")" if parent_node&.type == :begin
389
+ write ")" if node.parent&.type == :begin
389
390
  when :def, :defs
390
391
  newline unless @previous_node&.type.nil?
391
392
  if node.type == :defs
@@ -404,14 +405,14 @@ module Prettyrb
404
405
 
405
406
  if arguments_node.children.length > 0 || arguments_node.type != :args
406
407
  write "("
407
- visit arguments_node, node
408
+ visit arguments_node
408
409
  write ")"
409
410
  end
410
411
  newline
411
412
 
412
413
  if body_node
413
414
  indent do
414
- visit body_node, node
415
+ visit body_node
415
416
  end
416
417
 
417
418
  newline
@@ -420,7 +421,7 @@ module Prettyrb
420
421
  write "end"
421
422
  when :args
422
423
  node.children.each_with_index do |child, index|
423
- visit child, node
424
+ visit child
424
425
  write ", " unless index == node.children.length - 1
425
426
  end
426
427
  when :arg
@@ -438,7 +439,7 @@ module Prettyrb
438
439
  write ":"
439
440
  write content
440
441
  else
441
- if parent_node&.type == :pair
442
+ if node.parent&.type == :pair
442
443
  write content
443
444
  write ": "
444
445
  else
@@ -449,16 +450,18 @@ module Prettyrb
449
450
  when :return
450
451
  write "return"
451
452
 
452
- possible_output = capture do
453
- visit node.children[0], node
454
- end
453
+ if node.children[0]
454
+ possible_output = capture do
455
+ visit node.children[0]
456
+ end
455
457
 
456
- if !possible_output.start_with?("\n")
457
- write " "
458
+ if !possible_output.start_with?("\n")
459
+ write " "
460
+ end
458
461
  end
459
462
  when :case
460
463
  write "case "
461
- visit node.children[0], node
464
+ visit node.children[0]
462
465
  newline
463
466
  node.children[1..-1].each do |child|
464
467
  if child && child.type != :when
@@ -466,11 +469,11 @@ module Prettyrb
466
469
  newline
467
470
 
468
471
  indent do
469
- visit child, node
472
+ visit child
470
473
  end
471
474
  else
472
475
  if child
473
- visit child, node
476
+ visit child
474
477
  newline
475
478
  end
476
479
  end
@@ -482,11 +485,11 @@ module Prettyrb
482
485
  if child_node.type == :str
483
486
  write child_node.children[0].to_s
484
487
  else
485
- visit child_node, node
488
+ visit child_node
486
489
  end
487
490
  end
488
491
  write '/'
489
- visit node.children[-1], node
492
+ visit node.children[-1]
490
493
  when :regopt
491
494
  node.children.map { |child| child.to_s }.join('')
492
495
  when :when
@@ -498,27 +501,27 @@ module Prettyrb
498
501
 
499
502
  newline
500
503
  indent do
501
- visit node.children[-1], node
504
+ visit node.children[-1]
502
505
  end
503
506
  when :or_asgn, :and_asgn
504
507
  newline if @previous_node && ![:ivasgn, :or_asgn, :lvasgn, :op_asgn].include?(@previous_node.type)
505
- visit node.children[0], node
508
+ visit node.children[0]
506
509
  if node.type == :or_asgn
507
510
  write " ||= " # TODO handle long lines here too
508
511
  elsif node.type == :and_asgn
509
512
  write " &&= " # TODO handle long lines here too
510
513
  end
511
- visit node.children[1], node
514
+ visit node.children[1]
512
515
  when :ivasgn
513
516
  newline if @previous_node && ![:ivasgn, :or_asgn, :lvasgn, :op_asgn].include?(@previous_node.type)
514
517
  write node.children[0].to_s
515
518
 
516
519
  if node.children[1]
517
520
  write " = "
518
- visit node.children[1], node
521
+ visit node.children[1]
519
522
  end
520
523
  when :csend
521
- visit node.children[0], node
524
+ visit node.children[0]
522
525
  write "&."
523
526
  write node.children[1].to_s
524
527
  when :ivar
@@ -531,27 +534,27 @@ module Prettyrb
531
534
  write "yield"
532
535
  when :op_asgn
533
536
  newline if @previous_node && ![:ivasgn, :or_asgn, :lvasgn, :op_asgn].include?(@previous_node.type)
534
- visit node.children[0], node
537
+ visit node.children[0]
535
538
  write " "
536
539
  write node.children[1].to_s
537
540
  write "="
538
541
  write " "
539
- visit node.children[2], node
542
+ visit node.children[2]
540
543
  when :lvasgn
541
544
  newline if @previous_node && ![:ivasgn, :or_asgn, :lvasgn, :op_asgn].include?(@previous_node.type)
542
545
  write node.children[0].to_s
543
546
  if node.children[1]
544
547
  write " = "
545
- visit node.children[1], node
548
+ visit node.children[1]
546
549
  end
547
550
  when :irange
548
- visit node.children[0], node unless node.children[0].nil?
551
+ visit node.children[0] unless node.children[0].nil?
549
552
  write ".."
550
- visit node.children[1], node unless node.children[1].nil?
553
+ visit node.children[1] unless node.children[1].nil?
551
554
  when :erange
552
- visit node.children[0], node unless node.children[0].nil?
555
+ visit node.children[0] unless node.children[0].nil?
553
556
  write "..."
554
- visit node.children[1], node unless node.children[1].nil?
557
+ visit node.children[1] unless node.children[1].nil?
555
558
  when :hash
556
559
  if node.children.length == 0
557
560
  write "{}"
@@ -570,17 +573,17 @@ module Prettyrb
570
573
  end
571
574
  end
572
575
  when :pair
573
- visit node.children[0], node
576
+ visit node.children[0]
574
577
  if node.children[0].type != :sym
575
578
  write " => "
576
579
  end
577
- visit node.children[1], node
580
+ visit node.children[1]
578
581
  when :splat
579
582
  write "*"
580
- visit node.children[0], node
583
+ visit node.children[0]
581
584
  when :defined?
582
585
  write "defined?("
583
- visit node.children[0], node
586
+ visit node.children[0]
584
587
  write ")"
585
588
  when :complex,
586
589
  :dsym,
@@ -588,17 +591,16 @@ module Prettyrb
588
591
  :'nth-ref',
589
592
  :'back-ref',
590
593
  :gvasgn,
591
- :mlhs,
592
594
  :procarg0,
593
595
  :shadowarg
594
596
  raise "implement me, #{node.inspect}"
595
597
  when :sclass
596
598
  write "class << "
597
- visit node.children[0], node
599
+ visit node.children[0]
598
600
  newline
599
601
 
600
602
  indent do
601
- visit node.children[1], node if node.children[1]
603
+ visit node.children[1] if node.children[1]
602
604
  end
603
605
 
604
606
  newline if node.children[1]
@@ -606,24 +608,24 @@ module Prettyrb
606
608
  when :undef
607
609
  write "undef "
608
610
  node.children.each_with_index do |child_node, index|
609
- visit child_node, node
611
+ visit child_node
610
612
  write ", " unless index == node.children.length - 1
611
613
  end
612
614
  when :alias
613
615
  write 'alias '
614
- visit node.children[0], node
616
+ visit node.children[0]
615
617
  write ' '
616
- visit node.children[1], node
618
+ visit node.children[1]
617
619
  when :restarg
618
620
  write "*"
619
621
  write node.children[0].to_s
620
622
  when :optarg
621
623
  write node.children[0].to_s
622
624
  write " = "
623
- visit node.children[1], node
625
+ visit node.children[1]
624
626
  when :kwsplat
625
627
  write "**"
626
- visit node.children[0], node
628
+ visit node.children[0]
627
629
  when :kwarg
628
630
  write node.children[0].to_s
629
631
  write ":"
@@ -632,7 +634,7 @@ module Prettyrb
632
634
  when :kwoptarg
633
635
  write node.children[0].to_s
634
636
  write ": "
635
- visit node.children[1], node
637
+ visit node.children[1]
636
638
  when :kwrestarg
637
639
  write "**"
638
640
  write node.children[0].to_s if node.children[0]
@@ -640,13 +642,79 @@ module Prettyrb
640
642
  write "**nil"
641
643
  when :cbase
642
644
  write "::"
645
+ when :zsuper
646
+ write "super"
647
+ when :nth_ref
648
+ write "$"
649
+ write node.children[0].to_s
650
+ when :masgn
651
+ left = node.children[0...-1]
652
+ right = node.children[-1]
653
+
654
+ left.each_with_index do |child_node, index|
655
+ visit child_node
656
+ end
657
+
658
+ write " = "
659
+ visit right
660
+ when :mlhs
661
+ write "(" if node.parent&.type == :mlhs
662
+ node.children.each_with_index do |child_node, index|
663
+ visit child_node
664
+ write ", " unless index == node.children.length - 1
665
+ end
666
+ write ")" if node.parent&.type == :mlhs
667
+ when :block_pass
668
+ write "&"
669
+ visit node.children[0]
670
+ when :kwbegin
671
+ write "begin"
672
+ newline
673
+ indent do
674
+ visit node.children[0]
675
+ end
676
+ newline
677
+ write "end"
678
+ when :rescue
679
+ visit node.children[0]
680
+ newline
681
+
682
+ dedent do
683
+ write "rescue"
684
+ visit node.children[1]
685
+ end
686
+ when :resbody
687
+ if node.children[0]
688
+ write " "
689
+ visit node.children[0] if node.children[0]
690
+ end
691
+
692
+ if node.children[1]
693
+ write " => "
694
+ write node.children[1].children[0].to_s
695
+ end
696
+ newline
697
+
698
+ indent do
699
+ visit node.children[2] if node.children[2]
700
+ end
701
+ when :ensure
702
+ visit node.children[0] if node.children[0]
703
+ newline
704
+
705
+ dedent do
706
+ write "ensure"
707
+ end
708
+
709
+ newline
710
+ visit node.children[1] if node.children[1]
643
711
  else
644
712
  raise "unhandled node type `#{node.type}`\nnode: #{node}"
645
713
  end
646
714
  end
647
715
 
648
716
  def write_multiline_conditional(node)
649
- visit node.children[0], node
717
+ visit node.children[0]
650
718
 
651
719
  if node.type == :or
652
720
  write " ||"
@@ -656,7 +724,7 @@ module Prettyrb
656
724
 
657
725
  newline
658
726
 
659
- visit node.children[1], node
727
+ visit node.children[1]
660
728
  end
661
729
 
662
730
  def format_string(string)
@@ -673,7 +741,7 @@ module Prettyrb
673
741
  def splittable_separated_map(current_node, mappable, separator: ", ", skip_last_multiline_separator: false, write_space_if_single_line: false)
674
742
  one_line = capture do
675
743
  mappable.each_with_index do |child_node, index|
676
- visit child_node, current_node
744
+ visit child_node
677
745
  write separator unless index == mappable.length - 1
678
746
  end
679
747
  end
@@ -681,7 +749,7 @@ module Prettyrb
681
749
  if @current_line.length + one_line.length > MAX_LENGTH
682
750
  mappable.each_with_index do |child_node, index|
683
751
  newline
684
- visit child_node, current_node
752
+ visit child_node
685
753
  write separator.rstrip unless skip_last_multiline_separator && index == mappable.length - 1
686
754
  end
687
755
  MULTI_LINE
data/lib/prettyrb.rb CHANGED
@@ -1,6 +1,12 @@
1
1
  require "parser/current"
2
2
  require "prettyrb/version"
3
3
 
4
+ require "prettyrb/nodes/base_node"
5
+ require "prettyrb/nodes/if_node"
6
+ require "prettyrb/nodes/string_helper"
7
+ require "prettyrb/nodes/str_node"
8
+ require "prettyrb/nodes/dstr_node"
9
+ require "prettyrb/builder"
4
10
  require "prettyrb/formatter"
5
11
  require "prettyrb/visitor"
6
12
 
data/prettyrb.gemspec CHANGED
@@ -35,9 +35,9 @@ Gem::Specification.new do |spec|
35
35
  spec.add_dependency "parser", "~> 2.7.0.5"
36
36
  spec.add_dependency "thor"
37
37
 
38
- spec.add_development_dependency "bundler", "~> 1.17"
38
+ spec.add_development_dependency "bundler", "~> 2.1.4"
39
39
  spec.add_development_dependency "rake", "~> 12.3.3"
40
40
  spec.add_development_dependency "minitest", "~> 5.0"
41
- spec.add_development_dependency "byebug", "~> 11.1"
41
+ spec.add_development_dependency "pry", "~> 0.13"
42
42
  spec.add_development_dependency "simplecov", "~> 0.18"
43
43
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: prettyrb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Blake Williams
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-04-06 00:00:00.000000000 Z
11
+ date: 2020-05-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: parser
@@ -44,14 +44,14 @@ dependencies:
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '1.17'
47
+ version: 2.1.4
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: '1.17'
54
+ version: 2.1.4
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: rake
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -81,19 +81,19 @@ dependencies:
81
81
  - !ruby/object:Gem::Version
82
82
  version: '5.0'
83
83
  - !ruby/object:Gem::Dependency
84
- name: byebug
84
+ name: pry
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
87
  - - "~>"
88
88
  - !ruby/object:Gem::Version
89
- version: '11.1'
89
+ version: '0.13'
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
94
  - - "~>"
95
95
  - !ruby/object:Gem::Version
96
- version: '11.1'
96
+ version: '0.13'
97
97
  - !ruby/object:Gem::Dependency
98
98
  name: simplecov
99
99
  requirement: !ruby/object:Gem::Requirement
@@ -129,8 +129,14 @@ files:
129
129
  - bin/setup
130
130
  - exe/prettyrb
131
131
  - lib/prettyrb.rb
132
+ - lib/prettyrb/builder.rb
132
133
  - lib/prettyrb/cli.rb
133
134
  - lib/prettyrb/formatter.rb
135
+ - lib/prettyrb/nodes/base_node.rb
136
+ - lib/prettyrb/nodes/dstr_node.rb
137
+ - lib/prettyrb/nodes/if_node.rb
138
+ - lib/prettyrb/nodes/str_node.rb
139
+ - lib/prettyrb/nodes/string_helper.rb
134
140
  - lib/prettyrb/version.rb
135
141
  - lib/prettyrb/visitor.rb
136
142
  - prettyrb.gemspec
@@ -154,7 +160,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
154
160
  - !ruby/object:Gem::Version
155
161
  version: '0'
156
162
  requirements: []
157
- rubygems_version: 3.1.2
163
+ rubygems_version: 3.1.3
158
164
  signing_key:
159
165
  specification_version: 4
160
166
  summary: Ruby source code formatter