Skip to content

Commit 24b2e87

Browse files
authored
Merge pull request #865 from r7kamura/feature/file-path-cbase
Support `::Rails` and `::File` on `Rails/FilePath` cop
2 parents c9abc2a + ab5dbfc commit 24b2e87

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#865](https://github.com/rubocop/rubocop-rails/pull/865): Support `::Rails` and `::File` on `Rails/FilePath` cop. ([@r7kamura][])

lib/rubocop/cop/rails/file_path.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ class FilePath < Base
3434
RESTRICT_ON_SEND = %i[join].freeze
3535

3636
def_node_matcher :file_join_nodes?, <<~PATTERN
37-
(send (const nil? :File) :join ...)
37+
(send (const {nil? cbase} :File) :join ...)
3838
PATTERN
3939

4040
def_node_search :rails_root_nodes?, <<~PATTERN
41-
(send (const nil? :Rails) :root)
41+
(send (const {nil? cbase} :Rails) :root)
4242
PATTERN
4343

4444
def_node_matcher :rails_root_join_nodes?, <<~PATTERN
45-
(send (send (const nil? :Rails) :root) :join ...)
45+
(send #rails_root_nodes? :join ...)
4646
PATTERN
4747

4848
def on_dstr(node)

spec/rubocop/cop/rails/file_path_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@
1313
end
1414
end
1515

16+
context 'when using ::Rails.root.join with some path strings' do
17+
it 'registers an offense' do
18+
expect_offense(<<~RUBY)
19+
::Rails.root.join('app', 'models', 'user.rb')
20+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path/to')`.
21+
RUBY
22+
end
23+
end
24+
1625
context 'when using Rails.root.join in string interpolation of argument' do
1726
it 'registers an offense' do
1827
expect_offense(<<~'RUBY')
@@ -55,6 +64,15 @@
5564
end
5665
end
5766

67+
context 'when using ::File.join with Rails.root' do
68+
it 'registers an offense' do
69+
expect_offense(<<~RUBY)
70+
::File.join(Rails.root, 'app', 'models')
71+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path/to')`.
72+
RUBY
73+
end
74+
end
75+
5876
context 'when using Rails.root.join with slash separated path string' do
5977
it 'does not register an offense' do
6078
expect_no_offenses("Rails.root.join('app/models/goober')")

0 commit comments

Comments
 (0)