lib/rubocop/cop/rails/where_exists.rb in rubocop-rails-2.22.2 vs lib/rubocop/cop/rails/where_exists.rb in rubocop-rails-2.23.0
- old
+ new
@@ -53,30 +53,31 @@
MSG = 'Prefer `%<good_method>s` over `%<bad_method>s`.'
RESTRICT_ON_SEND = %i[exists?].freeze
def_node_matcher :where_exists_call?, <<~PATTERN
- (send (send _ :where $...) :exists?)
+ (call (call _ :where $...) :exists?)
PATTERN
def_node_matcher :exists_with_args?, <<~PATTERN
- (send _ :exists? $...)
+ (call _ :exists? $...)
PATTERN
def on_send(node)
find_offenses(node) do |args|
return unless convertable_args?(args)
range = correction_range(node)
- good_method = build_good_method(args)
+ good_method = build_good_method(args, dot_source: node.loc.dot.source)
message = format(MSG, good_method: good_method, bad_method: range.source)
add_offense(range, message: message) do |corrector|
corrector.replace(range, good_method)
end
end
end
+ alias on_csend on_send
private
def where_style?
style == :where
@@ -106,15 +107,15 @@
elsif where_style?
node.loc.selector.with(end_pos: node.source_range.end_pos)
end
end
- def build_good_method(args)
+ def build_good_method(args, dot_source: '.')
if exists_style?
build_good_method_exists(args)
elsif where_style?
- build_good_method_where(args)
+ build_good_method_where(args, dot_source)
end
end
def build_good_method_exists(args)
if args.size > 1
@@ -122,14 +123,14 @@
else
"exists?(#{args[0].source})"
end
end
- def build_good_method_where(args)
+ def build_good_method_where(args, dot_source)
if args.size > 1
- "where(#{args.map(&:source).join(', ')}).exists?"
+ "where(#{args.map(&:source).join(', ')})#{dot_source}exists?"
else
- "where(#{args[0].source}).exists?"
+ "where(#{args[0].source})#{dot_source}exists?"
end
end
end
end
end