rufo 0.0.16 → 0.0.17

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
  SHA1:
3
- metadata.gz: 4ff839772b1723bf26c212207ec9f3d1f597e3fb
4
- data.tar.gz: cd06f569d4abe3e4f1e7420be35b621e7707111a
3
+ metadata.gz: 50d15a24f09ec9bd78016c6e572736daae323c7c
4
+ data.tar.gz: 3043fa80ffc0e2d5693f64ae66f0d9778fa60216
5
5
  SHA512:
6
- metadata.gz: '07186d0a3e149837f9f0397e0553827d7de865147f41c8c112b5c8983594cbbae4d76f2a829be4d1c97afb0c22f8a20749dca9fc11d506789ba6e05b8dab5328'
7
- data.tar.gz: 479ab2c8a97ad8bb926481677a035e2130bc546e4ac4a0050e6c324e521bd4ef8363a0f795a82b123c7dd61617c5f658ef3a41f36a9918a587e7bd551f9ae80a
6
+ metadata.gz: 43d21b3b6966469feaf5eea5179de28248b973669b994a57d3a0fb84204c09cfee06edc88aab5d84a16f6dc7e1a953aa12603ede252cf07e7aacf1fec20bc3f1
7
+ data.tar.gz: 0a08b865224b3ae14c74aca6898790b9aa5bc8d094fe3423d90bce186b84eb26400041a53f610b74c5b28b7d30dc432b2b47b9a0476818c25350d0fb51be295e
data/.rufo CHANGED
@@ -1,5 +1,4 @@
1
1
  align_comments true
2
2
  align_assignments true
3
3
  align_hash_keys true
4
- convert_brace_to_do true
5
4
  indent_size 2
data/.travis.yml CHANGED
@@ -1,5 +1,8 @@
1
1
  sudo: false
2
2
  language: ruby
3
+ cache: bundler
3
4
  rvm:
4
5
  - 2.4.1
5
6
  before_install: gem install bundler -v 1.15.1
7
+ script:
8
+ - bundle exec rake ci
data/README.md CHANGED
@@ -67,9 +67,6 @@ align_assignments true
67
67
  # Whether to align successive hash keys (default: true)
68
68
  align_hash_keys true
69
69
 
70
- # Whether to convert multiline `{ ... }` block to `do ... end` (default: true)
71
- convert_brace_to_do true
72
-
73
70
  # The indent size (default: 2)
74
71
  indent_size 2
