rufo 0.0.5 → 0.0.6

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: 4dee48483405a03bf26d15795f7d17aec4859813
4
- data.tar.gz: 2003268779c62c5ba28066b176112baec2200aff
3
+ metadata.gz: 8c3067349b8d78ecb9c824cb5ffd701e036532ff
4
+ data.tar.gz: 27c3b81f7a251b93d318dd84fed77b136dbcb420
5
5
  SHA512:
6
- metadata.gz: c1199060cc3e550ab376e758d04605692dd25c295a67dc329d214a0d6055ee8eac43e6812e8b75ebcfa2021a5f8a06e31d3c06975be8ecb100867e0f30984cc9
7
- data.tar.gz: '093f40f565a3d9c229ad7e467e5ba82a170844444cbdaf0d5d02795df65a1a2c9041fe7e95bb7191454bdd2895e5f1c5d2abb267df15b7aa34be6b1a5a271f77'
6
+ metadata.gz: 2bc69a1bf16c36c68f025a9593c00bc085cfeb004f0fcd2a7faafb2b289f07bd6d4d15d645dcaca7cb5fde6acf8e8c0050a5a0ccb8ccbd0382e853416ce442c3
7
+ data.tar.gz: b15595976259d23deeb7e2ef774e527ccfd1c95ef1880cdd6ec4ae05bf60e1e787d0334beaa36b01d7c7e0c1b7e87a9810837e13f860f0314357243a2531e1e4
@@ -10,5 +10,6 @@ module Rufo
10
10
  end
11
11
  end
12
12
 
13
+ require_relative "rufo/backport"
13
14
  require_relative "rufo/command"
14
15
  require_relative "rufo/formatter"
@@ -0,0 +1,29 @@
1
+ module Rufo::Backport
2
+ # Implement Enumerable#chunk_while
3
+ # if it's not available in the current Ruby version
4
+ def self.chunk_while(array)
5
+ results = []
6
+ current = []
7
+ first = true
8
+ last = nil
9
+
10
+ array.each do |elem|
11
+ if first
12
+ current << elem
13
+ first = false
14
+ else
15
+ if yield(last, elem)
16
+ current << elem
17
+ else
18
+ results << current
19
+ current = [elem]
20
+ end
21
+ end
22
+ last = elem
23
+ end
24
+
25
+ results << current unless current.empty?
26
+
27
+ results
28
+ end
29
+ end
@@ -106,10 +106,15 @@ class Rufo::Formatter
106
106
  # [:void_stmt]
107
107
  skip_space_or_newline
108
108
  when :@int
109
- # Number literal
109
+ # Integer literal
110
110
  #
111
111
  # [:@int, "123", [1, 0]]
112
112
  consume_token :on_int
113
+ when :@float
114
+ # Float literal
115
+ #
116
+ # [:@int, "123.45", [1, 0]]
117
+ consume_token :on_float
113
118
  when :@gvar
114
119
  # [:@gvar, "$abc", [1, 0]]
115
120
  write node[1]
@@ -118,7 +123,7 @@ class Rufo::Formatter
118
123
  # [:@backref, "$1", [1, 0]]
119
124
  write node[1]
120
125
  next_token
121
- when :string_literal
126
+ when :string_literal, :xstring_literal
122
127
  visit_string_literal node
123
128
  when :string_concat
124
129
  visit_string_concat node
@@ -141,6 +146,8 @@ class Rufo::Formatter
141
146
  visit_string_interpolation node
142
147
  when :symbol_literal
143
148
  visit_symbol_literal(node)
149
+ when :symbol
150
+ visit_symbol(node)
144
151
  when :dyna_symbol
145
152
  visit_quoted_symbol_literal(node)
146
153
  when :@ident
@@ -306,6 +313,10 @@ class Rufo::Formatter
306
313
  visit_super(node)
307
314
  when :defined
308
315
  visit_defined(node)
316
+ when :alias
317
+ visit_alias(node)
318
+ when :mlhs_add_star
319
+ visit_mlhs_add_star(node)
309
320
  else
