Skip to content

Commit

Permalink
Merge pull request #1443 from ydah/fix/1442
Browse files Browse the repository at this point in the history
Fix an incorrect autocorrect for `FactoryBot/ConsistentParenthesesStyle` with `omit_parentheses` option when method name and first argument are not on same line
  • Loading branch information
pirj authored and bquorning committed Oct 25, 2022
1 parent 58da998 commit b0a638a
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Master (Unreleased)

- Fix an incorrect autocorrect for `FactoryBot/ConsistentParenthesesStyle` with `omit_parentheses` option when method name and first argument are not on same line. ([@ydah])
- Fix autocorrection loop in `RSpec/ExampleWording` for insufficient example wording. ([@pirj])
- Fix `RSpec/SortMetadata` not to reorder arguments of `include_`/`it_behaves_like`. ([@pirj])
- Fix a false positive for `RSpec/NoExpectationExample` when allowed pattern methods with arguments. ([@ydah])
Expand Down
10 changes: 10 additions & 0 deletions docs/modules/ROOT/pages/cops_rspec_factorybot.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,16 @@ create :user
build :user
create :login
create :login
# also good
# when method name and first argument are not on same line
create(
:user
)
build(
:user,
name: 'foo'
)
----

=== Configurable attributes
Expand Down
11 changes: 11 additions & 0 deletions lib/rubocop/cop/rspec/factory_bot/consistent_parentheses_style.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ module FactoryBot
# create :login
# create :login
#
# # also good
# # when method name and first argument are not on same line
# create(
# :user
# )
# build(
# :user,
# name: 'foo'
# )
#
class ConsistentParenthesesStyle < Base
extend AutoCorrector
include ConfigurableEnforcedStyle
Expand Down Expand Up @@ -66,6 +76,7 @@ def on_send(node)

def process_with_parentheses(node)
return unless style == :omit_parentheses
return unless same_line?(node, node.first_argument)

add_offense(node.loc.selector,
message: MSG_OMIT_PARENS) do |corrector|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,5 +275,43 @@
end
RUBY
end

context 'when create and first argument are on same line' do
it 'register an offense' do
expect_offense(<<~RUBY)
create(:user,
^^^^^^ Prefer method call without parentheses
name: 'foo'
)
RUBY

expect_correction(<<~RUBY)
create :user,
name: 'foo'
RUBY
end
end

context 'when create and first argument are not on same line' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
create(
:user
)
RUBY
end
end

context 'when create and some argument are not on same line' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
create(
:user,
name: 'foo'
)
RUBY
end
end
end
end

0 comments on commit b0a638a

Please sign in to comment.