rufo 0.0.9 → 0.0.10

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
  SHA1:
3
- metadata.gz: 372a723ce561b8de2f2b66f0c7e75a54a3244c70
4
- data.tar.gz: 2dc0c9d5c1bcd2b73a9cb0db3d9ed3f447cdbb08
3
+ metadata.gz: 9b4f9efa0b1ba8d9770f669de9facdb1b8b94d69
4
+ data.tar.gz: d851a347f0ca90587d9dc4575dd8a8e62f3e1105
5
5
  SHA512:
6
- metadata.gz: 0c54acef4e1604b32a35c87a939c698f7a58982f1bbee9b9fd3161bff76c2fe8db94f10a98d446cd91794b46d4e43857c7a97dbc759f5567f3863e1ee80d881d
7
- data.tar.gz: 071775e9ecba0da5b9c4cd3bdde5c65c7b8995589fd48269e0db74778cbab38fe4d5f07c94784dbb162a8c04bd3695835947e991ff9d5dc8f655c019f6d749bd
6
+ metadata.gz: 8b82667aad953010424caba2aa78c61234262932b9b3bfeee74c8ad6957c247f2911a8998b21e2ef1f65c4c18d87947310dab59f1ec057d69efbb174c18804e2
7
+ data.tar.gz: b430c54354544cd27f46cd11d873fc172f8dbc8d9be5c37dc6cd4a1875c8e72736945f44acf71e866c92f7b8368036d572a5d8b79fcfe0c7b9e43d87bb099fea
@@ -29,8 +29,8 @@ class Rufo::Formatter
29
29
  # Heredocs list, associated with calls ([call, heredoc, tilde])
30
30
  @heredocs = []
31
31
 
32
- # Current call, to be able to associate it to heredocs
33
- @current_call = nil
32
+ # Current node, to be able to associate it to heredocs
33
+ @current_node = nil
34
34
 
35
35
  # The current heredoc being printed
36
36
  @current_heredoc = nil
@@ -115,6 +115,9 @@ class Rufo::Formatter
115
115
  #
116
116
  # [:@int, "123.45", [1, 0]]
117
117
  consume_token :on_float
118
+ when :@CHAR
119
+ # [:@CHAR, "?a", [1, 0]]
120
+ consume_token :on_CHAR
118
121
  when :@gvar
119
122
  # [:@gvar, "$abc", [1, 0]]
120
123
  write node[1]
@@ -141,6 +144,9 @@ class Rufo::Formatter
141
144
  else
142
145
  consume_token :on_tstring_content
143
146
  end
147
+ when :string_content
148
+ # [:string_content, exp]
149
+ visit_exps node[1..-1], false, false
144
150
  when :string_embexpr
145
151
  # String interpolation piece ( #{exp} )
146
152
  visit_string_interpolation node
@@ -254,6 +260,8 @@ class Rufo::Formatter
254
260
  visit_mrhs_new_from_args(node)
255
261
  when :mlhs_paren
256
262
  visit_mlhs_paren(node)
263
+ when :mrhs_add_star
264
+ visit_mrhs_add_star(node)
257
265
  when :def
258
266
  visit_def(node)
259
267
  when :defs
@@ -319,6 +327,9 @@ class Rufo::Formatter
319
327
  visit_undef(node)
320
328
  when :mlhs_add_star
321
329
  visit_mlhs_add_star(node)
330
+ when :retry
331
+ # [:retry]
332
+ consume_keyword "retry"
322
333
  else
323
334
  bug "Unhandled node: #{node.first}"
324
335
  end
@@ -345,7 +356,14 @@ class Rufo::Formatter
345
356
  end
346
357
  end
347
358
 
348
- visit exp
359
+ push_node(exp) do
360
+ visit exp
361
+ end
362
+
363
+ skip_space
364
+ if newline? || comment?
365
+ check_heredocs_at_call_end(exp)
366
+ end
349
367
 
350
368
  line_before_endline = @line
351
369
 
@@ -388,8 +406,8 @@ class Rufo::Formatter
388
406
  # The same happens if we already have a heredoc in
