rufo 0.4.2 → 0.5.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: db1dcca141aa241d1f18e99eb17cab04fe26a2f922fbcd0272c371b43f62459d
4
- data.tar.gz: 5319a68334194f410d607a24425584ed114c9fb775908fa9f09eb37101d31da8
3
+ metadata.gz: dc9f1ff616a0e805085d05d0f2fcb92bd8c1b0887e5aedaff75a94a33ee3b0a7
4
+ data.tar.gz: 9525bb1291e062e4822105b95cce321b89ef06013a05bc5c444be04ddf1ae24e
5
5
  SHA512:
6
- metadata.gz: 9ba69ebe856e99f8a0a2d1bd467b0b61e1e12f7d0b3c0dd26d4e0de2b2d4adb5804fdf864904dcf5bccc05afe2e5607f67a4f8be28d9dc5d5004b955ff76d96c
7
- data.tar.gz: 276167c342eeddcf7e45608a9219029216081c34a0021aeb35504d4239078ac0a8f31f667d255a76fb384ee7d801857d64954ac7c8abebb0d37bec8120039554
6
+ metadata.gz: 5de0e49f1d959f51383602712f54c3c9e1dc947583f99c074c0524989ec8b2971158187b08d5c0bd85fdd982f0db327d2246ed4bfab0f37f9b76b2581dd0d5d2
7
+ data.tar.gz: 7ea03c65b2744b23fadc20ebd5021ec964997755d6d299ab92cdb5f31a1eaa07df00f53a72bcdbda38370a3918d7f2f3d9d0f489c51007ae0a920fe1b8b8c460
@@ -0,0 +1,92 @@
1
+ "-": &dockerbuild
2
+ steps:
3
+ - checkout
4
+
5
+ # Which version of ruby?
6
+ - run:
7
+ name: Which ruby?
8
+ command: ruby --version | tee ruby-version-for-ci.txt
9
+
10
+ # Which version of bundler?
11
+ - run:
12
+ name: Which bundler?
13
+ command: bundle -v
14
+
15
+ # Restore bundle cache
16
+ - restore_cache:
17
+ keys:
18
+ - bundler-packages-{{ checksum "ruby-version-for-ci.txt" }}-{{ checksum "rufo.gemspec" }}
19
+
20
+ - run:
21
+ name: Bundle Install
22
+ command: bundle check || bundle install
23
+
24
+ # Store bundle cache
25
+ - save_cache:
26
+ key: bundler-packages-{{ checksum "ruby-version-for-ci.txt" }}-{{ checksum "rufo.gemspec" }}
27
+ paths:
28
+ - vendor/bundle
29
+ - Gemfile.lock
30
+ - run:
31
+ name: Run rspec
32
+ command: |
33
+ bundle exec rspec --profile 10 \
34
+ --format RspecJunitFormatter \
35
+ --out test_results/rspec.xml \
36
+ --format progress
37
+ - run:
38
+ name: Run RuboCop
39
+ command: |
40
+ bundle exec rake rubocop
41
+ - run:
42
+ name: Run Rufo
43
+ command: |
44
+ bundle exec rake rufo:run['lib spec exe/rufo rakelib Rakefile rufo.gemspec']
45
+
46
+ # Save test results for timing analysis
47
+ - store_test_results:
48
+ path: test_results
49
+
50
+
51
+ version: 2
52
+ jobs:
53
+ build-2-6-1:
54
+ <<: *dockerbuild
55
+ docker:
56
+ - image: circleci/ruby:2.6.1
57
+ environment:
58
+ BUNDLE_JOBS: "3"
59
+ BUNDLE_RETRY: "3"
60
+ BUNDLE_PATH: vendor/bundle
61
+ build-2-5-1:
62
+ <<: *dockerbuild
63
+ docker:
64
+ - image: circleci/ruby:2.5.1
65
+ environment:
66
+ BUNDLE_JOBS: "3"
67
+ BUNDLE_RETRY: "3"
68
+ BUNDLE_PATH: vendor/bundle
69
+ build-2-4-4:
70
+ <<: *dockerbuild
71
+ docker:
72
+ - image: circleci/ruby:2.4.4
73
+ environment:
74
+ BUNDLE_JOBS: "3"
75
+ BUNDLE_RETRY: "3"
76
+ BUNDLE_PATH: vendor/bundle
77
+ build-2-3-7:
78
+ <<: *dockerbuild
79
+ docker:
80
+ - image: circleci/ruby:2.3.7
81
+ environment:
82
+ BUNDLE_JOBS: "3"
83
+ BUNDLE_RETRY: "3"
84
+ BUNDLE_PATH: vendor/bundle
85
+ workflows:
86
+ version: 2
87
+ test:
88
+ jobs:
89
+ - build-2-6-1
90
+ - build-2-5-1
91
+ - build-2-4-4
92
+ - build-2-3-7
@@ -0,0 +1,15 @@
1
+ AllCops:
2
+ Exclude:
3
+ - "spec/**/*"
4
+ - "vendor/**/*"
5
+ TargetRubyVersion: 2.3
6
+
7
+ Layout:
8
+ Enabled: false
9
+
10
+ Metrics:
11
+ Enabled: false
12
+
13
+ Style:
14
+ Enabled: false
15
+
@@ -10,6 +10,18 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
10
10
 