310
321
  bug "Unhandled node: #{node.first}"
311
322
  end
@@ -379,6 +390,8 @@ class Rufo::Formatter
379
390
  @heredocs << [@current_call, node, tilde]
380
391
  return
381
392
  end
393
+ elsif current_token_kind == :on_backtick
394
+ consume_token :on_backtick
382
395
  else
383
396
  consume_token :on_tstring_beg
384
397
  end
@@ -393,9 +406,12 @@ class Rufo::Formatter
393
406
  end
394
407
 
395
408
  def visit_string_literal_end(node)
396
- visit_exps(node[1][1..-1], false, false)
409
+ inner = node[1]
410
+ inner = inner[1..-1] unless node[0] == :xstring_literal
411
+ visit_exps(inner, false, false)
397
412
 
398
- if current_token_kind == :on_heredoc_end
413
+ case current_token_kind
414
+ when :on_heredoc_end
399
415
  heredoc, tilde = @current_heredoc
400
416
  if heredoc && tilde
401
417
  write_indent
@@ -404,6 +420,8 @@ class Rufo::Formatter
404
420
  write current_token_value.rstrip
405
421
  end
406
422
  next_token
423
+ when :on_backtick
424
+ consume_token :on_backtick
407
425
  else
408
426
  consume_token :on_tstring_end
409
427
  end
@@ -432,8 +450,19 @@ class Rufo::Formatter
432
450
  # :foo
433
451
  #
434
452
  # [:symbol_literal, [:symbol, [:@ident, "foo", [1, 1]]]]
453
+ #
454
+ # A symbol literal not necessarily begins with `:`.
455
+ # For example, an `alias foo bar` will treat `foo`
456
+ # a as symbol_literal but without a `:symbol` child.
457
+ visit node[1]
458
+ end
459
+
460
+ def visit_symbol(node)
461
+ # :foo
462
+ #
463
+ # [:symbol, [:@ident, "foo", [1, 1]]]
435
464
  consume_token :on_symbeg
436
- visit_exps node[1][1..-1], false, false
465
+ visit_exps node[1..-1], false, false
437
466
  end
438
467
 
439
468
  def visit_quoted_symbol_literal(node)
@@ -586,7 +615,7 @@ class Rufo::Formatter
586
615
  # exp rescue handler
587
616
  #
588
617
  # [:if_mod, cond, body]
589
- _, cond, body = node
618
+ _, body, cond = node
590
619
 
591
620
  visit body
592
621
  consume_space
@@ -1052,9 +1081,10 @@ class Rufo::Formatter
1052
1081
  end
1053
1082
 
1054
1083
  def visit_comma_separated_list(nodes, inside_call = false)
1055
- # This is `x, *y, z` on the left hand side of an assignment
1056
- if nodes[0] == :mlhs_add_star
1057
- visit_mlhs_add_star(nodes)
1084
+ # When there's *x inside a left hand side assignment
1085
+ # or a case when, it comes as [:op, ...]
1086
+ if nodes[0].is_a?(Symbol)
1087
+ visit nodes
1058
1088
  return
1059
1089
  end
1060
1090
 
@@ -1358,10 +1388,12 @@ class Rufo::Formatter
1358
1388
 
1359
1389
  if rest_param
1360
1390
  # [:rest_param, [:@ident, "x", [1, 15]]]
1391
+ _, rest = rest_param
1392
+
1361
1393
  write_params_comma if needs_comma
1362
1394
  consume_op "*"
1363
1395
  skip_space_or_newline
1364
- visit rest_param[1]
1396
+ visit rest if rest
1365
1397
  needs_comma = true
1366
1398
  end
1367
1399
 
@@ -1392,7 +1424,9 @@ class Rufo::Formatter
1392
1424
  write_params_comma if needs_comma
1393
1425
  consume_op "**"
1394
1426
  skip_space_or_newline
