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
base: master
Are you sure you want to change the base?
[Fix #12242] Support AllowModifiersOnAttrs
option for Style/AccessModifierDeclarations
#12385
Conversation
Hmm, I'm a bit puzzled by the proposal as I imagine that almost everyone will prefer to use the |
I agree that some people prefer the For example, in applications that have been maintained for a long time, much of the code is already written in the This option is similar to the class Foo
attr_reader :a
private :a
attr_accessor :b
private :b, :b=
end |
config/default.yml
Outdated
@@ -3045,6 +3045,7 @@ Style/AccessModifierDeclarations: | |||
- inline | |||
- group | |||
AllowModifiersOnSymbols: true | |||
AllowModifiersOnAttrs: false |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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} _)) |
There was a problem hiding this comment.
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)
(send nil? {:attr_reader :attr_writer :attr_accessor} _)) | |
(send nil? {:attr :attr_reader :attr_writer :attr_accessor} _)) |
There was a problem hiding this comment.
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.
e8e9788
to
88ea9fd
Compare
Could you give us an update? Can I do something to merge this PR? |
@krororo Sorry for dropping the ball on this! Too much focus on Prism support recently.
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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep.
@bbatsov No problem. Thank you for the review.
OK. I will update cop's documentation. |
…/AccessModifierDeclarations`
88ea9fd
to
212762e
Compare
@bbatsov Updated cop's documentation and rebased on top of the current master branch. Could you please review again? |
Fixes #12242
This PR supports
AllowModifiersOnAttrs
options forStyle/AccessModifierDeclarations
.Before submitting the PR make sure the following are checked:
[Fix #issue-number]
(if the related issue exists).master
(if not - rebase it).bundle exec rake default
. It executes all tests and runs RuboCop on its own code.{change_type}_{change_description}.md
if the new code introduces user-observable changes. See changelog entry format for details.