Skip to content

[Fix #1228] Enhance Rails/SaveBang to properly handle instance variables #1421

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

ydakuka
Copy link
Contributor

@ydakuka ydakuka commented Jan 18, 2025

Fix #1228


  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.

Comment on lines 331 to 333
assignment = assignable_node(node).parent
assignment&.lvasgn_type?

assignment&.ivasgn_type? || assignment&.lvasgn_type?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you refactor to the following?

diff --git a/lib/rubocop/cop/rails/save_bang.rb b/lib/rubocop/cop/rails/save_bang.rb
index 2729c762c..b60598a4e 100644
--- a/lib/rubocop/cop/rails/save_bang.rb
+++ b/lib/rubocop/cop/rails/save_bang.rb
@@ -328,9 +328,9 @@ module RuboCop
         end

         def return_value_assigned?(node)
-          assignment = assignable_node(node).parent
+          return false unless (assignment = assignable_node(node).parent)

-          assignment&.ivasgn_type? || assignment&.lvasgn_type?
+          assignment.ivasgn_type? || assignment.lvasgn_type?
         end

         def persist_method?(node, methods = RESTRICT_ON_SEND)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@ydakuka ydakuka force-pushed the fix/enhance-rails-save-bang-to-properly-handle-instance-variables branch from 7487f40 to 5030b42 Compare January 19, 2025 09:38
@koic
Copy link
Member

koic commented Jan 19, 2025

Can you add a changelog entry file?

assignment&.lvasgn_type?
return false unless (assignment = assignable_node(node).parent)

assignment.ivasgn_type? || assignment.lvasgn_type?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could it be that it’s not limited solely to adding instance variables?

Suggested change
assignment.ivasgn_type? || assignment.lvasgn_type?
assignment.assignment?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@ydakuka ydakuka force-pushed the fix/enhance-rails-save-bang-to-properly-handle-instance-variables branch 2 times, most recently from 5cb703b to bf0dc58 Compare January 19, 2025 10:52
@@ -0,0 +1 @@
* [#1228](https://github.com/rubocop/rubocop-rails/issues/1228): Enhance `Rails/SaveBang` to properly handle instance variables. ([@ydakuka][])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is missing the .md extension and I think it will not be picked up by the changelog generator because of that

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, fixed.

@ydakuka ydakuka force-pushed the fix/enhance-rails-save-bang-to-properly-handle-instance-variables branch from bf0dc58 to 48233de Compare January 19, 2025 11:28
@koic koic merged commit e5dce3a into rubocop:master Jan 19, 2025
16 checks passed
@koic
Copy link
Member

koic commented Jan 19, 2025

Thanks!

@ydakuka ydakuka deleted the fix/enhance-rails-save-bang-to-properly-handle-instance-variables branch January 19, 2025 15:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rails/SaveBang does not work with instance variables
3 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy