rufo 0.5.1 → 0.6.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: 35513ffcbf662415975c643eb3dff524ef01b969b64505f221ee2bee96390492
4
- data.tar.gz: f23d854af35a0ffbe302537d9d2483ef6f0500dfa9428babe66bca83a4bb81c0
3
+ metadata.gz: aa747d46eb2f8eaa95accca56c9bf814ef98d4c25552d5085e762ba33b5cdb8d
4
+ data.tar.gz: bc673bd377a04373a6e9bc9d6aee18abacea6bf9f5c478e0b5100cf91f7dcb24
5
5
  SHA512:
6
- metadata.gz: 51922a71d81402c5d56ff33cd6c1290fa917607b2a1dd934630098061942cef54f394552fa8975949137f02e824663563a2bdebdcfcf4e89a5239c02c0810e43
7
- data.tar.gz: b93dd700b63aa8fa8a9d3444b9cec9b996e9d031f474cab5c45a26d8768f991b89baf819be42483362c61bcfd7dedac955fd8ec1f82270f76a0db55faae31413
6
+ metadata.gz: d767e3d5061c3076b7cb976335a838816fba1eb0c7815145fc286479acbd978dadf203b0c59b62957dab27d41a0ab171f8dbb5d9f5aa20a60bdcf95c40beece1
7
+ data.tar.gz: df23b0d9cf4ac9d72fa17d5daeaaabf5fec0ac581dc53eabb5991f18a330e93b4f79b66e7f4ef59e8e34f8503bdfe81bf23dedd328cf517ff307d49556921d3f
@@ -41,7 +41,7 @@
41
41
  - run:
42
42
  name: Run Rufo
43
43
  command: |
44
- bundle exec rake rufo:run['lib spec exe/rufo rakelib Rakefile rufo.gemspec']
44
+ bundle exec rake rufo:run
45
45
 
46
46
  # Save test results for timing analysis
47
47
  - store_test_results:
@@ -58,26 +58,18 @@ jobs:
58
58
  BUNDLE_JOBS: "3"
59
59
  BUNDLE_RETRY: "3"
60
60
  BUNDLE_PATH: vendor/bundle
61
- build-2-5-1:
61
+ build-2-5-3:
62
62
  <<: *dockerbuild
63
63
  docker:
64
- - image: circleci/ruby:2.5.1
64
+ - image: circleci/ruby:2.5.3
65
65
  environment:
66
66
  BUNDLE_JOBS: "3"
67
67
  BUNDLE_RETRY: "3"
68
68
  BUNDLE_PATH: vendor/bundle
69
- build-2-4-4:
69
+ build-2-4-5:
70
70
  <<: *dockerbuild
71
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
72
+ - image: circleci/ruby:2.4.5
81
73
  environment:
82
74
  BUNDLE_JOBS: "3"
83
75
  BUNDLE_RETRY: "3"
@@ -87,6 +79,5 @@ workflows:
87
79
  test:
88
80
  jobs:
89
81
  - build-2-6-1
90
- - build-2-5-1
91
- - build-2-4-4
92
- - build-2-3-7
82
+ - build-2-5-3
83
+ - build-2-4-5
@@ -2,7 +2,7 @@ AllCops:
2
2
  Exclude:
3
3
  - "spec/**/*"
4
4
  - "vendor/**/*"
5
- TargetRubyVersion: 2.3
5
+ TargetRubyVersion: 2.4
6
6
 
7
7
  Layout:
8
8
  Enabled: false
@@ -10,6 +10,21 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
10
10
 
11
11
  ### Added
12
12
 
