From 0f36342a119659e3afc7385f4bc1e5791bb920ee Mon Sep 17 00:00:00 2001 From: Earlopain <14981592+Earlopain@users.noreply.github.com> Date: Sun, 1 Sep 2024 10:49:59 +0200 Subject: [PATCH] [Fix #1345] Improve offense message for `Rails/RootPathnameMethods` The current message is confusing/nonsensical for `Dir[]`. Let's display the correct code instead to show what needs to be done --- .../cop/rails/root_pathname_methods.rb | 18 ++++++++----- .../cop/rails/root_pathname_methods_spec.rb | 26 +++++++++---------- 2 files changed, 24 insertions(+), 20 deletions(-) diff --git a/lib/rubocop/cop/rails/root_pathname_methods.rb b/lib/rubocop/cop/rails/root_pathname_methods.rb index 233b1744d0..5898d0b421 100644 --- a/lib/rubocop/cop/rails/root_pathname_methods.rb +++ b/lib/rubocop/cop/rails/root_pathname_methods.rb @@ -23,6 +23,8 @@ module Rails # File.binread(Rails.root.join('db', 'schema.rb')) # File.write(Rails.root.join('db', 'schema.rb'), content) # File.binwrite(Rails.root.join('db', 'schema.rb'), content) + # Dir.glob(Rails.root.join('db', 'schema.rb')) + # Dir[Rails.root.join('db', 'schema.rb')] # # # good # Rails.root.join('db', 'schema.rb').open @@ -31,12 +33,13 @@ module Rails # Rails.root.join('db', 'schema.rb').binread # Rails.root.join('db', 'schema.rb').write(content) # Rails.root.join('db', 'schema.rb').binwrite(content) + # Rails.root.glob("db/schema.rb") # class RootPathnameMethods < Base # rubocop:disable Metrics/ClassLength extend AutoCorrector include RangeHelp - MSG = '`%s` is a `Pathname` so you can just append `#%s`.' + MSG = '`%s` is a `Pathname`, so you can use `%s`.' DIR_GLOB_METHODS = %i[[] glob].to_set.freeze @@ -188,13 +191,14 @@ class RootPathnameMethods < Base # rubocop:disable Metrics/ClassLength def on_send(node) evidence(node) do |method, path, args, rails_root| - add_offense(node, message: format(MSG, method: method, rails_root: rails_root.source)) do |corrector| - replacement = if dir_glob?(node) - build_path_glob_replacement(path) - else - build_path_replacement(path, method, args) - end + replacement = if dir_glob?(node) + build_path_glob_replacement(path) + else + build_path_replacement(path, method, args) + end + message = format(MSG, rails_root: rails_root.source, replacement: replacement) + add_offense(node, message: message) do |corrector| corrector.replace(node, replacement) end end diff --git a/spec/rubocop/cop/rails/root_pathname_methods_spec.rb b/spec/rubocop/cop/rails/root_pathname_methods_spec.rb index 04b9508073..82f00ea5da 100644 --- a/spec/rubocop/cop/rails/root_pathname_methods_spec.rb +++ b/spec/rubocop/cop/rails/root_pathname_methods_spec.rb @@ -12,7 +12,7 @@ it "registers an offense when using `#{receiver}.#{method}(Rails.public_path)` (if arity exists)" do expect_offense(<<~RUBY, receiver: receiver, method: method) %{receiver}.%{method}(Rails.public_path) - ^{receiver}^^{method}^^^^^^^^^^^^^^^^^^^ `Rails.public_path` is a `Pathname` so you can just append `#%{method}`. + ^{receiver}^^{method}^^^^^^^^^^^^^^^^^^^ `Rails.public_path` is a `Pathname`, so you can use `Rails.public_path.%{method}`. RUBY expect_correction(<<~RUBY) @@ -23,7 +23,7 @@ it "registers an offense when using `::#{receiver}.#{method}(::Rails.root.join(...))` (if arity exists)" do expect_offense(<<~RUBY, receiver: receiver, method: method) ::%{receiver}.%{method}(::Rails.root.join('db', 'schema.rb')) - ^^^{receiver}^^{method}^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `::Rails.root` is a `Pathname` so you can just append `#%{method}`. + ^^^{receiver}^^{method}^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `::Rails.root` is a `Pathname`, so you can use `::Rails.root.join('db', 'schema.rb').%{method}`. RUBY expect_correction(<<~RUBY) @@ -34,7 +34,7 @@ it "registers an offense when using `::#{receiver}.#{method}(::Rails.root.join(...), ...)` (if arity exists)" do expect_offense(<<~RUBY, receiver: receiver, method: method) ::%{receiver}.%{method}(::Rails.root.join('db', 'schema.rb'), 20, 5) - ^^^{receiver}^^{method}^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `::Rails.root` is a `Pathname` so you can just append `#%{method}`. + ^^^{receiver}^^{method}^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `::Rails.root` is a `Pathname`, so you can use `::Rails.root.join('db', 'schema.rb').%{method}(20, 5)`. RUBY expect_correction(<<~RUBY) @@ -56,7 +56,7 @@ it "registers an offense when using `Dir.glob(Rails.root.join('**/*.rb'))`" do expect_offense(<<~RUBY) Dir.glob(Rails.root.join('**/*.rb')) - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Rails.root` is a `Pathname` so you can just append `#glob`. + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Rails.root` is a `Pathname`, so you can use `Rails.root.glob('**/*.rb')`. RUBY expect_correction(<<~RUBY) @@ -67,7 +67,7 @@ it "registers an offense when using `::Dir.glob(Rails.root.join('**/*.rb'))`" do expect_offense(<<~RUBY) ::Dir.glob(Rails.root.join('**/*.rb')) - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Rails.root` is a `Pathname` so you can just append `#glob`. + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Rails.root` is a `Pathname`, so you can use `Rails.root.glob('**/*.rb')`. RUBY expect_correction(<<~RUBY) @@ -78,7 +78,7 @@ it "registers an offense when using `Dir.glob(Rails.root.join('**/\#{path}/*.rb'))`" do expect_offense(<<~'RUBY') Dir.glob(Rails.root.join("**/#{path}/*.rb")) - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Rails.root` is a `Pathname` so you can just append `#glob`. + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Rails.root` is a `Pathname`, so you can use `Rails.root.glob("**/#{path}/*.rb")`. RUBY expect_correction(<<~'RUBY') @@ -89,7 +89,7 @@ it "registers an offense when using `Dir.glob(Rails.root.join('**', '*.rb'))`" do expect_offense(<<~RUBY) Dir.glob(Rails.root.join('**', '*.rb')) - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Rails.root` is a `Pathname` so you can just append `#glob`. + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Rails.root` is a `Pathname`, so you can use `Rails.root.glob('**/*.rb')`. RUBY expect_correction(<<~RUBY) @@ -105,7 +105,7 @@ it "registers an offense when using `Dir.glob(Rails.root.join('**', '*.rb'))`" do expect_offense(<<~RUBY) Dir.glob(Rails.root.join('**', '*.rb')) - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Rails.root` is a `Pathname` so you can just append `#glob`. + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Rails.root` is a `Pathname`, so you can use `Rails.root.glob("**/*.rb")`. RUBY expect_correction(<<~RUBY) @@ -117,7 +117,7 @@ it "registers an offense when using `Dir.glob(Rails.root.join('**', \"\#{path}\", '*.rb'))`" do expect_offense(<<~'RUBY') Dir.glob(Rails.root.join('**', "#{path}", '*.rb')) - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Rails.root` is a `Pathname` so you can just append `#glob`. + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Rails.root` is a `Pathname`, so you can use `Rails.root.glob("**/#{path}/*.rb")`. RUBY expect_correction(<<~'RUBY') @@ -128,7 +128,7 @@ it 'registers an offense when using `Rails.env` argument within `Dir.glob`' do expect_offense(<<~RUBY) Dir.glob(Rails.root.join("db", "seeds", Rails.env, "*.rb")).sort.each do |file| - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Rails.root` is a `Pathname` so you can just append `#glob`. + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Rails.root` is a `Pathname`, so you can use `Rails.root.glob("db/seeds/\#{Rails.env}/*.rb")`. load file end RUBY @@ -145,7 +145,7 @@ it 'registers offense when using `Dir[Rails.root.join(...)]`' do expect_offense(<<~RUBY) Dir[Rails.root.join('spec/support/**/*.rb')] - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Rails.root` is a `Pathname` so you can just append `#[]`. + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Rails.root` is a `Pathname`, so you can use `Rails.root.glob('spec/support/**/*.rb')`. RUBY expect_correction(<<~RUBY) @@ -192,7 +192,7 @@ it 'registers an offense when using `File.open(Rails.root.join(...), ...)` inside an iterator' do expect_offense(<<~RUBY) files.map { |file| File.open(Rails.root.join('db', file), 'wb') } - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Rails.root` is a `Pathname` so you can just append `#open`. + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Rails.root` is a `Pathname`, so you can use `Rails.root.join('db', file).open('wb')`. RUBY expect_correction(<<~RUBY) @@ -203,7 +203,7 @@ it 'registers an offense when using `File.open Rails.root.join ...` without parens' do expect_offense(<<~RUBY) file = File.open Rails.root.join 'docs', 'invoice.pdf' - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Rails.root` is a `Pathname` so you can just append `#open`. + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Rails.root` is a `Pathname`, so you can use `Rails.root.join('docs', 'invoice.pdf').open`. RUBY expect_correction(<<~RUBY)