lib/rubocop/cop/capybara/specific_finders.rb in rubocop-capybara-2.20.0 vs lib/rubocop/cop/capybara/specific_finders.rb in rubocop-capybara-2.21.0
- old
+ new
@@ -8,10 +8,11 @@
# @example
# # bad
# find('#some-id')
# find('[id=some-id]')
# find(:css, '#some-id')
+ # find(:id, 'some-id')
#
# # good
# find_by_id('some-id')
#
class SpecificFinders < ::RuboCop::Cop::Base
@@ -21,11 +22,11 @@
MSG = 'Prefer `find_by_id` over `find`.'
RESTRICT_ON_SEND = %i[find].freeze
# @!method find_argument(node)
def_node_matcher :find_argument, <<~PATTERN
- (send _ :find $(sym :css)? (str $_) ...)
+ (send _ :find $(sym {:css :id})? (str $_) ...)
PATTERN
# @!method class_options(node)
def_node_search :class_options, <<~PATTERN
(pair (sym :class) $_ ...)
@@ -36,10 +37,11 @@
next if CssSelector.pseudo_classes(arg).any?
next if CssSelector.multiple_selectors?(arg)
on_attr(node, sym, arg) if attribute?(arg)
on_id(node, sym, arg) if CssSelector.id?(arg)
+ on_sym_id(node, sym, arg) if sym.first&.value == :id
end
end
private
@@ -57,10 +59,14 @@
id = CssSelector.id(arg)
register_offense(node, sym, "'#{id}'",
CssSelector.classes(arg.sub("##{id}", '')))
end
+ def on_sym_id(node, sym, id)
+ register_offense(node, sym, "'#{id}'")
+ end
+
def attribute?(arg)
CssSelector.attribute?(arg) &&
CapybaraHelp.common_attributes?(arg)
end
@@ -104,14 +110,14 @@
options = to_options(CssSelector.attributes(arg))
options.empty? ? id : "#{id}, #{options}"
end
def to_options(attrs)
- attrs.each.map do |key, value|
+ attrs.each.filter_map do |key, value|
next if key == 'id'
"#{key}: #{value}"
- end.compact.join(', ')
+ end.join(', ')
end
def offense_range(node)
range_between(node.loc.selector.begin_pos, end_pos(node))
end