Skip to content

Commit 2a96132

Browse files
authored
Merge pull request rubocop#855 from gsamokovarov/pathname-parens-autocorrect
Fix Rails/RootPathnameMethods autocorrection for Pathname calls without parens
2 parents 0c27719 + 3df1615 commit 2a96132

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#855](https://github.com/rubocop/rubocop-rails/pull/855): Fix Rails/RootPathnameMethods autocorrection for Pathname calls without parens. ([@gsamokovarov][])

lib/rubocop/cop/rails/root_pathname_methods.rb

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,11 @@ class RootPathnameMethods < Base
163163
def on_send(node)
164164
evidence(node) do |method, path, args, rails_root|
165165
add_offense(node, message: format(MSG, method: method, rails_root: rails_root.source)) do |corrector|
166-
if dir_glob?(node)
167-
replacement = build_path_glob(path, method)
168-
else
169-
replacement = "#{path.source}.#{method}"
170-
replacement += "(#{args.map(&:source).join(', ')})" unless args.empty?
171-
end
166+
replacement = if dir_glob?(node)
167+
build_path_glob_replacement(path, method)
168+
else
169+
build_path_replacement(path, method, args)
170+
end
172171

173172
corrector.replace(node, replacement)
174173
end
@@ -184,7 +183,7 @@ def evidence(node)
184183
yield(method, path, args, rails_root)
185184
end
186185

187-
def build_path_glob(path, method)
186+
def build_path_glob_replacement(path, method)
188187
receiver = range_between(path.loc.expression.begin_pos, path.children.first.loc.selector.end_pos).source
189188

190189
argument = if path.arguments.one?
@@ -196,6 +195,18 @@ def build_path_glob(path, method)
196195
"#{receiver}.#{method}(#{argument})"
197196
end
198197

198+
def build_path_replacement(path, method, args)
199+
path_replacement = path.source
200+
if path.arguments? && !path.parenthesized_call?
201+
path_replacement[' '] = '('
202+
path_replacement << ')'
203+
end
204+
205+
replacement = "#{path_replacement}.#{method}"
206+
replacement += "(#{args.map(&:source).join(', ')})" unless args.empty?
207+
replacement
208+
end
209+
199210
def include_interpolation?(arguments)
200211
arguments.any? do |argument|
201212
argument.children.any? { |child| child.respond_to?(:begin_type?) && child.begin_type? }

spec/rubocop/cop/rails/root_pathname_methods_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,4 +148,15 @@
148148
files.map { |file| Rails.root.join('db', file).open('wb') }
149149
RUBY
150150
end
151+
152+
it 'registers an offense when using `File.open Rails.root.join ...` without parens' do
153+
expect_offense(<<~RUBY)
154+
file = File.open Rails.root.join 'docs', 'invoice.pdf'
155+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Rails.root` is a `Pathname` so you can just append `#open`.
156+
RUBY
157+
158+
expect_correction(<<~RUBY)
159+
file = Rails.root.join('docs', 'invoice.pdf').open
160+
RUBY
161+
end
151162
end

0 commit comments

Comments
 (0)