forked from rubocop/rubocop-rails
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new
Rails/RootPathnameMethods
cop
`Rails.root` is an instance of `Pathname`. So instead of ```ruby File.open(Rails.root.join('db', 'schema.rb')) File.open(Rails.root.join('db', 'schema.rb'), 'w') File.read(Rails.root.join('db', 'schema.rb')) 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) ``` we can simply write ```ruby Rails.root.join('db', 'schema.rb').open Rails.root.join('db', 'schema.rb').open('w') Rails.root.join('db', 'schema.rb').read Rails.root.join('db', 'schema.rb').binread Rails.root.join('db', 'schema.rb').write(content) Rails.root.join('db', 'schema.rb').binwrite(content) ``` This cop works best when used together with [`Style/FileRead`](rubocop/rubocop#10261), [`Style/FileWrite`](rubocop/rubocop#10260), and [`Rails/RootJoinChain`](rubocop#586).
- Loading branch information
Showing
6 changed files
with
221 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* [#587](https://github.com/rubocop/rubocop-rails/pull/587): Add new `Rails/RootPathnameMethods` cop. ([@leoarnold][]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Rails | ||
# Use `Rails.root` IO methods instead of passing it to `File` | ||
# | ||
# `Rails.root` is an instance of `Pathname` | ||
# so we can apply many IO methods directly. | ||
# | ||
# This cop works best when used together with | ||
# `Style/FileRead`, `Style/FileWrite` and `Rails/RootJoinChain`. | ||
# | ||
# @example | ||
# # bad | ||
# File.open(Rails.root.join('db', 'schema.rb')) | ||
# File.open(Rails.root.join('db', 'schema.rb'), 'w') | ||
# File.read(Rails.root.join('db', 'schema.rb')) | ||
# 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) | ||
# | ||
# # good | ||
# Rails.root.join('db', 'schema.rb').open | ||
# Rails.root.join('db', 'schema.rb').open('w') | ||
# Rails.root.join('db', 'schema.rb').read | ||
# Rails.root.join('db', 'schema.rb').binread | ||
# Rails.root.join('db', 'schema.rb').write(content) | ||
# Rails.root.join('db', 'schema.rb').binwrite(content) | ||
# | ||
class RootPathnameMethods < Base | ||
extend AutoCorrector | ||
|
||
MSG = '`Rails.root` is a `Pathname` so you can just append `#%<method>s`.' | ||
|
||
RESTRICT_ON_SEND = %i[binread binwrite open read write].to_set.freeze | ||
|
||
def_node_matcher :file_open, <<~PATTERN | ||
(send | ||
(const nil? :File) :open | ||
$(send | ||
(send | ||
(const nil? :Rails) :root | ||
) :join ... | ||
) | ||
$... | ||
) | ||
PATTERN | ||
|
||
def_node_matcher :file_read, <<~PATTERN | ||
(send | ||
(const nil? :File) ${:binread :read} | ||
$(send | ||
(send | ||
(const nil? :Rails) :root | ||
) :join ... | ||
) | ||
) | ||
PATTERN | ||
|
||
def_node_matcher :file_write, <<~PATTERN | ||
(send | ||
(const nil? :File) ${:binwrite :write} | ||
$(send | ||
(send | ||
(const nil? :Rails) :root | ||
) :join ... | ||
) | ||
$_ | ||
) | ||
PATTERN | ||
|
||
def on_send(node) | ||
investigate_file_open(node) || investigate_file_read(node) || investigate_file_write(node) | ||
end | ||
|
||
private | ||
|
||
def investigate_file_open(node) | ||
return if node.parent? | ||
return unless (rails_root, args = file_open(node)) | ||
|
||
add_offense(node, message: format(MSG, method: :open)) do |corrector| | ||
replacement = "#{rails_root.source}.open" | ||
replacement += "(#{args.map(&:source).join(', ')})" unless args.empty? | ||
|
||
corrector.replace(node, replacement) | ||
end | ||
end | ||
|
||
def investigate_file_read(node) | ||
return unless (method, rails_root = file_read(node)) | ||
|
||
add_offense(node, message: format(MSG, method: method)) do |corrector| | ||
corrector.replace(node, "#{rails_root.source}.#{method}") | ||
end | ||
end | ||
|
||
def investigate_file_write(node) | ||
return unless (method, rails_root, content = file_write(node)) | ||
|
||
add_offense(node, message: format(MSG, method: method)) do |corrector| | ||
corrector.replace(node, "#{rails_root.source}.#{method}(#{content.source})") | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Rails::RootPathnameMethods, :config do | ||
it 'registers an offense when using `File.open(Rails.root.join(...))`' do | ||
expect_offense(<<~RUBY) | ||
File.open(Rails.root.join('db', 'schema.rb')) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Rails.root` is a `Pathname` so you can just append `#open`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
Rails.root.join('db', 'schema.rb').open | ||
RUBY | ||
end | ||
|
||
it "registers an offense when using `File.open(Rails.root.join(...), 'w')`" do | ||
expect_offense(<<~RUBY) | ||
File.open(Rails.root.join('db', 'schema.rb'), 'w') | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Rails.root` is a `Pathname` so you can just append `#open`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
Rails.root.join('db', 'schema.rb').open('w') | ||
RUBY | ||
end | ||
|
||
# This is handled by `Rails/RootJoinChain` | ||
it 'does not register an offense when using `File.read(Rails.root.join(...).join(...))`' do | ||
expect_no_offenses(<<~RUBY) | ||
File.read(Rails.root.join('db').join('schema.rb')) | ||
RUBY | ||
end | ||
|
||
# This is handled by `Style/FileRead` | ||
it 'does not register an offense when using `File.open(Rails.root.join(...)).read`' do | ||
expect_no_offenses(<<~RUBY) | ||
File.open(Rails.root.join('db', 'schema.rb')).read | ||
RUBY | ||
end | ||
|
||
# This is handled by `Style/FileRead` | ||
it 'does not register an offense when using `File.open(Rails.root.join(...)).binread`' do | ||
expect_no_offenses(<<~RUBY) | ||
File.open(Rails.root.join('db', 'schema.rb')).binread | ||
RUBY | ||
end | ||
|
||
# This is handled by `Style/FileWrite` | ||
it 'does not register an offense when using `File.open(Rails.root.join(...)).write(content)`' do | ||
expect_no_offenses(<<~RUBY) | ||
File.open(Rails.root.join('db', 'schema.rb')).write(content) | ||
RUBY | ||
end | ||
|
||
# This is handled by `Style/FileWrite` | ||
it 'does not register an offense when using `File.open(Rails.root.join(...)).binwrite(content)`' do | ||
expect_no_offenses(<<~RUBY) | ||
File.open(Rails.root.join('db', 'schema.rb')).binwrite(content) | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `File.read(Rails.root.join(...))`' do | ||
expect_offense(<<~RUBY) | ||
File.read(Rails.root.join('db', 'schema.rb')) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Rails.root` is a `Pathname` so you can just append `#read`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
Rails.root.join('db', 'schema.rb').read | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `File.binread(Rails.root.join(...))`' do | ||
expect_offense(<<~RUBY) | ||
File.binread(Rails.root.join('db', 'schema.rb')) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Rails.root` is a `Pathname` so you can just append `#binread`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
Rails.root.join('db', 'schema.rb').binread | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `File.write(Rails.root.join(...), content)`' do | ||
expect_offense(<<~RUBY) | ||
File.write(Rails.root.join('db', 'schema.rb'), content) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Rails.root` is a `Pathname` so you can just append `#write`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
Rails.root.join('db', 'schema.rb').write(content) | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `File.binwrite(Rails.root.join(...), content)`' do | ||
expect_offense(<<~RUBY) | ||
File.binwrite(Rails.root.join('db', 'schema.rb'), content) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Rails.root` is a `Pathname` so you can just append `#binwrite`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
Rails.root.join('db', 'schema.rb').binwrite(content) | ||
RUBY | ||
end | ||
end |