-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
Add new Style/MixinUsage
cop
#4840
Add new Style/MixinUsage
cop
#4840
Conversation
acd313b
to
2b63125
Compare
PATTERN | ||
|
||
def on_send(node) | ||
return unless (statement = include_statement(node)) |
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.
Hm. Looks like you managed to trick Lint/AssignmentInCondition
by using parentheses. 😅 Will open an issue for that.
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.
Never mind. This is the intended behaviour. Can be disabled by setting AllowSafeAssignment: false
. 🙂
|
||
def top_level_node?(node) | ||
if node.parent.parent.nil? | ||
node.parent.children.first == node |
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.
There's a Node#sibling_index
method you can use. 🙂
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.
Thanks for the review. I fixed it with f9a06a2 together with the following review comment.
#4840 (comment)
# prepend M | ||
# end | ||
class TopLevelInclude < Cop | ||
MSG = '`%s` is used at the top level. Use inside `class` or `module`.' |
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.
Please use the annotated form for the format string, e.g.:
`%<keyword>s`
(I think someone was working on a cop for this. 🤔)
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.
Isn't there a cop that checks for this?
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.
Yeah, there's such a cop indeed. Not sure what we set it do, though.
Btw, since when this is bad just for Rails apps? |
0cfec79
to
f9a06a2
Compare
Yes. Outside of Rails, programmers may want to use |
My point was that no application developer will want to do something like this most of the time, and people do write applications without Rails. |
I think that's right. A use case I encountered this time was a Rails application, but I think there is a possibility that it can be applied to applications without Rails. |
I think it's a style cop. The cop has three styles, implicit, explicit and open_class. (but I think the all options are not necessary in the first pull-request.) # implicit
include M
# explicit
Obuject.include M
# open_class
class Object
include M
end |
That sounds reasonable to me. |
That makes sense. This looks good to me.
@bbatsov First This PR will target |
Fine by me. |
9de3fc9
to
204e453
Compare
Rails/TopLevelInclude
copStyle/TopLevelInclude
cop
I updated and rebased. The following is the commit that changed the department. Also I updat PR's title and commit message to |
204e453
to
b4acb67
Compare
I'm just not sure about the name of the cop as it deals with more than just |
Indeed. I considered the name |
Maybe |
b4acb67
to
dfc7ddb
Compare
It looks like a very concise and clear name🌟 I changed this cop name to |
c6385d5
to
5ab74ce
Compare
Checks that `include`, `extend` and `prepend` exists at the top level. Using these at the top level affects the behavior of `Object`. ```console % cat app/models/example.rb include M class Example < ApplicationRecord end ``` ```console % rubocop app/models/example.rb Inspecting 1 file C Offenses: /tmp/example.rb:3:1: C: include is used at the top level. Use inside class or module. include M ^^^^^^^^^ 1 file inspected, 1 offense detected ``` Since it is difficult to automatically determine the position of `include` for class including multiple modules, autocorrect is not provided.
5ab74ce
to
a383739
Compare
👍 I forgot to reword the commit message (which mentions the old name), but we can live with this. |
Feature
Checks that
include
,extend
andprepend
exists at the top level.Using these at the top level affects the behavior of
Object
. Under theapp
directory of the Rails application, the convention that a file name and a class name are mapped is applied.Since it is difficult to automatically determine the position of
include
for class including multiple modules, autocorrect is not provided.Before submitting the PR make sure the following are checked:
[Fix #issue-number]
(if the related issue exists).master
(if not - rebase it).rake spec
) are passing.rake internal_investigation
.and description in grammatically correct, complete sentences.
rake generate_cops_documentation
(required only when you've added a new cop or changed the configuration/documentation of an existing cop).