389
407
  # the list, which means this will come after other
390
408
  # heredocs.
391
- if comma? || !@heredocs.empty?
392
- @heredocs << [@current_call, node, tilde]
409
+ if comma? || (current_token_kind == :on_period) || !@heredocs.empty?
410
+ @heredocs << [@current_node, node, tilde]
393
411
  return
394
412
  end
395
413
  elsif current_token_kind == :on_backtick
@@ -422,6 +440,12 @@ class Rufo::Formatter
422
440
  write current_token_value.rstrip
423
441
  end
424
442
  next_token
443
+ skip_space
444
+
445
+ if newline?
446
+ write_line
447
+ write_indent
448
+ end
425
449
  when :on_backtick
426
450
  consume_token :on_backtick
427
451
  else
@@ -471,9 +495,18 @@ class Rufo::Formatter
471
495
  # :"foo"
472
496
  #
473
497
  # [:dyna_symbol, exps]
474
- consume_token :on_symbeg
475
- visit_exps node[1], false, false
476
- consume_token :on_tstring_end
498
+ _, exps = node
499
+
500
+ # This is `"...":` as a hash key
501
+ if current_token_kind == :on_tstring_beg
502
+ consume_token :on_tstring_beg
503
+ visit exps
504
+ consume_token :on_label_end
505
+ else
506
+ consume_token :on_symbeg
507
+ visit_exps exps, false, false
508
+ consume_token :on_tstring_end
509
+ end
477
510
  end
478
511
 
479
512
  def visit_path(node)
@@ -532,6 +565,12 @@ class Rufo::Formatter
532
565
  _, lefts, right = node
533
566
 
534
567
  visit_comma_separated_list lefts
568
+ skip_space
569
+
570
+ # A trailing comma can come after the left hand side,
571
+ # and we remove it
572
+ next_token if comma?
573
+
535
574
  consume_space
536
575
  track_assignment
537
576
  consume_op "="
@@ -774,7 +813,11 @@ class Rufo::Formatter
774
813
  def visit_command_end(node, args)
775
814
  push_call(node) do
776
815
  indent(@column) do
777
- visit args
816
+ if args[0].is_a?(Symbol)
817
+ visit args
818
+ else
819
+ visit_exps args, false, false
820
+ end
778
821
  end
779
822
  end
780
823
 
@@ -946,11 +989,10 @@ class Rufo::Formatter
946
989
  if !args.empty? && args[0] == :args_add_star
947
990
  # arg1, ..., *star
948
991
  visit args
949
- return
992
+ else
993
+ visit_comma_separated_list args, true
950
994
  end
951
995
 
952
- visit_comma_separated_list args, true
953
-
954
996
  if block_arg
955
997
  write_params_comma if comma?
956
998
 
@@ -1044,7 +1086,7 @@ class Rufo::Formatter
1044
1086
  end
1045
1087
 
1046
1088
  def visit_rescue_types(node)
1047
- if node[0] == :mrhs_new_from_args
1089
+ if node[0].is_a?(Symbol)
1048
1090
  visit node
1049
1091
  else
1050
1092
  visit_exps node, false, false
@@ -1054,8 +1096,13 @@ class Rufo::Formatter
1054
1096
  def visit_mrhs_new_from_args(node)
1055
1097
  # Multiple exception types
1056
1098
  # [:mrhs_new_from_args, exps, final_exp]
1057
- nodes = [*node[1], node[2]]
1058
- visit_comma_separated_list(nodes)
1099
+ _, exps, final_exp = node
1100
+ if final_exp
1101
+ nodes = [*node[1], node[2]]
1102
+ visit_comma_separated_list(nodes)
1103
+ else
1104
+ visit exps
1105
+ end
1059
1106
  end
1060
1107
 
1061
1108
  def visit_mlhs_paren(node)
@@ -1086,6 +1133,22 @@ class Rufo::Formatter
1086
1133
  end
1087
1134
  end
1088
1135
 
