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 #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

ydakuka
Copy link
Contributor

@ydakuka ydakuka commented Feb 2, 2025

Fix #1071


  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.

@ydakuka ydakuka changed the title [Fix rubocop#1071] Fix an error occurring in the Rails/FilePath cop when File.join is used with a variable [Fix #1071] Fix an error occurring in the Rails/FilePath cop when File.join is used with a variable Feb 2, 2025
@ydakuka ydakuka changed the title [Fix #1071] Fix an error occurring in the Rails/FilePath cop when File.join is used with a variable [Fix #1071] Fix an error occurring for Rails/FilePath cop when File.join is used with a variable Feb 2, 2025
@ydakuka ydakuka force-pushed the 1071-fix-an-error-occurring-in-the-rails-file-path-cop-when-file-join-is-used-with-a-variable branch from e0a85cc to 0663062 Compare February 2, 2025 20:13
@@ -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?)
Copy link
Member

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?

Copy link
Member

@koic koic Feb 3, 2025

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've done it.

@ydakuka ydakuka force-pushed the 1071-fix-an-error-occurring-in-the-rails-file-path-cop-when-file-join-is-used-with-a-variable branch 2 times, most recently from 4d84c42 to e63f979 Compare February 4, 2025 23:14
Comment on lines 132 to 149
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
Copy link
Member

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?

Suggested change
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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've applied it.

@ydakuka ydakuka force-pushed the 1071-fix-an-error-occurring-in-the-rails-file-path-cop-when-file-join-is-used-with-a-variable branch 2 times, most recently from a72c49e to a444d65 Compare February 5, 2025 09:51
@ydakuka ydakuka changed the title [Fix #1071] Fix an error occurring for Rails/FilePath cop when File.join is used with a variable [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 Feb 5, 2025
@ydakuka ydakuka force-pushed the 1071-fix-an-error-occurring-in-the-rails-file-path-cop-when-file-join-is-used-with-a-variable branch from a444d65 to 90c533d Compare February 6, 2025 08:03
@@ -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
Copy link
Member

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'

Copy link
Contributor Author

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.

https://github.com/rubocop/rubocop-rails/blob/master/lib/rubocop/cop/rails/root_pathname_methods.rb#L38

Copy link
Member

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`
@ydakuka ydakuka force-pushed the 1071-fix-an-error-occurring-in-the-rails-file-path-cop-when-file-join-is-used-with-a-variable branch from 90c533d to 65199b8 Compare February 7, 2025 18:52
@koic koic merged commit c9a5749 into rubocop:master Feb 7, 2025
16 checks passed
@koic
Copy link
Member

koic commented Feb 7, 2025

Thanks!

@ydakuka ydakuka deleted the 1071-fix-an-error-occurring-in-the-rails-file-path-cop-when-file-join-is-used-with-a-variable branch February 7, 2025 19:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

FilePath safe autocorrect breaks code
2 participants