75
72
  ```
@@ -90,7 +87,7 @@ Rufo is a **real** formatter, not a simple find and replace one. It works by emp
90
87
  a Ruby parser and a Ruby lexer. The parser is used for the shape of the program. The program
91
88
  is traversed and the lexer is used to sync this structure to tokens. This is why comments
92
89
  can be handled well, because they are provided by the lexer (comments are not returned by
93
- a parser).
90
+ a parser).
94
91
 
95
92
  To parse and lex, [Ripper](https://ruby-doc.org/stdlib-2.4.0/libdoc/ripper/rdoc/Ripper.html) is used.
96
93
 
data/exe/rufo CHANGED
@@ -1,4 +1,4 @@
1
1
  #!/usr/bin/env ruby
2
2
  require "rufo"
3
3
 
4
- Rufo::Command.run
4
+ Rufo::Command.run(ARGV)
data/lib/rufo/backport.rb CHANGED
@@ -4,8 +4,8 @@ module Rufo::Backport
4
4
  def self.chunk_while(array)
5
5
  results = []
6
6
  current = []
7
- first = true
8
- last = nil
7
+ first = true
8
+ last = nil
9
9
 
10
10
  array.each do |elem|
11
11
  if first
@@ -26,4 +26,4 @@ module Rufo::Backport
26
26
 
27
27
  results
28
28
  end
29
- end
29
+ end
data/lib/rufo/command.rb CHANGED
@@ -1,13 +1,13 @@
1
1
  require "optionparser"
2
2
 
3
3
  module Rufo::Command
4
- def self.run
5
- want_check = parse_options
4
+ def self.run(argv)
5
+ want_check = parse_options(argv)
6
6
 
7
- if ARGV.empty?
7
+ if argv.empty?
8
8
  format_stdin want_check
9
9
  else
10
- format_args ARGV, want_check
10
+ format_args argv, want_check
11
11
  end
12
12
  end
13
13
 
@@ -55,7 +55,15 @@ module Rufo::Command
55
55
 
56
56
  def self.format_file(filename, want_check)
57
57
  code = File.read(filename)
58
- result = format(code, File.dirname(filename))
58
+
59
+ begin
60
+ result = format(code, File.dirname(filename))
61
+ rescue Rufo::SyntaxError
62
+ # We ignore syntax errors as these might be template files
63
+ # with .rb extension
64
+ STDERR.puts "Error: #{filename} has syntax errors"
65
+ return true
66
+ end
59
67
 
60
68
  if code != result
61
69
  if want_check
@@ -110,7 +118,7 @@ module Rufo::Command
110
118
  find_dot_rufo(parent_dir)
111
119
  end
112
120
 
113
- def self.parse_options
121
+ def self.parse_options(argv)
114
122
  want_check = false
115
123
 
116
124
  OptionParser.new do |opts|
@@ -124,7 +132,7 @@ module Rufo::Command
124
132
  puts opts
125
133
  exit
126
134
  end
127
- end.parse!
135
+ end.parse!(argv)
128
136
 
129
137
  want_check
130
138
  end
@@ -26,7 +26,7 @@ class Rufo::Formatter
26
26
  # calls to that dot
27
27
  @dot_column = nil
28
28
 
29
- # Heredocs list, associated with calls ([call, heredoc, tilde])
29
+ # Heredocs list, associated with calls ([heredoc, tilde])
30
30
  @heredocs = []
31
31
 
32
32
  # Current node, to be able to associate it to heredocs
@@ -55,7 +55,6 @@ class Rufo::Formatter
55
55
  # Settings
56
56
  @indent_size = options.fetch(:indent_size, 2)
57
57
  @align_comments = options.fetch(:align_comments, true)
58
- @convert_brace_to_do = options.fetch(:convert_brace_to_do, true)
59
58
  @align_assignments = options.fetch(:align_assignments, true)
60
59
  @align_hash_keys = options.fetch(:align_hash_keys, true)
61
60
  end
@@ -70,12 +69,6 @@ class Rufo::Formatter
70
69
  @align_comments = value
71
70
  end
72
71
 
73
- # Whether to convert multiline `{ ... }` block
74
- # to `do ... end` (default: true)
75
- def convert_brace_to_do(value)
76
- @convert_brace_to_do = value
77
- end
78
-
79
72
  # Whether to align successive assignments (default: true)
80
73
  def align_assignments(value)
81
74
  @align_assignments = value
@@ -143,6 +136,9 @@ class Rufo::Formatter
143
136
  # [:@backref, "$1", [1, 0]]
144
137
  write node[1]
145
138
  next_token
139
+ when :@backtick
140
+ # [:@backtick, "`", [1, 4]]
141
+ consume_token :on_backtick
146
142
  when :string_literal, :xstring_literal
147
143
  visit_string_literal node
148
144
  when :string_concat
@@ -399,7 +395,7 @@ class Rufo::Formatter
399
395
 
400
396
  skip_space
401
397
  if newline? || comment?
402
- check_heredocs_at_call_end(exp)
398
+ check_pending_heredocs
403
399
  end
404
400
 
405
401
  line_before_endline = @line
@@ -437,14 +433,12 @@ class Rufo::Formatter
437
433
  next_token
438
434
  skip_space
439
435
 
440
- # A comma after a heredoc means the heredoc contents
441
- # come after an argument list, so put it in a list
442
- # for later.
443
- # The same happens if we already have a heredoc in
444
- # the list, which means this will come after other
445
- # heredocs.
446
- if comma? || (current_token_kind == :on_period) || !@heredocs.empty?
447
- @heredocs << [@current_node, node, tilde]
436
+ # If something other than a newline follows the heredoc
437
+ # beginning it means some other code follows and
438
+ # we have to accumulate the heredoc and print it
439
+ # later, when the line ends.
440
+ if !newline? || !@heredocs.empty?
441
+ @heredocs << [node, tilde]
448
442
  return