11
11
  ### Added
12
12
 
13
+ ## [0.5.0] - 2019-02-09
14
+
15
+ ### Added
16
+
17
+ - Add space inside hash literal braces when contents are on the same line. `{a: 1} => { a: 1 }`. This brings Rufo inline with:
18
+ - [RuboCop](https://www.rubydoc.info/gems/rubocop/RuboCop/Cop/Layout/SpaceInsideHashLiteralBraces)
19
+ - [Prettier](https://prettier.io/docs/en/options.html#bracket-spacing)
20
+ - [RSpec core](https://github.com/rspec/rspec-core/blob/7b6b9c3f2e2878213f97d6fc9e9eb23c323cfe1c/lib/rspec/core/bisect/shell_command.rb#L49)
21
+ - [Rails activemodel](https://github.com/rails/rails/blob/master/activemodel/lib/active_model/validations/acceptance.rb#L7)
22
+ - [Devise](https://github.com/plataformatec/devise/blob/master/app/controllers/devise/sessions_controller.rb#L44)
23
+ - Format gem and Rake related files.
24
+
13
25
  ## [0.4.2] - 2019-01-22
14
26
 
15
27
  ### Fixed
data/Rakefile CHANGED
@@ -1,6 +1,8 @@
1
1
  require "bundler/gem_tasks"
2
2
  require "rspec/core/rake_task"
3
+ require "rubocop/rake_task"
3
4
 
4
5
  RSpec::Core::RakeTask.new(:spec)
6
+ RuboCop::RakeTask.new
5
7
 
6
8
  task :default => :spec
@@ -10,9 +10,9 @@ module Rufo
10
10
  end
11
11
  end
12
12
 
13
- require_relative "rufo/backport"
14
13
  require_relative "rufo/command"
15
14
  require_relative "rufo/dot_file"
16
15
  require_relative "rufo/settings"
17
16
  require_relative "rufo/formatter"
18
17
  require_relative "rufo/version"
18
+ require_relative "rufo/file_finder"
@@ -61,30 +61,28 @@ class Rufo::Command
61
61
  end
62
62
 
63
63
  def format_args(args)
64
- files = []
65
-
66
- args.each do |arg|
67
- if Dir.exist?(arg)
68
- files.concat Dir[File.join(arg, "**", "*.rb")].select(&File.method(:file?))
69
- elsif File.exist?(arg)
70
- files << arg
71
- else
72
- STDERR.puts "Error: file or directory not found: #{arg}"
73
- end
74
- end
75
-
76
- return CODE_ERROR if files.empty?
64
+ file_finder = Rufo::FileFinder.new(args)
65
+ files = file_finder.to_a
77
66
 
78
67
  changed = false
79
68
  syntax_error = false
69
+ files_exist = false
80
70
 
81
- files.each do |file|
71
+ files.each do |(exists, file)|
72
+ if exists
73
+ files_exist = true
74
+ else
75
+ STDERR.puts "Error: file or directory not found: #{file}"
76
+ next
77
+ end
82
78
  result = format_file(file)
83
79
 
84
80
  changed |= result == CODE_CHANGE
85
81
  syntax_error |= result == CODE_ERROR
86
82
  end
87
83
 
84
+ return CODE_ERROR unless files_exist
85
+
88
86
  STDERR.puts squiggly_heredoc_warning unless @squiggly_warning_files.empty?
89
87
 
90
88
  case
@@ -95,16 +93,16 @@ class Rufo::Command
95
93
  end
96
94
 
97
95
  def squiggly_heredoc_warning
98
- <<-EOF
96
+ <<-WARNING
99
97
  Rufo Warning!
100
98
  File#{squiggly_pluralize} #{squiggly_warning_files} #{squiggly_pluralize(:has)} not been formatted due to a problem with Ruby version #{RUBY_VERSION}
101
99
  Please update to Ruby #{backported_version} to fix your formatting!
102
100
  See https://github.com/ruby-formatter/rufo/wiki/Squiggly-Heredocs for information.
103
- EOF
101
+ WARNING
104
102
  end
105
103
 
106
- def squiggly_pluralize(x = :s)
107
- idx = x == :s ? 0 : 1
104
+ def squiggly_pluralize(word = :s)
105
+ idx = word == :s ? 0 : 1
108
106
  (@squiggly_warning_files.length > 1 ? ["s", "have"] : ["", "has"])[idx]
109
107
  end
110
108
 
@@ -0,0 +1,36 @@
1
+ class Rufo::FileFinder
2
+ include Enumerable
3
+
4
+ # Taken from https://github.com/ruby/rake/blob/f0a897e3fb557f64f5da59785b1a4464826f77b2/lib/rake/application.rb#L41
5
+ RAKEFILES = [
6
+ "rakefile",
7
+ "Rakefile",
8
+ "rakefile.rb",
9
+ "Rakefile.rb",
10
+ ].join(",")
11
+
12
+ def initialize(files_or_dirs)
13
+ @files_or_dirs = files_or_dirs
14
+ end
15
+
16
+ def each
17
+ files_or_dirs.each do |file_or_dir|
18
+ if Dir.exist?(file_or_dir)
19
+ all_rb_files(file_or_dir).each { |file| yield [true, file] }
20
+ else
21
+ yield [File.exist?(file_or_dir), file_or_dir]
22
+ end
23
+ end
24
+ end
25
+
26
+ private
27
+
28
+ attr_reader :files_or_dirs
29
+
30
+ def all_rb_files(file_or_dir)
31
+ Dir.glob(
32
+ File.join(file_or_dir, "**", "{*.rb,Gemfile,*.gemspec,#{RAKEFILES},*.rake}"),
33
+ File::FNM_EXTGLOB
34
+ ).select(&File.method(:file?))
35
+ end
36
+ end
@@ -29,7 +29,7 @@ class Rufo::Formatter
29
29
  @line = 0
30
30
  @column = 0
31
31
  @last_was_newline = true
32
- @output = "".dup
32
+ @output = +""
33
33
 
34
34
  # The column of a `obj.method` call, so we can align
35
35
  # calls to that dot
@@ -343,7 +343,6 @@ class Rufo::Formatter
343
343
  visit_suffix(node, "rescue")
344
344
  when :vcall
345
345
  # [:vcall, exp]
346
- token_column = current_token_column
347
346
  visit node[1]
348
347
  when :fcall
349
348
  # [:fcall, [:@ident, "foo", [1, 0]]]
@@ -485,9 +484,9 @@ class Rufo::Formatter
485
484
  when :for
486
485
  visit_for(node)
487
486
  when :BEGIN
488
- visit_BEGIN(node)
487
+ visit_begin_node(node)
489
488
  when :END
490
- visit_END(node)
489
+ visit_end_node(node)
491
490
  else
492
491
  bug "Unhandled node: #{node.first}"
493
492
  end
@@ -677,7 +676,7 @@ class Rufo::Formatter
677
676
  #
678
677
  # This can happen with heredocs, but also with string literals spanning
679
678
  # multiple lines.
680
- def with_unmodifiable_string_lines(&block)
679
+ def with_unmodifiable_string_lines
681
680
  line = @line
682
681
  yield
683
682
  (line + 1..@line).each do |i|
@@ -724,7 +723,7 @@ class Rufo::Formatter
724
723
 
725
724
  visit string1
726
725
 
727
- has_backslash, first_space = skip_space_backslash
726
+ has_backslash, _ = skip_space_backslash
728
727
  if has_backslash
729
728
  write " \\"
730
729
  write_line
@@ -885,9 +884,7 @@ class Rufo::Formatter
885
884
  end
886
885
 
887
886
  def visit_assign_value(value)
888
- base_column = @column
889
-
890
- has_slash_newline, first_space = skip_space_backslash
887
+ has_slash_newline, _first_space = skip_space_backslash
891
888
 
892
889
  sticky = indentable_value?(value)
893
890
 
@@ -901,8 +898,7 @@ class Rufo::Formatter
901
898
  end
902
899
  else
903
900
  indent_after_space value, sticky: sticky,
904
- want_space: true,
905
- first_space: first_space
901
+ want_space: true
906
902
  end
907
903
  end
908
904
 
@@ -919,7 +915,7 @@ class Rufo::Formatter
919
915
  body = value[1]
920
916
  return false unless body[0] == :bodystmt
921
917
 
922
- _, body, rescue_body, else_body, ensure_body = body
918
+ _, _, rescue_body, else_body, ensure_body = body
923
919
  rescue_body || else_body || ensure_body
924
920
  else
925
921
  false
@@ -1000,7 +996,7 @@ class Rufo::Formatter
1000
996
 
1001
997
  def visit_call_with_receiver(node)
1002
998
  # [:call, obj, :".", name]
1003
- _, obj, text, name = node
999
+ _, obj, _, name = node
1004
1000
 
1005
1001
  @dot_column = nil
1006
1002
  visit obj
@@ -1224,13 +1220,12 @@ class Rufo::Formatter
1224
1220
  # :".",
1225
1221
  # name
1226
1222
  # [:args_add_block, [[:@int, "1", [1, 8]]], block]]
1227
- _, receiver, dot, name, args = node
1223
+ _, receiver, _, name, args = node
1228
1224
 
1229
1225
  base_column = current_token_column
1230
1226
 
1231
1227
  visit receiver
1232
1228
 
1233
- line = @line
1234
1229
  skip_space_or_newline
1235
1230
 
1236
1231
  # Remember dot column
@@ -1371,7 +1366,7 @@ class Rufo::Formatter
1371
1366
  return
1372
1367
  end
1373
1368
 
1374
- closing_brace_token, index = find_closing_brace_token
1369
+ closing_brace_token, _ = find_closing_brace_token
1375
1370
 
1376
1371
  # If the whole block fits into a single line, use braces
1377
1372
  if current_token_line == closing_brace_token[0][0]
@@ -1394,7 +1389,7 @@ class Rufo::Formatter
1394
1389
  consume_token :on_lbrace
1395
1390
  consume_block_args args
1396
1391
 
1397
- if call_info = @line_to_call_info[@line]
1392
+ if (call_info = @line_to_call_info[@line])
1398
1393
  call_info << true
1399
1394
  end
1400
1395
 
@@ -1469,7 +1464,6 @@ class Rufo::Formatter
1469
1464
  # Nothing
1470
1465
  elsif empty_params && local_params
1471
1466
  consume_token :on_semicolon
1472
- found_semicolon = true
1473
1467
  end
1474
1468
 
1475
1469
  skip_space_or_newline
@@ -1559,8 +1553,6 @@ class Rufo::Formatter
1559
1553
  # [:bodystmt, [[:@int, "1", [2, 1]]], nil, [[:@int, "2", [4, 1]]], nil] (2.6.0)
1560
1554
  _, body, rescue_body, else_body, ensure_body = node
1561
1555
 
1562
- inside_type_body = @inside_type_body
1563
- current_type = @current_type
1564
1556
  @inside_type_body = false
1565
1557
 
1566
1558
  line = @line
@@ -1726,15 +1718,15 @@ class Rufo::Formatter
1726
1718
  consume_keyword "end"
1727
1719
  end
1728
1720
 
1729
- def visit_BEGIN(node)
1730
- visit_BEGIN_or_END node, "BEGIN"
1721
+ def visit_begin_node(node)
1722
+ visit_begin_or_end node, "BEGIN"
1731
1723
  end
1732
1724
 
1733
- def visit_END(node)
1734
- visit_BEGIN_or_END node, "END"
1725
+ def visit_end_node(node)
1726
+ visit_begin_or_end node, "END"
1735
1727
  end
1736
1728
 
1737
- def visit_BEGIN_or_END(node, keyword)
1729
+ def visit_begin_or_end(node, keyword)
1738
1730
  # [:BEGIN, body]
1739
1731
  _, body = node
1740
1732
 
@@ -1800,7 +1792,7 @@ class Rufo::Formatter
1800
1792
  # then a star shouldn't be here... but if it is... handle it
1801
1793
  # somehow...
1802
1794
  if current_token_kind == :on_op && current_token_value == "*"
1803
- before, star, after = nil, before, after
1795
+ star = before
1804
1796
  else
1805
1797
  visit_comma_separated_list to_ary(before)
1806
1798
  write_params_comma
@@ -1848,9 +1840,7 @@ class Rufo::Formatter
1848
1840
  # [:unary, :-@, [:vcall, [:@ident, "x", [1, 2]]]]
1849
1841
  _, op, exp = node
1850
1842
 
1851
- consume_op_or_keyword op
1852
-
1853
- setting = op == :not ? :one : :no
1843
+ consume_op_or_keyword
1854
1844
 
1855
1845
  first_space = space?
1856
1846
  skip_space_or_newline
@@ -1881,7 +1871,7 @@ class Rufo::Formatter
1881
1871
 
1882
1872
  def visit_binary(node)
1883
1873
  # [:binary, left, op, right]
1884
- _, left, op, right = node
1874
+ _, left, _, right = node
1885
1875
 
1886
1876
  # If this binary is not at the beginning of a line, if there's
1887
1877
  # a newline following the op we want to align it with the left
@@ -1903,7 +1893,7 @@ class Rufo::Formatter
1903
1893
  visit left
1904
1894
  needs_space = space?
1905
1895
 
1906
- has_backslash, first_space = skip_space_backslash
1896
+ has_backslash, _ = skip_space_backslash
1907
1897
  if has_backslash
1908
1898
  needs_space = true
1909
1899
  write " \\"
@@ -1913,9 +1903,9 @@ class Rufo::Formatter
1913
1903
  write_space
1914
1904
  end
1915
1905
 
1916
- consume_op_or_keyword op
1906
+ consume_op_or_keyword
1917
1907
 
1918
- first_space = skip_space
1908
+ skip_space
1919
1909
 
1920
1910
  if newline? || comment?
1921
1911
  indent_after_space right,
@@ -1929,7 +1919,7 @@ class Rufo::Formatter
1929
1919
  end
1930
1920
  end
1931
1921
 
1932
- def consume_op_or_keyword(op)
1922
+ def consume_op_or_keyword
1933
1923
  case current_token_kind
1934
1924
  when :on_op, :on_kw
1935
1925
  write current_token_value
@@ -2005,7 +1995,7 @@ class Rufo::Formatter
2005
1995
  # [:@ident, "bar", [1, 9]],
2006
1996
  # [:params, nil, nil, nil, nil, nil, nil, nil],
2007
1997
  # [:bodystmt, [[:void_stmt]], nil, nil, nil]]
2008
- _, receiver, period, name, params, body = node
1998
+ _, receiver, _, name, params, body = node
2009
1999
 
2010
2000
  consume_keyword "def"
2011
2001
  consume_space
@@ -2026,7 +2016,7 @@ class Rufo::Formatter
2026
2016
 
2027
2017
  params = params[1] if params[0] == :paren
2028
2018
 
2029
- first_space = skip_space
2019
+ skip_space
2030
2020
 
2031
2021
  if current_token_kind == :on_lparen
2032
2022
  next_token
@@ -2309,11 +2299,14 @@ class Rufo::Formatter
2309
2299
  def visit_hash(node)
2310
2300
  # [:hash, elements]
2311
2301
  _, elements = node
2312
-
2313
2302
  token_column = current_token_column
2314
2303
 
2304
+ closing_brace_token, _ = find_closing_brace_token
2305
+ need_space = need_space_for_hash?(node, closing_brace_token)
2306
+
2315
2307
  check :on_lbrace
2316
2308
  write "{"
2309
+ write " " if need_space
2317
2310
  next_token
2318
2311
 
2319
2312
  if elements
@@ -2326,6 +2319,7 @@ class Rufo::Formatter
2326
2319
  end
2327
2320
 
2328
2321
  check :on_rbrace
2322
+ write " " if need_space
2329
2323
  write "}"
2330
2324
  next_token
2331
2325
  end
@@ -2477,7 +2471,7 @@ class Rufo::Formatter
2477
2471
  # (followed by `=`, though not included in this node)
2478
2472
  #
2479
2473
  # [:field, receiver, :".", name]
2480
- _, receiver, dot, name = node
2474
+ _, receiver, _, name = node
2481
2475
 
2482
2476
  @dot_column = nil
2483
2477
  @original_dot_column = nil
@@ -2573,7 +2567,7 @@ class Rufo::Formatter
2573
2567
  brace = current_token_value == "{"
2574
2568
 
2575
2569
  if brace
2576
- closing_brace_token, index = find_closing_brace_token
2570
+ closing_brace_token, _ = find_closing_brace_token
2577
2571
 
2578
2572
  # Check if the whole block fits into a single line
2579
2573
  if current_token_line == closing_brace_token[0][0]
@@ -2722,7 +2716,7 @@ class Rufo::Formatter
2722
2716
  # We have to be careful not to aumatically write a heredoc on next_token,
2723
2717
  # because we miss the chance to write a comma to separate elements
2724
2718
  first_space = skip_space_no_heredoc_check
2725
- wrote_comma = check_heredocs_in_literal_elements(is_last, needs_trailing_comma, wrote_comma)
2719
+ wrote_comma = check_heredocs_in_literal_elements(is_last, wrote_comma)
2726
2720
 
2727
2721
  next unless comma?
2728
2722
 
@@ -2736,7 +2730,7 @@ class Rufo::Formatter
2736
2730
  next_token_no_heredoc_check
2737
2731
 
2738
2732
  first_space = skip_space_no_heredoc_check
2739
- wrote_comma = check_heredocs_in_literal_elements(is_last, needs_trailing_comma, wrote_comma)
2733
+ wrote_comma = check_heredocs_in_literal_elements(is_last, wrote_comma)
2740
2734
 
2741
2735
  if newline? || comment?
2742
2736
  if is_last
@@ -2784,7 +2778,7 @@ class Rufo::Formatter
2784
2778
  end
2785
2779
  end
2786
2780
 
2787
- def check_heredocs_in_literal_elements(is_last, needs_trailing_comma, wrote_comma)
2781
+ def check_heredocs_in_literal_elements(is_last, wrote_comma)
2788
2782
  if (newline? || comment?) && !@heredocs.empty?
2789
2783
  if is_last && trailing_commas
2790
2784
  write "," unless wrote_comma
@@ -2820,7 +2814,7 @@ class Rufo::Formatter
2820
2814
  skip_space
2821
2815
 
2822
2816
  indent_body node[2]
2823
- if else_body = node[3]
2817
+ if (else_body = node[3])
2824
2818
  # [:else, else_contents]
2825
2819
  # [:elsif, cond, then, else]
2826
2820
  write_indent
@@ -2895,8 +2889,6 @@ class Rufo::Formatter
2895
2889
  consume_keyword "when"
2896
2890
  consume_space
2897
2891
 
2898
- space_after_when = nil
2899
-
2900
2892
  indent(@column) do
2901
2893
  visit_comma_separated_list conds
2902
2894
  skip_space
@@ -3001,7 +2993,7 @@ class Rufo::Formatter
3001
2993
  end
3002
2994
 
3003
2995
  def consume_space_or_newline
3004
- first_space = skip_space
2996
+ skip_space
3005
2997
  if newline? || comment?
3006
2998
  consume_end_of_line
3007
2999
  write_indent(next_indent)
@@ -3187,9 +3179,7 @@ class Rufo::Formatter
3187
3179
  if !found_newline && want_semicolon && last != :semicolon
3188
3180
  skip_space
3189
3181
  kind = current_token_kind
3190
- case kind
3191
- when :on_ignored_nl, :on_eof
3192
- else
3182
+ unless [:on_ignored_nl, :on_eof].include?(kind)
3193
3183
  return if (kind == :on_kw) &&
3194
3184
  (%w[class module def].include?(current_token_value))
3195
3185
  write "; "
@@ -3343,8 +3333,8 @@ class Rufo::Formatter
3343
3333
  consume_token :on___end__
3344
3334
 
3345
3335
  lines = @code.lines[line..-1]
3346
- lines.each do |line|
3347
- write line.chomp
3336
+ lines.each do |current_line|
3337
+ write current_line.chomp
3348
3338
  write_line
3349
3339
  end
3350
3340
  end
@@ -3451,7 +3441,7 @@ class Rufo::Formatter
3451
3441
 
3452
3442
  def capture_output
3453
3443
  old_output = @output
3454
- @output = "".dup
3444
+ @output = +""
3455
3445
  yield
3456
3446
  result = @output
3457
3447
  @output = old_output
@@ -3502,8 +3492,7 @@ class Rufo::Formatter
3502
3492
  @column += indent
3503
3493
  end
3504
3494
 
3505
- def indent_after_space(node, sticky: false, want_space: true, first_space: nil, needed_indent: next_indent, token_column: nil, base_column: nil)
3506
- first_space = current_token if space?
3495
+ def indent_after_space(node, sticky: false, want_space: true, needed_indent: next_indent, token_column: nil, base_column: nil)
3507
3496
  skip_space
3508
3497
 
3509
3498
  case current_token_kind
@@ -3575,8 +3564,8 @@ class Rufo::Formatter
3575
3564
  current_token[0][1]
3576
3565
  end
3577
3566
 
3578
- def keyword?(kw)
3579
- current_token_kind == :on_kw && current_token_value == kw
3567
+ def keyword?(keyword)
3568
+ current_token_kind == :on_kw && current_token_value == keyword
3580
3569
  end
3581
3570
 
3582
3571
  def newline?
@@ -3608,7 +3597,7 @@ class Rufo::Formatter
3608
3597
  i = @tokens.size - 1
3609
3598
  while i >= 0
3610
3599
  token = @tokens[i]
3611
- (line, column), kind = token
3600
+ _, kind = token
3612
3601
  case kind
3613
3602
  when :on_lbrace, :on_tlambeg
3614
3603
  count += 1
@@ -3621,23 +3610,6 @@ class Rufo::Formatter
3621
3610
  nil
3622
3611
  end
3623
3612
 
3624
- def newline_follows_token(index)
3625
- index -= 1
3626
- while index >= 0
3627
- token = @tokens[index]
3628
- case current_token_kind
3629
- when :on_sp
3630
- # OK
3631
- when :on_nl, :on_ignored_nl
3632
- return true
3633
- else
3634
- return false
3635
- end
3636
- index -= 1
3637
- end
3638
- true
3639
- end
3640
-
3641
3613
  def next_token
3642
3614
  prev_token = self.current_token
3643
3615
 
@@ -3665,8 +3637,8 @@ class Rufo::Formatter
3665
3637
  @tokens.pop
3666
3638
  end
3667
3639
 
3668
- def last?(i, array)
3669
- i == array.size - 1
3640
+ def last?(index, array)
3641
+ index == array.size - 1
3670
3642
  end
3671
3643
 
3672
3644
  def push_call(node)
@@ -3711,7 +3683,7 @@ class Rufo::Formatter
3711
3683
 
3712
3684
  lines = @output.lines
3713
3685
 
3714
- while line_to_call_info = @line_to_call_info.shift
3686
+ while (line_to_call_info = @line_to_call_info.shift)
3715
3687
  first_line, call_info = line_to_call_info
3716
3688
  next unless call_info.size == 5
3717
3689
 
@@ -3767,18 +3739,18 @@ class Rufo::Formatter
3767
3739
  do_align @case_when_positions, :case
3768
3740
  end
3769
3741
 
3770
- def do_align(elements, scope)
3742
+ def do_align(components, scope)
3771
3743
  lines = @output.lines
3772
3744
 
3773
- # Chunk elements that are in consecutive lines
3774
- chunks = chunk_while(elements) do |(l1, c1, i1, id1), (l2, c2, i2, id2)|
3745
+ # Chunk components that are in consecutive lines
3746
+ chunks = components.chunk_while do |(l1, _c1, i1, id1), (l2, _c2, i2, id2)|
3775
3747
  l1 + 1 == l2 && i1 == i2 && id1 == id2
3776
3748
  end
3777
3749
 
3778
3750
  chunks.each do |elements|
3779
3751
  next if elements.size == 1
3780
3752
 
3781
- max_column = elements.map { |l, c| c }.max
3753
+ max_column = elements.map { |_l, c| c }.max
3782
3754
 
3783
3755
  elements.each do |(line, column, _, _, offset)|
3784
3756
  next if column == max_column
@@ -3828,14 +3800,6 @@ class Rufo::Formatter
3828
3800
  end
3829
3801
  end
3830
3802
 
3831
- def chunk_while(array, &block)
3832
- if array.respond_to?(:chunk_while)
3833
- array.chunk_while(&block)
3834
- else
3835
- Rufo::Backport.chunk_while(array, &block)
3836
- end
3837
- end
3838
-
3839
3803
  def broken_ripper_version?
3840
3804
  version, teeny = RUBY_VERSION[0..2], RUBY_VERSION[4..4].to_i
3841
3805
  (version == "2.3" && teeny < 5) ||
@@ -3859,4 +3823,44 @@ class Rufo::Formatter
3859
3823
  def result
3860
3824
  @output
3861
3825
  end
3826
+
3827
+ # Check to see if need to add space inside hash literal braces.
3828
+ def need_space_for_hash?(node, closing_brace_token)
3829
+ return false unless node[1]
3830
+
3831
+ left_need_space = current_token_line == node_line(node, beginning: true)
3832
+ right_need_space = closing_brace_token[0][0] == node_line(node, beginning: false)
3833
+
3834
+ left_need_space && right_need_space
3835
+ end
3836
+
3837
+ def node_line(node, beginning: true)
3838
+ # get line of node, it is only used in visit_hash right now,
3839
+ # so handling the following node types is enough.
3840
+ case node.first
3841
+ when :hash, :string_literal, :symbol_literal, :symbol, :vcall, :string_content, :assoc_splat, :var_ref
3842
+ node_line(node[1], beginning: beginning)
3843
+ when :assoc_new
3844
+ if beginning
3845
+ node_line(node[1], beginning: beginning)
3846
+ else
3847
+ if node.last == [:string_literal, [:string_content]]
3848
+ # there's no line number for [:string_literal, [:string_content]]
3849
+ node_line(node[1], beginning: beginning)
3850
+ else
3851
+ node_line(node.last, beginning: beginning)
3852
+ end
3853
+ end
3854
+ when :assoclist_from_args
3855
+ node_line(beginning ? node[1][0] : node[1].last, beginning: beginning)
3856
+ when :dyna_symbol
3857
+ if node[1][0].is_a?(Symbol)
3858
+ node_line(node[1], beginning: beginning)
3859
+ else
3860
+ node_line(node[1][0], beginning: beginning)
3861
+ end
3862
+ when :@label, :@int, :@ident, :@tstring_content, :@kw
3863
+ node[2][0]
3864
+ end
3865
+ end
3862
3866
  end
@@ -7,7 +7,7 @@ module Rufo::Settings
7
7
  quote_style: [:double, :single],
8
8
  }
9
9
 
10
- attr_accessor *OPTIONS.keys
10
+ attr_accessor(*OPTIONS.keys)
11
11
 
12
12
  def init_settings(options)
13
13
  OPTIONS.each do |name, valid_options|
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Rufo
4
- VERSION = "0.4.2"
4
+ VERSION = "0.5.0"
5
5
  end
@@ -11,12 +11,12 @@ namespace :rufo do
11
11
  end
12
12
 
13
13
  desc "Format Ruby code in current directory"
14
- task :run, [:files_or_dirs] do |task, rake_args|
14
+ task :run, [:files_or_dirs] do |_task, rake_args|
15
15
  rufo_command(rake_args)
16
16
  end
17
17
 
18
18
  desc "Check that no formatting changes are produced"
19
- task :check, [:files_or_dirs] do |task, rake_args|
19
+ task :check, [:files_or_dirs] do |_task, rake_args|
20
20
  rufo_command("--check", rake_args)
21
21
  end
22
22
  end
@@ -4,26 +4,29 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
  require "rufo/version"
5
5
 
6
6
  Gem::Specification.new do |spec|
7
- spec.name = "rufo"
8
- spec.version = Rufo::VERSION
9
- spec.authors = ["Ary Borenszweig"]
10
- spec.email = ["asterite@gmail.com"]
7
+ spec.name = "rufo"
8
+ spec.version = Rufo::VERSION
9
+ spec.authors = ["Ary Borenszweig"]
10
+ spec.email = ["asterite@gmail.com"]
11
11
 
12
- spec.summary = %q{Ruby code formatter}
13
- spec.description = %q{Fast and unobtrusive Ruby code formatter}
14
- spec.homepage = "https://github.com/ruby-formatter/rufo"
15
- spec.license = "MIT"
12
+ spec.summary = %q{Ruby code formatter}
13
+ spec.description = %q{Fast and unobtrusive Ruby code formatter}
14
+ spec.homepage = "https://github.com/ruby-formatter/rufo"
15
+ spec.license = "MIT"
16
16
 
17
- spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
18
  f.match(%r{^(test|spec|features)/})
19
19
  end
20
- spec.bindir = "exe"
21
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
22
  spec.require_paths = ["lib"]
23
- spec.required_ruby_version = '>= 2.3.5'
23
+ spec.required_ruby_version = ">= 2.3.5"
24
24
 
25
25
  spec.add_development_dependency "bundler", ">= 1.15"
26
+ spec.add_development_dependency "byebug", "~> 10.0.2"
27
+ spec.add_development_dependency "guard-rspec", "~> 4.0"
26
28
  spec.add_development_dependency "rake", "~> 10.0"
27
29
  spec.add_development_dependency "rspec", "~> 3.0"
28
- spec.add_development_dependency "guard-rspec", "~> 4.0"
30
+ spec.add_development_dependency "rspec_junit_formatter", "~> 0.4.1"
31
+ spec.add_development_dependency "rubocop", "~> 0.63.1"
29
32
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rufo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.2
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ary Borenszweig
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-01-22 00:00:00.000000000 Z
11
+ date: 2019-02-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -24,6 +24,34 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.15'
27
+ - !ruby/object:Gem::Dependency
28
+ name: byebug
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 10.0.2
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 10.0.2
41
+ - !ruby/object:Gem::Dependency
42
+ name: guard-rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '4.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '4.0'
27
55
  - !ruby/object:Gem::Dependency
28
56
  name: rake
29
57
  requirement: !ruby/object:Gem::Requirement
@@ -53,19 +81,33 @@ dependencies:
53
81
  - !ruby/object:Gem::Version
54
82
  version: '3.0'
55
83
  - !ruby/object:Gem::Dependency
56
- name: guard-rspec
84
+ name: rspec_junit_formatter
57
85
  requirement: !ruby/object:Gem::Requirement
58
86
  requirements:
59
87
  - - "~>"
60
88
  - !ruby/object:Gem::Version
61
- version: '4.0'
89
+ version: 0.4.1
62
90
  type: :development
63
91
  prerelease: false
64
92
  version_requirements: !ruby/object:Gem::Requirement
65
93
  requirements:
66
94
  - - "~>"
67
95
  - !ruby/object:Gem::Version
68
- version: '4.0'
96
+ version: 0.4.1
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 0.63.1
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 0.63.1
69
111
  description: Fast and unobtrusive Ruby code formatter
70
112
  email:
71
113
  - asterite@gmail.com
@@ -74,11 +116,12 @@ executables:
74
116
  extensions: []
75
117
  extra_rdoc_files: []
76
118
  files:
119
+ - ".circleci/config.yml"
77
120
  - ".github/PULL_REQUEST_TEMPLATE.md"
78
121
  - ".gitignore"
79
122
  - ".rspec"
123
+ - ".rubocop.yml"
80
124
  - ".rufo"
81
- - ".travis.yml"
82
125
  - CHANGELOG.md
83
126
  - Gemfile
84
127
  - Guardfile
@@ -92,13 +135,12 @@ files:
92
135
  - docs/settings.md
93
136
  - exe/rufo
94
137
  - lib/rufo.rb
95
- - lib/rufo/backport.rb
96
138
  - lib/rufo/command.rb
97
139
  - lib/rufo/dot_file.rb
140
+ - lib/rufo/file_finder.rb
98
141
  - lib/rufo/formatter.rb
99
142
  - lib/rufo/settings.rb
100
143
  - lib/rufo/version.rb
101
- - rakelib/ci.rake
102
144
  - rakelib/rufo.rake
103
145
  - rufo.gemspec
104
146
  homepage: https://github.com/ruby-formatter/rufo
@@ -1,15 +0,0 @@
1
- sudo: false
2
- language: ruby
3
- cache: bundler
4
- rvm:
5
- - 2.6.0
6
- - 2.5.1
7
- - 2.4.4
8
- - 2.3.7
9
- - ruby-head
10
- matrix:
11
- allow_failures:
12
- - rvm: ruby-head
13
- before_install: gem install bundler -v 1.16.2
14
- script:
15
- - bundle exec rake ci
@@ -1,31 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Rufo::Backport
4
- # Implement Enumerable#chunk_while
5
- # if it's not available in the current Ruby version
6
- def self.chunk_while(array)
7
- results = []
8
- current = []
9
- first = true
10
- last = nil
11
-
12
- array.each do |elem|
13
- if first
14
- current << elem
15
- first = false
16
- else
17
- if yield(last, elem)
18
- current << elem
19
- else
20
- results << current
21
- current = [elem]
22
- end
23
- end
24
- last = elem
25
- end
26
-
27
- results << current unless current.empty?
28
-
29
- results
30
- end
31
- end
@@ -1,4 +0,0 @@
1
- desc "Run tasks on CI"
2
- task :ci => :spec do
3
- Rake::Task["rufo:check"].invoke("lib spec")
4
- end