Skip to content
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

[Fix #12242] Support AllowModifiersOnAttrs option for Style/AccessModifierDeclarations #12385

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

krororo
Copy link

@krororo krororo commented Nov 12, 2023

Fixes #12242

This PR supports AllowModifiersOnAttrs options for Style/AccessModifierDeclarations.


Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
  • 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.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.

@bbatsov
Copy link
Collaborator

bbatsov commented Nov 23, 2023

Hmm, I'm a bit puzzled by the proposal as I imagine that almost everyone will prefer to use the inline style in this case. Is there something I'm missing here?

@krororo
Copy link
Author

krororo commented Dec 2, 2023

I agree that some people prefer the inline style. However, as noted in the issue, there is a case where we want to keep the group style and write only attr_* in inline style.

For example, in applications that have been maintained for a long time, much of the code is already written in the group style. Changing the setting to inline style requires modifying a lot of code. Also, developers who are not very familiar with Ruby may know that the modifier applies to def, but they may not know that it also applies to attr_*. It would be easier to understand if we could write attr_* in inline style.

This option is similar to the Style/AllowModifiersOnSymbols option that is enabled by default. So we could write the code as follows, but it would be simpler to write the attr_* in inline style.

class Foo
  attr_reader :a
  private :a
  attr_accessor :b
  private :b, :b=
end

@@ -3045,6 +3045,7 @@ Style/AccessModifierDeclarations:
- inline
- group
AllowModifiersOnSymbols: true
AllowModifiersOnAttrs: false
Copy link
Author

Choose a reason for hiding this comment

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

I wondered what the default value should be, but decided to set it to false for compatibility.

Copy link

Choose a reason for hiding this comment

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

Set it to true won't cause any existing code to suddenly have a violation, because this is not really a new rule but just an exception to an existing rule. So I think it can default to true to align with AllowModifiersOnSymbols.

Copy link
Author

Choose a reason for hiding this comment

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

Indeed. Updated.

# @!method access_modifier_with_attr?(node)
def_node_matcher :access_modifier_with_attr?, <<~PATTERN
(send nil? {:private :protected :public :module_function}
(send nil? {:attr_reader :attr_writer :attr_accessor} _))
Copy link

Choose a reason for hiding this comment

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

Also :attr (https://ruby-doc.org/3.2.2/Module.html#method-i-attr)

Suggested change
(send nil? {:attr_reader :attr_writer :attr_accessor} _))
(send nil? {:attr :attr_reader :attr_writer :attr_accessor} _))

Copy link
Author

Choose a reason for hiding this comment

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

Good catch! I forgot it.

@krororo krororo force-pushed the support-allow-attrs-for-access-modifier-declarations branch from e8e9788 to 88ea9fd Compare December 16, 2023 09:53
@krororo
Copy link
Author

krororo commented Dec 28, 2023

Could you give us an update? Can I do something to merge this PR?

@bbatsov
Copy link
Collaborator

bbatsov commented Apr 8, 2024

@krororo Sorry for dropping the ball on this! Too much focus on Prism support recently.

For example, in applications that have been maintained for a long time, much of the code is already written in the group style. Changing the setting to inline style requires modifying a lot of code. Also, developers who are not very familiar with Ruby may know that the modifier applies to def, but they may not know that it also applies to attr_. It would be easier to understand if we could write attr_ in inline style.

Makes sense to me. Just reflect this also it cop's documentation, so it's clearer for people why this option exists.

@@ -67,6 +67,28 @@ module Style
# private :bar, :baz
#
# end
#
# @example AllowModifiersOnAttrs: true (default)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think those examples should be extended to feature something else besides attributes, so people would understand the idea is to augment whatever the primary enforced style of the cop is.

Copy link
Author

Choose a reason for hiding this comment

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

I would like to confirm that my understanding is correct, should I add method definitions like this?

# good
class Foo

  public attr_reader :bar
  protected attr_writer :baz
  private attr_accessor :qux
  private attr :quux

  def public_method; end

  private

  def private_method; end

end

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yep.

@krororo
Copy link
Author

krororo commented Apr 20, 2024

@bbatsov No problem. Thank you for the review.

Makes sense to me. Just reflect this also it cop's documentation, so it's clearer for people why this option exists.

OK. I will update cop's documentation.

@krororo krororo force-pushed the support-allow-attrs-for-access-modifier-declarations branch from 88ea9fd to 212762e Compare April 21, 2024 09:15
@krororo
Copy link
Author

krororo commented Apr 21, 2024

@bbatsov Updated cop's documentation and rebased on top of the current master branch. Could you please review again?

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.

Cop idea: Style/AllowModifiersOnAttrs
3 participants