Skip to content

Commit 1e11df9

Browse files
authored
Merge pull request #87 from rubocop/fix82
Fix a false positive for `Capybara/SpecificMatcher` when `text:` or `exact_text:` with regexp
2 parents 4c0a43b + 5f7fffe commit 1e11df9

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- Add new `Capybara/RedundantWithinFind` cop. ([@ydah])
88
- Fix an invalid attributes parse when name with multiple `[]` for `Capybara/SpecificFinders` and `Capybara/SpecificActions` and `Capybara/SpecificMatcher`. ([@ydah])
99
- Change to default `EnforcedStyle: have_no` for `Capybara/NegationMatcher` cop. ([@ydah])
10+
- Fix a false positive for `Capybara/SpecificMatcher` when `text:` or `exact_text:` with regexp. ([@ydah])
1011

1112
## 2.19.0 (2023-09-20)
1213

lib/rubocop/cop/capybara/specific_matcher.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ class SpecificMatcher < ::RuboCop::Cop::Base
4242
(send nil? _ (str $_) ... )
4343
PATTERN
4444

45+
# @!method text_with_regexp?(node)
46+
def_node_search :text_with_regexp?, <<-PATTERN
47+
(pair (sym {:text :exact_text}) (regexp ...))
48+
PATTERN
49+
4550
def on_send(node)
4651
first_argument(node) do |arg|
4752
next unless (matcher = specific_matcher(arg))
@@ -61,6 +66,7 @@ def specific_matcher(arg)
6166

6267
def replaceable?(node, arg, matcher)
6368
replaceable_attributes?(arg) &&
69+
!text_with_regexp?(node) &&
6470
CapybaraHelp.replaceable_option?(node, arg, matcher) &&
6571
CapybaraHelp.replaceable_pseudo_classes?(arg)
6672
end

spec/rubocop/cop/capybara/specific_matcher_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,4 +264,20 @@
264264
expect(page).to have_css(%{a[href="#{foo}"]}, text: "bar")
265265
RUBY
266266
end
267+
268+
it 'does not register an offense for abstract matcher when ' \
269+
'exact_text: /some regex/' do
270+
expect_no_offenses(<<-RUBY)
271+
expect(page).to have_css('a', class: 'cls', exact_text: /some regex/)
272+
expect(page).to have_css('button', exact_text: /some regex/, class: 'cls')
273+
RUBY
274+
end
275+
276+
it 'does not register an offense for abstract matcher when ' \
277+
'text: /some regex/' do
278+
expect_no_offenses(<<-RUBY)
279+
expect(page).to have_css('select', class: 'cls', text: /some regex/)
280+
expect(page).to have_css('table', text: /some regex/, class: 'cls')
281+
RUBY
282+
end
267283
end

0 commit comments

Comments
 (0)