-
-
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.
Fix: #115
- Loading branch information
Showing
7 changed files
with
240 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,78 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Capybara | ||
# Enforces use of `first` instead of `all` with `first` or `[0]`. | ||
# | ||
# @example | ||
# | ||
# # bad | ||
# all('a').first | ||
# all('a')[0] | ||
# find('a', match: :first) | ||
# all('a', match: :first) | ||
# | ||
# # good | ||
# first('a') | ||
# | ||
class FindAllFirst < ::RuboCop::Cop::Base | ||
extend AutoCorrector | ||
include RangeHelp | ||
|
||
MSG = 'Use `first(%<selector>s)`.' | ||
RESTRICT_ON_SEND = %i[all find].freeze | ||
|
||
# @!method find_all_first?(node) | ||
def_node_matcher :find_all_first?, <<~PATTERN | ||
{ | ||
(send (send _ :all _ ...) :first) | ||
(send (send _ :all _ ...) :[] (int 0)) | ||
} | ||
PATTERN | ||
|
||
# @!method include_match_first?(node) | ||
def_node_matcher :include_match_first?, <<~PATTERN | ||
(send _ {:find :all} _ $(hash <(pair (sym :match) (sym :first)) ...>)) | ||
PATTERN | ||
|
||
def on_send(node) | ||
on_all_first(node) | ||
on_match_first(node) | ||
end | ||
|
||
private | ||
|
||
def on_all_first(node) | ||
return unless (parent = node.parent) | ||
return unless find_all_first?(parent) | ||
|
||
range = range_between(node.loc.selector.begin_pos, | ||
parent.loc.selector.end_pos) | ||
selector = node.arguments.map(&:source).join(', ') | ||
add_offense(range, | ||
message: format(MSG, selector: selector)) do |corrector| | ||
corrector.replace(range, "first(#{selector})") | ||
end | ||
end | ||
|
||
def on_match_first(node) | ||
include_match_first?(node) do |hash| | ||
selector = ([node.first_argument.source] + replaced_hash(hash)) | ||
.join(', ') | ||
add_offense(node, | ||
message: format(MSG, selector: selector)) do |corrector| | ||
corrector.replace(node, "first(#{selector})") | ||
end | ||
end | ||
end | ||
|
||
def replaced_hash(hash) | ||
hash.child_nodes.flat_map(&:source).reject do |arg| | ||
arg == 'match: :first' | ||
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,121 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Capybara::FindAllFirst, :config do | ||
it 'registers an offense when using `all` with `first`' do | ||
expect_offense(<<~RUBY) | ||
all('a').first | ||
^^^^^^^^^^^^^^ Use `first('a')`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
first('a') | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `all` with `[0]`' do | ||
expect_offense(<<~RUBY) | ||
all('a')[0] | ||
^^^^^^^^^^^ Use `first('a')`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
first('a') | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `find` with `match: :first`' do | ||
expect_offense(<<~RUBY) | ||
find('a', match: :first) | ||
^^^^^^^^^^^^^^^^^^^^^^^^ Use `first('a')`. | ||
find('a', text: 'b', match: :first) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `first('a', text: 'b')`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
first('a') | ||
first('a', text: 'b') | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `all` with `match: :first`' do | ||
expect_offense(<<~RUBY) | ||
all('a', match: :first) | ||
^^^^^^^^^^^^^^^^^^^^^^^ Use `first('a')`. | ||
all('a', text: 'b', match: :first) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `first('a', text: 'b')`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
first('a') | ||
first('a', text: 'b') | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `all` with argument and `first`' do | ||
expect_offense(<<~RUBY) | ||
all('a', text: 'b')[0] | ||
^^^^^^^^^^^^^^^^^^^^^^ Use `first('a', text: 'b')`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
first('a', text: 'b') | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `all` with `first` and receiver' do | ||
expect_offense(<<~RUBY) | ||
page.all('a').first | ||
^^^^^^^^^^^^^^ Use `first('a')`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
page.first('a') | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using nested `all` with `first` and receiver' do | ||
expect_offense(<<~RUBY) | ||
find('a') | ||
.all('div') | ||
^^^^^^^^^^ Use `first('div')`. | ||
.first | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
find('a') | ||
.first('div') | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `first`' do | ||
expect_no_offenses(<<~RUBY) | ||
first('a') | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `all` without `first`' do | ||
expect_no_offenses(<<~RUBY) | ||
all('a').map(&:text) | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `all` with `[1]`' do | ||
expect_no_offenses(<<~RUBY) | ||
all('a')[1] | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `all` with argument' \ | ||
' without `first`' do | ||
expect_no_offenses(<<~RUBY) | ||
all('a', text: 'b') | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using no argument `all`' do | ||
expect_no_offenses(<<~RUBY) | ||
all.first | ||
all[0] | ||
RUBY | ||
end | ||
end |