13
+ ### Changed
14
+
15
+ ## [0.6.0] - 2019-03-04
16
+
17
+ ### Fixed
18
+ - Fix odd indentation of nested one liner "if" statements (issue [128](https://github.com/ruby-formatter/rufo/issues/128))
19
+
20
+ ### Added
21
+ - Ignore `vendor` directories from files to be checked. This allows rufo to be run without any arguments against a directory both locally and on common CI tools without needing any configuration.
22
+ - Allow logging level to be configured with `--loglevel[=LEVEL]`.
23
+
24
+ ### Changed
25
+ - Dropped support for ruby 2.3 as it is end of life.
26
+ - Remove space before paren for short lambda syntax `-> () {}` becomes `->() {}`.
27
+
13
28
  ## [0.5.1] - 2019-02-13
14
29
 
15
30
  ### Fixed
@@ -11,6 +11,7 @@ module Rufo
11
11
  end
12
12
 
13
13
  require_relative "rufo/command"
14
+ require_relative "rufo/logger"
14
15
  require_relative "rufo/dot_file"
15
16
  require_relative "rufo/settings"
16
17
  require_relative "rufo/formatter"
@@ -8,16 +8,17 @@ class Rufo::Command
8
8
  CODE_CHANGE = 3
9
9
 
10
10
  def self.run(argv)
11
- want_check, exit_code, filename_for_dot_rufo = parse_options(argv)
12
- new(want_check, exit_code, filename_for_dot_rufo).run(argv)
11
+ want_check, exit_code, filename_for_dot_rufo, loglevel = parse_options(argv)
12
+ new(want_check, exit_code, filename_for_dot_rufo, loglevel).run(argv)
13
13
  end
14
14
 
15
- def initialize(want_check, exit_code, filename_for_dot_rufo)
15
+ def initialize(want_check, exit_code, filename_for_dot_rufo, loglevel)
16
16
  @want_check = want_check
17
17
  @exit_code = exit_code
18
18
  @filename_for_dot_rufo = filename_for_dot_rufo
19
19
  @dot_file = Rufo::DotFile.new
20
20
  @squiggly_warning_files = []
21
+ @logger = Rufo::Logger.new(loglevel)
21
22
  end
22
23
 
23
24
  def exit_code(status_code)
@@ -51,12 +52,11 @@ class Rufo::Command
51
52
 
52
53
  code == result ? CODE_OK : CODE_CHANGE
53
54
  rescue Rufo::SyntaxError
54
- STDERR.puts "Error: the given text is not a valid ruby program (it has syntax errors)"
55
+ logger.error("Error: the given text is not a valid ruby program (it has syntax errors)")
55
56
  CODE_ERROR
56
57
  rescue => ex
57
- STDERR.puts "You've found a bug!"
58
- STDERR.puts "Please report it to https://github.com/ruby-formatter/rufo/issues with code that triggers it"
59
- STDERR.puts
58
+ logger.error("You've found a bug!")
59
+ logger.error("Please report it to https://github.com/ruby-formatter/rufo/issues with code that triggers it\n")
60
60
  raise ex
61
61
  end
62
62
 
@@ -72,7 +72,7 @@ class Rufo::Command
72
72
  if exists
73
73
  files_exist = true
74
74
  else
75
- STDERR.puts "Error: file or directory not found: #{file}"
75
+ logger.warn("Error: file or directory not found: #{file}")
76
76
  next
77
77
  end
78
78
  result = format_file(file)
@@ -83,8 +83,6 @@ class Rufo::Command
83
83
 
84
84
  return CODE_ERROR unless files_exist
85
85
 
86
- STDERR.puts squiggly_heredoc_warning unless @squiggly_warning_files.empty?
87
-
88
86
  case
89
87
  when syntax_error then CODE_ERROR
90
88
  when changed then CODE_CHANGE
@@ -92,30 +90,8 @@ class Rufo::Command
92
90
  end
93
91
  end
94
92
 
95
- def squiggly_heredoc_warning
96
- <<-WARNING
97
- Rufo Warning!
98
- File#{squiggly_pluralize} #{squiggly_warning_files} #{squiggly_pluralize(:has)} not been formatted due to a problem with Ruby version #{RUBY_VERSION}
99
- Please update to Ruby #{backported_version} to fix your formatting!
100
- See https://github.com/ruby-formatter/rufo/wiki/Squiggly-Heredocs for information.
101
- WARNING
102
- end
103
-
104
- def squiggly_pluralize(word = :s)
105
- idx = word == :s ? 0 : 1
106
- (@squiggly_warning_files.length > 1 ? ["s", "have"] : ["", "has"])[idx]
107
- end
108
-
109
- def squiggly_warning_files
110
- @squiggly_warning_files.join(", ")
111
- end
112
-
113
- def backported_version
114
- return "2.3.7" if RUBY_VERSION[0..2] == "2.3"
115
- "2.4.4"
116
- end
117
-
118
93
  def format_file(filename)
94
+ logger.debug("Formatting: #{filename}")
119
95
  code = File.read(filename)
120
96
 
121
97
  begin
@@ -123,32 +99,27 @@ Rufo Warning!
123
99
  rescue Rufo::SyntaxError
124
100
  # We ignore syntax errors as these might be template files
125
101
  # with .rb extension
126
- STDERR.puts "Error: #{filename} has syntax errors"
102
+ logger.warn("Error: #{filename} has syntax errors")
127
103
  return CODE_ERROR
128
104
  end
129
105
 
130
106
  if code.force_encoding(result.encoding) != result
131
107
  if @want_check
132
- STDERR.puts "Formatting #{filename} produced changes"
108
+ logger.warn("Formatting #{filename} produced changes")
133
109
  else
134
- unless @squiggly_warning
135
- File.write(filename, result)
136
- puts "Format: #{filename}"
137
- else
138
- @squiggly_warning_files << filename
139
- end
110
+ File.write(filename, result)
111
+ logger.log("Format: #{filename}")
140
112
  end
141
113
 
142
114
  return CODE_CHANGE
143
115
  end
144
116
  rescue Rufo::SyntaxError
145
- STDERR.puts "Error: the given text in #{filename} is not a valid ruby program (it has syntax errors)"
117
+ logger.error("Error: the given text in #{filename} is not a valid ruby program (it has syntax errors)")
146
118
  CODE_ERROR
147
119
  rescue => ex
148
- STDERR.puts "You've found a bug!"
149
- STDERR.puts "It happened while trying to format the file #{filename}"
150
- STDERR.puts "Please report it to https://github.com/ruby-formatter/rufo/issues with code that triggers it"
151
- STDERR.puts
120
+ logger.error("You've found a bug!")
121
+ logger.error("It happened while trying to format the file #{filename}")
122
+ logger.error("Please report it to https://github.com/ruby-formatter/rufo/issues with code that triggers it\n")
152
123
  raise ex
153
124
  end
154
125
 
@@ -162,13 +133,13 @@ Rufo Warning!
162
133
  end
163
134
  formatter.format
164
135
  result = formatter.result
165
- @squiggly_warning = true if formatter.squiggly_flag
166
136
  result
167
137
  end
168
138
 
169
139
  def self.parse_options(argv)
170
140
  exit_code, want_check = true, false
171
141
  filename_for_dot_rufo = nil
142
+ loglevel = :log
172
143
 
173
144
  OptionParser.new do |opts|
174
145
  opts.version = Rufo::VERSION
@@ -186,12 +157,20 @@ Rufo Warning!
186
157
  exit_code = false
187
158
  end
188
159
 
160
+ opts.on(Rufo::Logger::LEVELS, "--loglevel[=LEVEL]", "Change the level of logging for the CLI. Options are: error, warn, log (default), debug, silent") do |value|
161
+ loglevel = value.to_sym
162
+ end
163
+
189
164
  opts.on("-h", "--help", "Show this help") do
190
165
  puts opts
191
166
  exit
192
167
  end
193
168
  end.parse!(argv)
194
169
 
195
- [want_check, exit_code, filename_for_dot_rufo]
170
+ [want_check, exit_code, filename_for_dot_rufo, loglevel]
196
171
  end
172
+
173
+ private
174
+
175
+ attr_reader :logger
197
176
  end
@@ -1,3 +1,5 @@
1
+ require "find"
2
+
1
3
  class Rufo::FileFinder
2
4
  include Enumerable
3
5
 
@@ -7,7 +9,22 @@ class Rufo::FileFinder
7
9
  "Rakefile",
8
10
  "rakefile.rb",
9
11
  "Rakefile.rb",
10
- ].join(",")
12
+ ]
13
+
14
+ FILENAMES = [
15
+ "Gemfile",
16
+ *RAKEFILES,
17
+ ]
18
+
19
+ EXTENSIONS = [
20
+ ".rb",
21
+ ".gemspec",
22
+ ".rake",
23
+ ]
24
+
25
+ EXCLUDED_DIRS = [
26
+ "vendor",
27
+ ]
11
28
 
12
29
  def initialize(files_or_dirs)
13
30
  @files_or_dirs = files_or_dirs
@@ -28,9 +45,17 @@ class Rufo::FileFinder
28
45
  attr_reader :files_or_dirs
29
46
 
30
47
  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?))
48
+ files = []
49
+ Find.find(file_or_dir) do |path|
50
+ basename = File.basename(path)
51
+ if File.directory?(path)
52
+ Find.prune if EXCLUDED_DIRS.include?(basename)
53
+ else
54
+ if EXTENSIONS.include?(File.extname(basename)) || FILENAMES.include?(basename)
55
+ files << path
56
+ end
57
+ end
58
+ end
59
+ files
35
60
  end
36
61
  end
@@ -7,8 +7,6 @@ class Rufo::Formatter
7
7
 
8
8
  INDENT_SIZE = 2
9
9
 
10
- attr_reader :squiggly_flag
11
-
12
10
  def self.format(code, **options)
13
11
  formatter = new(code, **options)
14
12
  formatter.format
@@ -16,7 +14,6 @@ class Rufo::Formatter
16
14
  end
17
15
 
18
16
  def initialize(code, **options)
19
- @squiggly_flag = false
20
17
  @code = code
21
18
  @tokens = Ripper.lex(code).reverse!
22
19
  @sexp = Ripper.sexp(code)
@@ -242,9 +239,6 @@ class Rufo::Formatter
242
239
  when :@tstring_content