1395
- visit double_star_param
1427
+
1428
+ # A nameless double star comes as an... Integer? :-S
1429
+ visit double_star_param if double_star_param.is_a?(Array)
1396
1430
  skip_space_or_newline
1397
1431
  needs_comma = true
1398
1432
  end
@@ -1461,7 +1495,7 @@ class Rufo::Formatter
1461
1495
  # For %W it seems elements appear inside other arrays
1462
1496
  # for some reason, so we flatten them
1463
1497
  if elements[0].is_a?(Array) && elements[0][0].is_a?(Array)
1464
- elements = elements.flat_map(&:itself)
1498
+ elements = elements.flat_map { |x| x }
1465
1499
  end
1466
1500
 
1467
1501
  write current_token_value.strip
@@ -1631,21 +1665,26 @@ class Rufo::Formatter
1631
1665
 
1632
1666
  skip_space
1633
1667
 
1634
- if newline? || comment?
1635
- needed_indent = next_indent
1636
- if args
1637
- consume_end_of_line
1638
- write_indent(needed_indent)
1668
+ # Sometimes args comes with an array...
1669
+ if args && args[0].is_a?(Array)
1670
+ visit_literal_elements args
1671
+ else
1672
+ if newline? || comment?
1673
+ needed_indent = next_indent
1674
+ if args
1675
+ consume_end_of_line
1676
+ write_indent(needed_indent)
1677
+ else
1678
+ skip_space_or_newline
1679
+ end
1639
1680
  else
1640
- skip_space_or_newline
1681
+ needed_indent = column
1641
1682
  end
1642
- else
1643
- needed_indent = column
1644
- end
1645
1683
 
1646
- if args
1647
- indent(needed_indent) do
1648
- visit args
1684
+ if args
1685
+ indent(needed_indent) do
1686
+ visit args
1687
+ end
1649
1688
  end
1650
1689
  end
1651
1690
 
@@ -1856,6 +1895,17 @@ class Rufo::Formatter
1856
1895
  end
1857
1896
  end
1858
1897
 
1898
+ def visit_alias(node)
1899
+ # [:alias, from, to]
1900
+ _, from, to = node
1901
+
1902
+ consume_keyword "alias"
1903
+ consume_space
1904
+ visit from
1905
+ consume_space
1906
+ visit to
1907
+ end
1908
+
1859
1909
  def visit_literal_elements(elements)
1860
1910
  base_column = @column
1861
1911
 
@@ -2498,7 +2548,7 @@ class Rufo::Formatter
2498
2548
  elements.reject! { |l, c, indent, id, off, ignore| ignore == :ignore }
2499
2549
 
2500
2550
  # Chunk comments that are in consecutive lines
2501
- chunks = elements.chunk_while do |(l1, c1, i1, id1), (l2, c2, i2, id2)|
2551
+ chunks = chunk_while(elements) do |(l1, c1, i1, id1), (l2, c2, i2, id2)|
2502
2552
  l1 + 1 == l2 && i1 == i2 && id1 == id2
2503
2553
  end
2504
2554
 
@@ -2526,6 +2576,14 @@ class Rufo::Formatter
2526
2576
  @output = lines.join
2527
2577
  end
2528
2578
 
2579
+ def chunk_while(array, &block)
2580
+ if array.respond_to?(:chunk_while)
2581
+ array.chunk_while(&block)
2582
+ else
2583
+ Rufo::Backport.chunk_while(array, &block)
2584
+ end
2585
+ end
2586
+
2529
2587
  def result
2530
2588
  @output
2531
2589
  end
@@ -1,3 +1,3 @@
1
1
  module Rufo
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
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.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ary Borenszweig
@@ -72,6 +72,7 @@ files:
72
72
  - bin/setup
73
73
  - exe/rufo
74
74
  - lib/rufo.rb
75
+ - lib/rufo/backport.rb
75
76
  - lib/rufo/command.rb
76
77
  - lib/rufo/formatter.rb
77
78
  - lib/rufo/version.rb