rubocop / rubocop-rails

A RuboCop extension focused on enforcing Rails best practices and coding conventions.
https://docs.rubocop.org/rubocop-rails
MIT License
813 stars 263 forks source link

Engine.root.join not treated the same as Rails.root.join by Rails/FilePath cop #984

Open schinery opened 1 year ago

schinery commented 1 year ago

Expected behavior

MyEngine::Engine.root.join("spec", "fixtures", "images", "small_image.jpg") should be raised as a linting issue by the Rails/FilePath cop when using EnforcedStyle: slashes (default) in the same way Rails.root.join is.

Actual behavior

MyEngine::Engine.root.join("spec", "fixtures", "images", "small_image.jpg") is not raised as linting issue.

Steps to reproduce the problem

Put the following in a Ruby file and see what gets flagged as a linting issue:

Rails.root.join("spec", "fixtures", "images", "small_image.jpg")
MyEngine::Engine.root.join("spec", "fixtures", "images", "small_image.jpg")

RuboCop version

❯ bundle exec rubocop -V
1.50.0 (using Parser 3.2.2.0, rubocop-ast 1.28.0, running on ruby 3.2.2) [arm64-darwin22]
  - rubocop-faker 1.1.0
  - rubocop-rails 2.19.0
  - rubocop-rake 0.6.0
  - rubocop-rspec 2.19.0
koic commented 1 year ago

RuboCop's static analysis cannot know that MyEngine::Engine.root.join is an object with the same behavior as Rails.root.path. So I understand the use case, but I'm wondering if it's better to detect that AnyModel.root.path case, because the cop can't be kept safe by default. If will be provided, it is an alternative configuration option.