449
443
  end
450
444
  elsif current_token_kind == :on_backtick
@@ -748,7 +742,11 @@ class Rufo::Formatter
748
742
  skip_space_or_newline
749
743
  end
750
744
 
751
- visit call
745
+ if call == :call
746
+ # :call means it's .()
747
+ else
748
+ visit call
749
+ end
752
750
 
753
751
  # Only set it after we visit the call after the dot,
754
752
  # so we remember the outmost dot position
@@ -843,8 +841,6 @@ class Rufo::Formatter
843
841
  end
844
842
 
845
843
  consume_token :on_rparen
846
-
847
- check_heredocs_at_call_end(node)
848
844
  end
849
845
 
850
846
  def visit_command(node)
@@ -871,16 +867,13 @@ class Rufo::Formatter
871
867
  push_call(node) do
872
868
  visit_command_args(args)
873
869
  end
874
-
875
- check_heredocs_at_call_end(node)
876
870
  end
877
871
 
878
- def check_heredocs_at_call_end(node)
872
+ def check_pending_heredocs
879
873
  printed = false
880
874
 
881
875
  until @heredocs.empty?
882
- scope, heredoc, tilde = @heredocs.first
883
- break unless scope.equal?(node)
876
+ heredoc, tilde = @heredocs.first
884
877
 
885
878
  # Need to print a line between consecutive heredoc ends
886
879
  write_line if printed
@@ -960,7 +953,7 @@ class Rufo::Formatter
960
953
  return
961
954
  end
962
955
 
963
- closing_brace_token = find_closing_brace_token
956
+ closing_brace_token, index = find_closing_brace_token
964
957
 
965
958
  # If the whole block fits into a single line, use braces
966
959
  if current_token[0][0] == closing_brace_token[0][0]
@@ -976,31 +969,12 @@ class Rufo::Formatter
976
969
  return
977
970
  end
978
971
 
979
- # Otherwise, use `do` (if told so)
980
- check :on_lbrace
981
-
982
- if @convert_brace_to_do
983
- write "do"
984
- else
985
- write "{"
986
- end
987
-
988
- next_token
989
-
972
+ # Otherwise it's multiline
973
+ consume_token :on_lbrace
990
974
  consume_block_args args
991
-
992
975
  indent_body body
993
-
994
976
  write_indent
995
-
996
- check :on_rbrace
997
- next_token
998
-
999
- if @convert_brace_to_do
1000
- write "end"
1001
- else
1002
- write "}"
1003
- end
977
+ consume_token :on_rbrace
1004
978
  end
1005
979
 
1006
980
  def visit_do_block(node)
@@ -1029,16 +1003,29 @@ class Rufo::Formatter
1029
1003
  end
1030
1004
 
1031
1005
  def visit_block_arguments(node)
1032
- # [:block_var, params, ??]
1033
- _, params = node
1006
+ # [:block_var, params, blockarg]
1007
+ _, params, blockarg = node
1034
1008
 
1035
- consume_op "|"
1036
- skip_space_or_newline
1009
+ check :on_op
1037
1010
 
1038
- visit params
1011
+ # check for ||
1012
+ if blockarg.nil?
1013
+ # Don't write || as it's meaningless
1014
+ next_token
1015
+ else
1016
+ next_token
1017
+ skip_space_or_newline
1039
1018
 
1040
- skip_space_or_newline
1041
- consume_op "|"
1019
+ # This means it's an empty | |, so we remove it
1020
+ if current_token_kind == :on_op && current_token_value == "|"
1021
+ next_token
1022
+ else
1023
+ write "|"
1024
+ visit params
1025
+ skip_space_or_newline
1026
+ consume_op "|"
1027
+ end
1028
+ end
1042
1029
  end
1043
1030
 
1044
1031
  def visit_call_args(node)
@@ -1156,13 +1143,15 @@ class Rufo::Formatter
1156
1143
  # Multiple exception types
1157
1144
  # [:mrhs_new_from_args, exps, final_exp]
