Skip to content

Commit

Permalink
[Fix rubocop#7186] Fix a false positive for Style/MixinUsage
Browse files Browse the repository at this point in the history
Fixes rubocop#7186.

This PR fixes a false positive for `Style/MixinUsage` when using inside
multiline block and `if` condition is after `include`.

And this PR is a additional patch of rubocop#7000.
  • Loading branch information
koic committed Jul 3, 2019
1 parent 203843f commit 753856b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -10,6 +10,7 @@
### Bug fixes

* [#7170](https://github.com/rubocop-hq/rubocop/issues/7170): Fix a false positive for `Layout/RescueEnsureAlignment` when def line is preceded with `private_class_method`. ([@tatsuyafw][])
* [#7186](https://github.com/rubocop-hq/rubocop/issues/7186): Fix a false positive for `Style/MixinUsage` when using inside multiline block and `if` condition is after `include`. ([@koic][])

### Changes

Expand Down
13 changes: 12 additions & 1 deletion lib/rubocop/ast/node/mixin/method_dispatch_node.rb
Expand Up @@ -222,11 +222,22 @@ def binary_operation?

def_node_matcher :macro_scope?, <<~PATTERN
{^{({sclass class module block} ...) class_constructor?}
^^{({sclass class module block} ... ({begin if} ...)) class_constructor?}
^^#ascend_macro_scope?
^#macro_kwbegin_wrapper?
#root_node?}
PATTERN

def_node_matcher :wrapped_macro_scope?, <<~PATTERN
{({sclass class module block} ... ({begin if} ...)) class_constructor?}
PATTERN

def ascend_macro_scope?(ancestor)
return true if wrapped_macro_scope?(ancestor)
return false if root_node?(ancestor)

ascend_macro_scope?(ancestor.parent)
end

# Check if a node's parent is a kwbegin wrapper within a macro scope
#
# @param parent [Node] parent of the node being checked
Expand Down
3 changes: 2 additions & 1 deletion spec/rubocop/cop/style/mixin_usage_spec.rb
Expand Up @@ -66,7 +66,8 @@ class C
'and `if` condition is after `include`' do
expect_no_offenses(<<~RUBY)
klass.class_eval do
include M if defined?(M)
include M1
include M2 if defined?(M)
end
RUBY
end
Expand Down

0 comments on commit 753856b

Please sign in to comment.