Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix #1345] Improve offense message for Rails/RootPathnameMethods #1347

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions lib/rubocop/cop/rails/root_pathname_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 = '`%<rails_root>s` is a `Pathname` so you can just append `#%<method>s`.'
MSG = '`%<rails_root>s` is a `Pathname`, so you can use `%<replacement>s`.'

DIR_GLOB_METHODS = %i[[] glob].to_set.freeze

Expand Down Expand Up @@ -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
Expand Down
26 changes: 13 additions & 13 deletions spec/rubocop/cop/rails/root_pathname_methods_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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')
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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')
Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down