1158
1145
  _, exps, final_exp = node
1146
+
1159
1147
  if final_exp
1160
- nodes = [*node[1], node[2]]
1161
- visit_comma_separated_list(nodes)
1148
+ visit_comma_separated_list exps
1149
+ write_params_comma
1150
+ visit final_exp
1162
1151
  elsif exps[0].is_a?(Symbol)
1163
1152
  visit exps
1164
1153
  else
1165
- visit_exps exps, false, false
1154
+ visit_comma_separated_list exps
1166
1155
  end
1167
1156
  end
1168
1157
 
@@ -1251,7 +1240,7 @@ class Rufo::Formatter
1251
1240
  consume_keyword(keyword)
1252
1241
  consume_space
1253
1242
 
1254
- closing_brace_token = find_closing_brace_token
1243
+ closing_brace_token, _index = find_closing_brace_token
1255
1244
 
1256
1245
  # If the whole block fits into a single line, format
1257
1246
  # in a single line
@@ -1330,7 +1319,7 @@ class Rufo::Formatter
1330
1319
  consume_op "*"
1331
1320
  skip_space_or_newline
1332
1321
 
1333
- visit star
1322
+ visit star if star
1334
1323
 
1335
1324
  if after && !after.empty?
1336
1325
  write_params_comma
@@ -1345,11 +1334,11 @@ class Rufo::Formatter
1345
1334
  consume_op_or_keyword op
1346
1335
 
1347
1336
  if op == :not
1348
- consume_space
1337
+ consume_space
1349
1338
  else
1350
1339
  skip_space_or_newline
1351
1340
  end
1352
-
1341
+
1353
1342
  visit exp
1354
1343
  end
1355
1344
 
@@ -1584,14 +1573,18 @@ class Rufo::Formatter
1584
1573
  end
1585
1574
 
1586
1575
  if rest_param
1587
- # [:rest_param, [:@ident, "x", [1, 15]]]
1588
- _, rest = rest_param
1589
-
1590
- write_params_comma if needs_comma
1591
- consume_op "*"
1592
- skip_space_or_newline
1593
- visit rest if rest
1594
- needs_comma = true
1576
+ # check for trailing , |x, |
1577
+ if rest_param == 0
1578
+ write_params_comma
1579
+ else
1580
+ # [:rest_param, [:@ident, "x", [1, 15]]]
1581
+ _, rest = rest_param
1582
+ write_params_comma if needs_comma
1583
+ consume_op "*"
1584
+ skip_space_or_newline
1585
+ visit rest if rest
1586
+ needs_comma = true
1587
+ end
1595
1588
  end
1596
1589
 
1597
1590
  if post_rest_params
@@ -1672,6 +1665,7 @@ class Rufo::Formatter
1672
1665
 
1673
1666
  if elements
1674
1667
  if elements[0].is_a?(Symbol)
1668
+ skip_space_or_newline
1675
1669
  visit elements
1676
1670
  skip_space_or_newline
1677
1671
  else
@@ -2024,7 +2018,7 @@ class Rufo::Formatter
2024
2018
  brace = current_token_value == "{"
2025
2019
 
2026
2020
  if brace
2027
- closing_brace_token = find_closing_brace_token
2021
+ closing_brace_token, index = find_closing_brace_token
2028
2022
 
2029
2023
  # Check if the whole block fits into a single line
2030
2024
  if current_token[0][0] == closing_brace_token[0][0]
@@ -2321,6 +2315,7 @@ class Rufo::Formatter
2321
2315
 
2322
2316
  indent(@column) do
2323
2317
  visit_comma_separated_list conds
2318
+ skip_space
2324
2319
  end
2325
2320
 
2326
2321
  then_keyword = keyword?("then")
@@ -2486,9 +2481,9 @@ class Rufo::Formatter
2486
2481
  # - want_semicolon: do we want do print a semicolon to separate expressions?
2487
2482
  # - want_multiline: do we want multiple lines to appear, or at most one?
2488
2483
  def consume_end_of_line(at_prefix = false, want_semicolon = false, want_multiline = true)
