-
-
Notifications
You must be signed in to change notification settings - Fork 268
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 #1071] Fix Rails/FilePath
cop to correctly handle File.join
with variables and ignore leading and multiple slashes in string literal arguments for Rails.root.join
and File.join
#1433
[Fix #1071] Fix Rails/FilePath
cop to correctly handle File.join
with variables and ignore leading and multiple slashes in string literal arguments for Rails.root.join
and File.join
#1433
Conversation
Rails/FilePath
cop when File.join
is used with a variableRails/FilePath
cop when File.join
is used with a variable
Rails/FilePath
cop when File.join
is used with a variableRails/FilePath
cop when File.join
is used with a variable
e0a85cc
to
0663062
Compare
lib/rubocop/cop/rails/file_path.rb
Outdated
@@ -98,6 +98,7 @@ def check_for_extension_after_rails_root_join_in_dstr(node) | |||
def check_for_file_join_with_rails_root(node) | |||
return unless file_join_nodes?(node) | |||
return unless node.arguments.any? { |e| rails_root_nodes?(e) } | |||
return if node.arguments.any?(&:variable?) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
String literals starting with /
should also be ignored when the argument is a string literal. Can you update it and add tests?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cases where the string contains two or more //
should also be ignored.
rails8-app(dev)> default_url = "default_images/ofn-logo.png"
rails8-app(dev)> Rails.root.join('public//', default_url).to_s
=> "/tmp/example/public/default_images/ofn-logo.png"
rails8-app(dev)> File.join(Rails.root, 'public//', default_url)
=> "/tmp/example/public//default_images/ofn-logo.png"
And, please document that cases potentially containing these /
should be ignored.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've done it.
4d84c42
to
e63f979
Compare
lib/rubocop/cop/rails/file_path.rb
Outdated
def valid_arguments_for_file_join_with_rails_root?(arguments) | ||
arguments.any? { |e| rails_root_nodes?(e) } && | ||
arguments.none?(&:variable?) && | ||
arguments.none? { |arg| string_contains_multiple_slashes?(arg) } | ||
end | ||
|
||
def valid_string_arguments_for_rails_root_join?(arguments) | ||
arguments.size > 1 && | ||
arguments.all?(&:str_type?) && | ||
arguments.none? { |arg| string_with_leading_slash?(arg) } && | ||
arguments.none? { |arg| string_contains_multiple_slashes?(arg) } | ||
end | ||
|
||
def valid_slash_separated_path_for_rails_root_join?(arguments) | ||
arguments.any? { |arg| string_contains_slash?(arg) } && | ||
arguments.none? { |arg| string_with_leading_slash?(arg) } && | ||
arguments.none? { |arg| string_contains_multiple_slashes?(arg) } | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you refactor to the following?
def valid_arguments_for_file_join_with_rails_root?(arguments) | |
arguments.any? { |e| rails_root_nodes?(e) } && | |
arguments.none?(&:variable?) && | |
arguments.none? { |arg| string_contains_multiple_slashes?(arg) } | |
end | |
def valid_string_arguments_for_rails_root_join?(arguments) | |
arguments.size > 1 && | |
arguments.all?(&:str_type?) && | |
arguments.none? { |arg| string_with_leading_slash?(arg) } && | |
arguments.none? { |arg| string_contains_multiple_slashes?(arg) } | |
end | |
def valid_slash_separated_path_for_rails_root_join?(arguments) | |
arguments.any? { |arg| string_contains_slash?(arg) } && | |
arguments.none? { |arg| string_with_leading_slash?(arg) } && | |
arguments.none? { |arg| string_contains_multiple_slashes?(arg) } | |
end | |
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? || string_contains_multiple_slashes?(arg) } | |
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) } | |
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) } | |
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've applied it.
a72c49e
to
a444d65
Compare
Rails/FilePath
cop when File.join
is used with a variableRails/FilePath
cop to correctly handle File.join
with variables and ignore leading and multiple slashes in string literal arguments for Rails.root.join
and File.join
a444d65
to
90c533d
Compare
lib/rubocop/cop/rails/file_path.rb
Outdated
@@ -34,7 +37,7 @@ module Rails | |||
# # good | |||
# Rails.root.join('app', 'models', 'goober').to_s | |||
# | |||
class FilePath < Base | |||
class FilePath < Base # rubocop:disable Metrics/ClassLength |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if this cop should be permanently disabled with rubocop:disable Metrics/ClassLength
. Can you add Include
entry to .rubocop_todo.yml
instead?
Metrics/ClassLength:
Max: 163
Exclude:
- 'lib/rubocop/cop/rails/file_path.rb'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, I've fixed it, but I had seen the same kind of exception for Rails/RootPathnameMethods
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That case involves many multi-line arrays, so the context may be slightly different from this one. In any case, the existing directive doesn't require changes for now. Let's leave it as is -)
….join` with variables and ignore leading and multiple slashes in string literal arguments for `Rails.root.join` and `File.join`
90c533d
to
65199b8
Compare
Thanks! |
Fix #1071
[Fix #issue-number]
(if the related issue exists).master
(if not - rebase it).bundle exec rake default
. It executes all tests and runs RuboCop on its own code.{change_type}_{change_description}.md
if the new code introduces user-observable changes. See changelog entry format for details.