243
240
  # [:@tstring_content, "hello ", [1, 1]]
244
241
  heredoc, tilde = @current_heredoc
245
- if heredoc && tilde && broken_ripper_version?
246
- @squiggly_flag = true
247
- end
248
242
  looking_at_newline = current_token_kind == :on_tstring_content && current_token_value == "\n"
249
243
  if heredoc && tilde && !@last_was_newline && looking_at_newline
250
244
  check :on_tstring_content
@@ -2549,8 +2543,7 @@ class Rufo::Formatter
2549
2543
  write "->"
2550
2544
  next_token
2551
2545
 
2552
- first_space = skip_space
2553
- write_space_using_setting(first_space, :one)
2546
+ skip_space
2554
2547
 
2555
2548
  if empty_params?(params)
2556
2549
  if current_token_kind == :on_lparen
@@ -2562,16 +2555,18 @@ class Rufo::Formatter
2562
2555
  end
2563
2556
  else
2564
2557
  visit params
2565
- consume_space
2566
2558
  end
2567
2559
 
2568
2560
  if void_exps?(body)
2561
+ consume_space
2569
2562
  consume_token :on_tlambeg
2570
2563
  consume_space
2571
2564
  consume_token :on_rbrace
2572
2565
  return
2573
2566
  end
2574
2567
 
2568
+ consume_space
2569
+
2575
2570
  brace = current_token_value == "{"
2576
2571
 
2577
2572
  if brace
@@ -2825,7 +2820,7 @@ class Rufo::Formatter
2825
2820
  if (else_body = node[3])
2826
2821
  # [:else, else_contents]
2827
2822
  # [:elsif, cond, then, else]
2828
- write_indent
2823
+ write_indent if @line != line
2829
2824
 
2830
2825
  case else_body[0]
2831
2826
  when :else
@@ -3808,12 +3803,6 @@ class Rufo::Formatter
3808
3803
  end
3809
3804
  end
3810
3805
 
3811
- def broken_ripper_version?
3812
- version, teeny = RUBY_VERSION[0..2], RUBY_VERSION[4..4].to_i
3813
- (version == "2.3" && teeny < 5) ||
3814
- (version == "2.4" && teeny < 2)
3815
- end
3816
-
3817
3806
  def remove_lines_before_inline_declarations
3818
3807
  return if @inline_declarations.empty?
3819
3808
 
@@ -0,0 +1,39 @@
1
+ module Rufo
2
+ class Logger
3
+ LEVELS = {
4
+ silent: 0,
5
+ error: 1,
6
+ warn: 2,
7
+ log: 3,
8
+ debug: 4,
9
+ }
10
+
11
+ def initialize(level)
12
+ @level = LEVELS.fetch(level)
13
+ end
14
+
15
+ def debug(*args)
16
+ $stdout.puts(*args) if should_output?(:debug)
17
+ end
18
+
19
+ def log(*args)
20
+ $stdout.puts(*args) if should_output?(:log)
21
+ end
22
+
23
+ def warn(*args)
24
+ $stderr.puts(*args) if should_output?(:warn)
25
+ end
26
+
27
+ def error(*args)
28
+ $stderr.puts(*args) if should_output?(:error)
29
+ end
30
+
31
+ private
32
+
33
+ attr_reader :level
34
+
35
+ def should_output?(level_to_check)
36
+ LEVELS.fetch(level_to_check) <= level
37
+ end
38
+ end
39
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Rufo
4
- VERSION = "0.5.1"
4
+ VERSION = "0.6.0"
5
5
  end
@@ -20,7 +20,7 @@ Gem::Specification.new do |spec|
20
20
  spec.bindir = "exe"
21
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.4.5"
24
24
 
25
25
  spec.add_development_dependency "bundler", ">= 1.15"
26
26
  spec.add_development_dependency "byebug", "~> 10.0.2"
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.5.1
4
+ version: 0.6.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-02-13 00:00:00.000000000 Z
11
+ date: 2019-03-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -139,6 +139,7 @@ files:
139
139
  - lib/rufo/dot_file.rb
140
140
  - lib/rufo/file_finder.rb
141
141
  - lib/rufo/formatter.rb
142
+ - lib/rufo/logger.rb
142
143
  - lib/rufo/settings.rb
143
144
  - lib/rufo/version.rb
144
145
  - rakelib/rufo.rake
@@ -155,7 +156,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
155
156
  requirements:
156
157
  - - ">="
157
158
  - !ruby/object:Gem::Version
158
- version: 2.3.5
159
+ version: 2.4.5
159
160
  required_rubygems_version: !ruby/object:Gem::Requirement
160
161
  requirements:
161
162
  - - ">="