2489
- found_newline = false # Did we find any newline during this method?
2490
- last = nil # Last token kind found
2491
- multilple_lines = false # Did we pass through more than one newline?
2484
+ found_newline = false # Did we find any newline during this method?
2485
+ last = nil # Last token kind found
2486
+ multilple_lines = false # Did we pass through more than one newline?
2492
2487
  last_comment_has_newline = false # Does the last comment has a newline?
2493
2488
 
2494
2489
  while true
@@ -2501,7 +2496,7 @@ class Rufo::Formatter
2501
2496
  # can appear with nil as the "text", and that's wrong
2502
2497
  if current_token[2] == nil
2503
2498
  next_token
2504
- next
2499
+ next
2505
2500
  end
2506
2501
 
2507
2502
  if last == :newline
@@ -2563,7 +2558,7 @@ class Rufo::Formatter
2563
2558
  write_line if multilple_lines
2564
2559
 
2565
2560
  consume_embedded_comment
2566
- last = :comment
2561
+ last = :comment
2567
2562
  last_comment_has_newline = true
2568
2563
  else
2569
2564
  break
@@ -2755,19 +2750,39 @@ class Rufo::Formatter
2755
2750
 
2756
2751
  def find_closing_brace_token
2757
2752
  count = 0
2758
- @tokens.reverse_each do |token|
2753
+ i = @tokens.size - 1
2754
+ while i >= 0
2755
+ token = @tokens[i]
2759
2756
  (line, column), kind = token
2760
2757
  case kind
2761
2758
  when :on_lbrace, :on_tlambeg
2762
2759
  count += 1
2763
2760
  when :on_rbrace
2764
2761
  count -= 1
2765
- return token if count == 0
2762
+ return [token, i] if count == 0
2766
2763
  end
2764
+ i -= 1
2767
2765
  end
2768
2766
  nil
2769
2767
  end
2770
2768
 
2769
+ def newline_follows_token(index)
2770
+ index -= 1
2771
+ while index >= 0
2772
+ token = @tokens[index]
2773
+ case current_token_kind
2774
+ when :on_sp
2775
+ # OK
2776
+ when :on_nl, :on_ignored_nl
2777
+ return true
2778
+ else
2779
+ return false
2780
+ end
2781
+ index -= 1
2782
+ end
2783
+ true
2784
+ end
2785
+
2771
2786
  def next_token
2772
2787
  @tokens.pop
2773
2788
  end
data/lib/rufo/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Rufo
2
- VERSION = "0.0.16"
2
+ VERSION = "0.0.17"
3
3
  end
data/rakelib/ci.rake ADDED
@@ -0,0 +1,4 @@
1
+ desc "Run tasks on CI"
2
+ task :ci => :spec do
3
+ Rake::Task["rufo:check"].invoke("lib spec")
4
+ end
data/rakelib/rufo.rake ADDED
@@ -0,0 +1,22 @@
1
+ desc "Alias for `rake rufo:run`"
2
+ task :rufo => ["rufo:run"]
3
+
4
+ namespace :rufo do
5
+ require "rufo"
6
+
7
+ def rufo_command(*switches, rake_args)
8
+ files_or_dirs = rake_args[:files_or_dirs] || "."
9
+ args = switches + files_or_dirs.split(" ")
10
+ Rufo::Command.run(args)
11
+ end
12
+
13
+ desc "Format Ruby code in current directory"
14
+ task :run, [:files_or_dirs] do |task, rake_args|
15
+ rufo_command(rake_args)
16
+ end
17
+
18
+ desc "Check that no formatting changes are produced"
19
+ task :check, [:files_or_dirs] do |task, rake_args|
20
+ rufo_command("--check", rake_args)
21
+ end
22
+ 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.16
4
+ version: 0.0.17
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ary Borenszweig
@@ -76,6 +76,8 @@ files:
76
76
  - lib/rufo/command.rb
77
77
  - lib/rufo/formatter.rb
78
78
  - lib/rufo/version.rb
79
+ - rakelib/ci.rake
80
+ - rakelib/rufo.rake
79
81
  - rufo.gemspec
80
82
  homepage: https://github.com/asterite/rufo
81
83
  licenses: