Skip to content

Commit

Permalink
Support ::Rails and ::File on Rails/FilePath cop
Browse files Browse the repository at this point in the history
  • Loading branch information
r7kamura committed Nov 14, 2022
1 parent c9abc2a commit ab5dbfc
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
1 change: 1 addition & 0 deletions changelog/fix_support_rails_and_file_on_rails_file_path.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#865](https://github.com/rubocop/rubocop-rails/pull/865): Support `::Rails` and `::File` on `Rails/FilePath` cop. ([@r7kamura][])
6 changes: 3 additions & 3 deletions lib/rubocop/cop/rails/file_path.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ class FilePath < Base
RESTRICT_ON_SEND = %i[join].freeze

def_node_matcher :file_join_nodes?, <<~PATTERN
(send (const nil? :File) :join ...)
(send (const {nil? cbase} :File) :join ...)
PATTERN

def_node_search :rails_root_nodes?, <<~PATTERN
(send (const nil? :Rails) :root)
(send (const {nil? cbase} :Rails) :root)
PATTERN

def_node_matcher :rails_root_join_nodes?, <<~PATTERN
(send (send (const nil? :Rails) :root) :join ...)
(send #rails_root_nodes? :join ...)
PATTERN

def on_dstr(node)
Expand Down
18 changes: 18 additions & 0 deletions spec/rubocop/cop/rails/file_path_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@
end
end

context 'when using ::Rails.root.join with some path strings' do
it 'registers an offense' do
expect_offense(<<~RUBY)
::Rails.root.join('app', 'models', 'user.rb')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path/to')`.
RUBY
end
end

context 'when using Rails.root.join in string interpolation of argument' do
it 'registers an offense' do
expect_offense(<<~'RUBY')
Expand Down Expand Up @@ -55,6 +64,15 @@
end
end

context 'when using ::File.join with Rails.root' do
it 'registers an offense' do
expect_offense(<<~RUBY)
::File.join(Rails.root, 'app', 'models')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path/to')`.
RUBY
end
end

context 'when using Rails.root.join with slash separated path string' do
it 'does not register an offense' do
expect_no_offenses("Rails.root.join('app/models/goober')")
Expand Down

0 comments on commit ab5dbfc

Please sign in to comment.