-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #77 from rubocop/feature/75
Add new `Capybara/RedundantWithinFind` cop
- Loading branch information
Showing
7 changed files
with
207 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Capybara | ||
# Checks for redundant `within find(...)` calls. | ||
# | ||
# @example | ||
# # bad | ||
# within find('foo.bar') do | ||
# # ... | ||
# end | ||
# | ||
# # good | ||
# within 'foo.bar' do | ||
# # ... | ||
# end | ||
# | ||
# # bad | ||
# within find_by_id('foo') do | ||
# # ... | ||
# end | ||
# | ||
# # good | ||
# within '#foo' do | ||
# # ... | ||
# end | ||
# | ||
class RedundantWithinFind < ::RuboCop::Cop::Base | ||
extend AutoCorrector | ||
MSG = 'Redundant `within %<method>s(...)` call detected.' | ||
RESTRICT_ON_SEND = %i[within].freeze | ||
FIND_METHODS = Set.new(%i[find find_by_id]).freeze | ||
|
||
# @!method within_find(node) | ||
def_node_matcher :within_find, <<~PATTERN | ||
(send nil? :within | ||
$(send nil? %FIND_METHODS ...)) | ||
PATTERN | ||
|
||
def on_send(node) | ||
within_find(node) do |find_node| | ||
add_offense(find_node, message: msg(find_node)) do |corrector| | ||
corrector.replace(find_node, replaced(find_node)) | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def msg(node) | ||
format(MSG, method: node.method_name) | ||
end | ||
|
||
def replaced(node) | ||
replaced = node.arguments.map(&:source).join(', ') | ||
if node.method?(:find_by_id) | ||
replaced.sub(/\A(["'])/, '\1#') | ||
else | ||
replaced | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Capybara::RedundantWithinFind, :config do | ||
it 'registers an offense when using `within find(...)`' do | ||
expect_offense(<<~RUBY) | ||
within find('foo.bar') do | ||
^^^^^^^^^^^^^^^ Redundant `within find(...)` call detected. | ||
end | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
within 'foo.bar' do | ||
end | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `within(find ...)`' do | ||
expect_offense(<<~RUBY) | ||
within(find 'foo.bar') do | ||
^^^^^^^^^^^^^^ Redundant `within find(...)` call detected. | ||
end | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
within('foo.bar') do | ||
end | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `within find(...)` with other argument' do | ||
expect_offense(<<~RUBY) | ||
within find('foo.bar', visible: false) do | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Redundant `within find(...)` call detected. | ||
end | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
within 'foo.bar', visible: false do | ||
end | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `within find_by_id(...)`' do | ||
expect_offense(<<~RUBY) | ||
within find_by_id('foo') do | ||
^^^^^^^^^^^^^^^^^ Redundant `within find_by_id(...)` call detected. | ||
end | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
within '#foo' do | ||
end | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `within(find_by_id ...)`' do | ||
expect_offense(<<~RUBY) | ||
within(find_by_id 'foo') do | ||
^^^^^^^^^^^^^^^^ Redundant `within find_by_id(...)` call detected. | ||
end | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
within('#foo') do | ||
end | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `within find_by_id(...)` with ' \ | ||
'other argument' do | ||
expect_offense(<<~RUBY) | ||
within find_by_id('foo', visible: false) do | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Redundant `within find_by_id(...)` call detected. | ||
end | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
within '#foo', visible: false do | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `within` without `find`' do | ||
expect_no_offenses(<<~RUBY) | ||
within 'foo.bar' do | ||
end | ||
RUBY | ||
end | ||
end |