Skip to content

Commit

Permalink
Merge pull request #1420 from koic/fix_an_incorrect_autocorrect_for_r…
Browse files Browse the repository at this point in the history
…ails_strong_parameters_expect

[Fix #1417] Fix an incorrect autocorrect for `Rails/StrongParametersExpect`
  • Loading branch information
koic authored Jan 19, 2025
2 parents b80ecd9 + efc6dc2 commit 160d624
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#1417](https://github.com/rubocop/rubocop-rails/issues/1417): Fix an incorrect autocorrect for `Rails/StrongParametersExpect` when using a leading dot multiline call to `require` with `permit`. ([@koic][])
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rails/strong_parameters_expect.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def on_send(node)
end

add_offense(range, message: format(MSG, prefer: prefer)) do |corrector|
corrector.remove(require_method.loc.dot.join(require_method.source_range.end))
corrector.remove(require_method.receiver.source_range.end.join(require_method.source_range.end))
corrector.replace(permit_method.loc.selector, 'expect')
if replace_argument
corrector.insert_before(permit_method.first_argument, "#{require_key(require_method)}[")
Expand Down
14 changes: 14 additions & 0 deletions spec/rubocop/cop/rails/strong_parameters_expect_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,20 @@
RUBY
end

it 'registers an offense when using a leading dot multiline call to `params.require(:user).permit(:name, :age)`' do
expect_offense(<<~RUBY)
params
.require(:user)
^^^^^^^^^^^^^^ Use `expect(user: [:name, :age])` instead.
.permit(:name, :age)
RUBY

expect_correction(<<~RUBY)
params
.expect(user: [:name, :age])
RUBY
end

it 'does not register an offense when using `params.expect(user: [:name, :age])`' do
expect_no_offenses(<<~RUBY)
params.expect(user: [:name, :age])
Expand Down

0 comments on commit 160d624

Please sign in to comment.