1136
+ def visit_mrhs_add_star(node)
1137
+ # [:mrhs_add_star, [], [:vcall, [:@ident, "x", [3, 8]]]]
1138
+ _, x, y = node
1139
+
1140
+ if x.empty?
1141
+ consume_op "*"
1142
+ visit y
1143
+ else
1144
+ visit x
1145
+ write_params_comma
1146
+ consume_space
1147
+ consume_op "*"
1148
+ visit y
1149
+ end
1150
+ end
1151
+
1089
1152
  def visit_comma_separated_list(nodes, inside_call = false)
1090
1153
  # When there's *x inside a left hand side assignment
1091
1154
  # or a case when, it comes as [:op, ...]
@@ -1516,9 +1579,14 @@ class Rufo::Formatter
1516
1579
 
1517
1580
  if elements
1518
1581
  elements.each_with_index do |elem, i|
1519
- # elem is [:@tstring_content, string, [1, 5]
1520
- write elem[1].strip
1521
- next_token
1582
+ if elem[0] == :@tstring_content
1583
+ # elem is [:@tstring_content, string, [1, 5]
1584
+ write elem[1].strip
1585
+ next_token
1586
+ else
1587
+ visit elem
1588
+ end
1589
+
1522
1590
  unless last?(i, elements)
1523
1591
  check :on_words_sep
1524
1592
 
@@ -1589,6 +1657,11 @@ class Rufo::Formatter
1589
1657
  # [:assoc_new, key, value]
1590
1658
  _, key, value = node
1591
1659
 
1660
+ # If a symbol comes it means it's something like
1661
+ # `:foo => 1` or `:"foo" => 1` and a `=>`
1662
+ # always follows
1663
+ symbol = current_token_kind == :on_symbeg
1664
+
1592
1665
  visit key
1593
1666
 
1594
1667
  skip_space_or_newline
@@ -1597,7 +1670,8 @@ class Rufo::Formatter
1597
1670
  track_hash_key
1598
1671
 
1599
1672
  # Don't output `=>` for keys that are `label: value`
1600
- unless key[0] == :@label
1673
+ # or `"label": value`
1674
+ if symbol || !(key[0] == :@label || key[0] == :dyna_symbol)
1601
1675
  consume_op "=>"
1602
1676
  skip_space_or_newline
1603
1677
  write_space " "
@@ -2288,6 +2362,13 @@ class Rufo::Formatter
2288
2362
  # Ignore spaces
2289
2363
  next_token
2290
2364
  when :on_nl, :on_ignored_nl
2365
+ # I don't know why but sometimes a on_ignored_nl
2366
+ # can appear with nil as the "text", and that's wrong
2367
+ if current_token[2] == nil
2368
+ next_token
2369
+ next
2370
+ end
2371
+
2291
2372
  if last == :newline
2292
2373
  # If we pass through consecutive newlines, don't print them
2293
2374
  # yet, but remember this fact
@@ -2525,17 +2606,23 @@ class Rufo::Formatter
2525
2606
  i == array.size - 1
2526
2607
  end
2527
2608
 
2528
- def push_call(call)
2529
- old_call = @current_call
2530
- @current_call = call
2531
-
2532
- # A call can specify hash arguments so it acts as a
2533
- # hash for key alignment purposes
2534
- push_hash(call) do
2535
- yield
2609
+ def push_call(node)
2610
+ push_node(node) do
2611
+ # A call can specify hash arguments so it acts as a
2612
+ # hash for key alignment purposes
2613
+ push_hash(node) do
2614
+ yield
2615
+ end
2536
2616
  end
2617
+ end
2618
+
2619
+ def push_node(node)
2620
+ old_node = @current_node
2621
+ @current_node = node
2622
+
2623
+ yield
2537
2624
 
2538
- @current_call = old_call
2625
+ @current_node = old_node
2539
2626
  end
2540
2627
 
2541
2628
  def push_hash(node)
@@ -1,3 +1,3 @@
1
1
  module Rufo
2
- VERSION = "0.0.9"
2
+ VERSION = "0.0.10"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rufo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
4
+ version: 0.0.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ary Borenszweig