Skip to content

[Fix #1177] Enhance Rails/FilePath to properly handle trailing slashes #1437

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#1177](https://github.com/rubocop/rubocop-rails/issues/1177): Enhance `Rails/FilePath` to properly handle trailing slashes. ([@ydakuka][])
20 changes: 17 additions & 3 deletions lib/rubocop/cop/rails/file_path.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ def on_send(node)
def check_for_slash_after_rails_root_in_dstr(node)
rails_root_index = find_rails_root_index(node)
slash_node = node.children[rails_root_index + 1]
return if slash_node&.source == File::SEPARATOR
return unless slash_node&.str_type? && slash_node.source.start_with?(File::SEPARATOR)
return unless node.children[rails_root_index].children.first.send_type?

Expand Down Expand Up @@ -132,20 +133,29 @@ def check_for_rails_root_join_with_slash_separated_path(node)
def valid_arguments_for_file_join_with_rails_root?(arguments)
return false unless arguments.any? { |arg| rails_root_nodes?(arg) }

arguments.none? { |arg| arg.variable? || arg.const_type? || string_contains_multiple_slashes?(arg) }
arguments.none? do |arg|
arg.variable? || arg.const_type? ||
string_contains_multiple_slashes?(arg) || string_with_trailing_slash?(arg)
end
end

def valid_string_arguments_for_rails_root_join?(arguments)
return false unless arguments.size > 1
return false unless arguments.all?(&:str_type?)

arguments.none? { |arg| string_with_leading_slash?(arg) || string_contains_multiple_slashes?(arg) }
arguments.none? do |arg|
string_with_leading_slash?(arg) ||
string_contains_multiple_slashes?(arg) || string_with_trailing_slash?(arg)
end
end

def valid_slash_separated_path_for_rails_root_join?(arguments)
return false unless arguments.any? { |arg| string_contains_slash?(arg) }

arguments.none? { |arg| string_with_leading_slash?(arg) || string_contains_multiple_slashes?(arg) }
arguments.none? do |arg|
string_with_leading_slash?(arg) ||
string_contains_multiple_slashes?(arg) || string_with_trailing_slash?(arg)
end
end

def string_contains_slash?(node)
Expand All @@ -160,6 +170,10 @@ def string_with_leading_slash?(node)
node.str_type? && node.value.start_with?(File::SEPARATOR)
end

def string_with_trailing_slash?(node)
node.str_type? && node.value.end_with?(File::SEPARATOR)
end

def register_offense(node, require_to_s:, &block)
line_range = node.loc.column...node.loc.last_column
source_range = source_range(processed_source.buffer, node.first_line, line_range)
Expand Down
234 changes: 234 additions & 0 deletions spec/rubocop/cop/rails/file_path_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,123 @@
end
end

context 'when using File.join with Rails.root and a single forward slash as a path' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
File.join(Rails.root, "/")
RUBY
end
end

context 'when using File.join with Rails.root and a single forward slash as the last argument' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
File.join(Rails.root, 'app', '/')
RUBY
end
end

context 'when using File.join with Rails.root and multiple string paths with different trailing slash placements' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
File.join(Rails.root, 'app/models/', 'goober')
RUBY

expect_no_offenses(<<~RUBY)
File.join(Rails.root, 'app/models/', '/goober')
RUBY

expect_no_offenses(<<~RUBY)
File.join(Rails.root, 'app/models', 'goober/')
RUBY

expect_no_offenses(<<~RUBY)
File.join(Rails.root, 'app/models/', 'goober/')
RUBY
end
end

context 'when using Rails.root.join and a single forward slash as a path' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
Rails.root.join("/")
RUBY
end
end

context 'when using Rails.root.join and a single forward slash as the last argument' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
Rails.root.join('app', '/')
RUBY
end
end

context 'when using Rails.root.join and multiple string paths with different trailing slash placements' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
Rails.root.join('app/models/', 'goober')
RUBY

expect_no_offenses(<<~RUBY)
Rails.root.join('app/models/', '/goober')
RUBY

expect_no_offenses(<<~RUBY)
Rails.root.join('app/models', 'goober/')
RUBY

expect_no_offenses(<<~RUBY)
Rails.root.join('app/models/', 'goober/')
RUBY
end
end

context 'when using Rails.root.join with Rails.root and a single forward slash as a path' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
Rails.root.join(Rails.root, "/")
RUBY
end
end

context 'when using Rails.root.join with Rails.root and a single forward slash as the last argument' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
Rails.root.join(Rails.root, 'app', '/')
RUBY
end
end

context 'when using Rails.root.join with Rails.root and multiple string paths ' \
'with different trailing slash placements' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
Rails.root.join(Rails.root, 'app/models/', 'goober')
RUBY

expect_no_offenses(<<~RUBY)
Rails.root.join(Rails.root, 'app/models/', '/goober')
RUBY

expect_no_offenses(<<~RUBY)
Rails.root.join(Rails.root, 'app/models', 'goober/')
RUBY

expect_no_offenses(<<~RUBY)
Rails.root.join(Rails.root, 'app/models/', 'goober/')
RUBY
end
end

context 'when using Rails.root in string interpolation followed by a forward slash' do
it 'does not register an offense' do
expect_no_offenses(<<~'RUBY')
"#{Rails.root}/"
RUBY
end
end

context 'when using Rails.root called by double quoted string' do
it 'registers an offense' do
expect_offense(<<~'RUBY')
Expand Down Expand Up @@ -526,6 +643,123 @@
end
end

context 'when using File.join with Rails.root and a single forward slash as a path' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
File.join(Rails.root, "/")
RUBY
end
end

context 'when using File.join with Rails.root and a single forward slash as the last argument' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
File.join(Rails.root, 'app', '/')
RUBY
end
end

context 'when using File.join with Rails.root and multiple string paths with different trailing slash placements' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
File.join(Rails.root, 'app/models/', 'goober')
RUBY

expect_no_offenses(<<~RUBY)
File.join(Rails.root, 'app/models/', '/goober')
RUBY

expect_no_offenses(<<~RUBY)
File.join(Rails.root, 'app/models', 'goober/')
RUBY

expect_no_offenses(<<~RUBY)
File.join(Rails.root, 'app/models/', 'goober/')
RUBY
end
end

context 'when using Rails.root.join and a single forward slash as a path' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
Rails.root.join("/")
RUBY
end
end

context 'when using Rails.root.join and a single forward slash as the last argument' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
Rails.root.join('app', '/')
RUBY
end
end

context 'when using Rails.root.join and multiple string paths with different trailing slash placements' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
Rails.root.join('app/models/', 'goober')
RUBY

expect_no_offenses(<<~RUBY)
Rails.root.join('app/models/', '/goober')
RUBY

expect_no_offenses(<<~RUBY)
Rails.root.join('app/models', 'goober/')
RUBY

expect_no_offenses(<<~RUBY)
Rails.root.join('app/models/', 'goober/')
RUBY
end
end

context 'when using Rails.root.join with Rails.root and a single forward slash as a path' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
Rails.root.join(Rails.root, "/")
RUBY
end
end

context 'when using Rails.root.join with Rails.root and a single forward slash as the last argument' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
Rails.root.join(Rails.root, 'app', '/')
RUBY
end
end

context 'when using Rails.root.join with Rails.root and multiple string paths ' \
'with different trailing slash placements' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
Rails.root.join(Rails.root, 'app/models/', 'goober')
RUBY

expect_no_offenses(<<~RUBY)
Rails.root.join(Rails.root, 'app/models/', '/goober')
RUBY

expect_no_offenses(<<~RUBY)
Rails.root.join(Rails.root, 'app/models', 'goober/')
RUBY

expect_no_offenses(<<~RUBY)
Rails.root.join(Rails.root, 'app/models/', 'goober/')
RUBY
end
end

context 'when using Rails.root in string interpolation followed by a forward slash' do
it 'does not register an offense' do
expect_no_offenses(<<~'RUBY')
"#{Rails.root}/"
RUBY
end
end

context 'when using Rails.root called by double quoted string' do
it 'registers an offense' do
expect_offense(<<~'RUBY')
Expand Down
Loading