-
-
Notifications
You must be signed in to change notification settings - Fork 267
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Instead of chaining `#join` on `Rails.root` or `Rails.public_path` ```ruby Rails.root.join('db').join('schema.rb') Rails.root.join('db').join(migrate).join('migration.rb') Rails.public_path.join('path').join('file.pdf') Rails.public_path.join('path').join(to).join('file.pdf') ``` we can combine all arguments into a single join ```ruby Rails.root.join('db', 'schema.rb') Rails.root.join('db', migrate, 'migration.rb') Rails.public_path.join('path', 'file.pdf') Rails.public_path.join('path', to, 'file.pdf') ```
- Loading branch information
Showing
6 changed files
with
179 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* [#586](https://github.com/rubocop/rubocop-rails/pull/586): Add new `Rails/RootJoinChain` 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,72 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Rails | ||
# Use a single `#join` instead of chaining on `Rails.root` or `Rails.public_path`. | ||
# | ||
# @example | ||
# # bad | ||
# Rails.root.join('db').join('schema.rb') | ||
# Rails.root.join('db').join(migrate).join('migration.rb') | ||
# Rails.public_path.join('path').join('file.pdf') | ||
# Rails.public_path.join('path').join(to).join('file.pdf') | ||
# | ||
# # good | ||
# Rails.root.join('db', 'schema.rb') | ||
# Rails.root.join('db', migrate, 'migration.rb') | ||
# Rails.public_path.join('path', 'file.pdf') | ||
# Rails.public_path.join('path', to, 'file.pdf') | ||
# | ||
class RootJoinChain < Base | ||
extend AutoCorrector | ||
include RangeHelp | ||
|
||
MSG = 'Use `%<root>s.join(...)` instead of chaining `#join` calls.' | ||
|
||
RESTRICT_ON_SEND = %i[join].to_set.freeze | ||
|
||
# @!method rails_root?(node) | ||
def_node_matcher :rails_root?, <<~PATTERN | ||
(send (const {nil? cbase} :Rails) {:root :public_path}) | ||
PATTERN | ||
|
||
# @!method join?(node) | ||
def_node_matcher :join?, <<~PATTERN | ||
(send _ :join $...) | ||
PATTERN | ||
|
||
def on_send(node) | ||
evidence(node) do |rails_node, args| | ||
add_offense(node, message: format(MSG, root: rails_node.source)) do |corrector| | ||
range = range_between(rails_node.loc.selector.end_pos, node.loc.expression.end_pos) | ||
replacement = ".join(#{args.map(&:source).join(', ')})" | ||
|
||
corrector.replace(range, replacement) | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def evidence(node) | ||
# Are we at the *end* of the join chain? | ||
return if join?(node.parent) | ||
# Is there only one join? | ||
return if rails_root?(node.receiver) | ||
|
||
all_args = [] | ||
|
||
while (args = join?(node)) | ||
all_args = args + all_args | ||
node = node.receiver | ||
end | ||
|
||
rails_root?(node) do | ||
yield(node, all_args) | ||
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
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,81 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Rails::RootJoinChain, :config do | ||
it 'does not register an offense for `Rails.root.join(...)`' do | ||
expect_no_offenses(<<~RUBY) | ||
Rails.root.join('db', 'schema.rb') | ||
RUBY | ||
end | ||
|
||
it 'registers and offense and corrects for `::Rails.root.join(...).join(...)`' do | ||
expect_offense(<<~RUBY) | ||
::Rails.root.join('db').join('sch' + 'ema.rb') | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `::Rails.root.join(...)` instead of chaining `#join` calls. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
::Rails.root.join('db', 'sch' + 'ema.rb') | ||
RUBY | ||
end | ||
|
||
it 'registers and offense and corrects for `::Rails.root.join(...).join(...).read`' do | ||
expect_offense(<<~RUBY) | ||
::Rails.root.join('db').join('sch' + 'ema.rb').read | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `::Rails.root.join(...)` instead of chaining `#join` calls. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
::Rails.root.join('db', 'sch' + 'ema.rb').read | ||
RUBY | ||
end | ||
|
||
it 'registers and offense and corrects for `Rails.root.join(...).join(...)`' do | ||
expect_offense(<<~RUBY) | ||
Rails.root.join('db').join('sch' + 'ema.rb') | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `Rails.root.join(...)` instead of chaining `#join` calls. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
Rails.root.join('db', 'sch' + 'ema.rb') | ||
RUBY | ||
end | ||
|
||
it 'registers and offense and corrects for `Rails.root` with any number of joins greater one' do | ||
expect_offense(<<~RUBY) | ||
Rails.root.join.join.join('db').join(migrate).join.join("migration.\#{rb}") | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `Rails.root.join(...)` instead of chaining `#join` calls. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
Rails.root.join('db', migrate, "migration.\#{rb}") | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense for `Rails.public_path.join(...)`' do | ||
expect_no_offenses(<<~RUBY) | ||
Rails.public_path.join('path', 'file.pdf') | ||
RUBY | ||
end | ||
|
||
it 'registers and offense and corrects for `Rails.public_path.join(...).join(...)`' do | ||
expect_offense(<<~RUBY) | ||
Rails.public_path.join('path').join('fi' + 'le.pdf') | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `Rails.public_path.join(...)` instead of chaining `#join` calls. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
Rails.public_path.join('path', 'fi' + 'le.pdf') | ||
RUBY | ||
end | ||
|
||
it 'registers and offense and corrects for `Rails.public_path` with any number of joins greater one' do | ||
expect_offense(<<~RUBY) | ||
Rails.public_path.join.join.join('path').join(to).join.join("file.\#{pdf}") | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `Rails.public_path.join(...)` instead of chaining `#join` calls. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
Rails.public_path.join('path', to, "file.\#{pdf}") | ||
RUBY | ||
end | ||
end |