Skip to content

Commit

Permalink
Simplify Style/MixinUsage and don't depend on broken Node#macro?
Browse files Browse the repository at this point in the history
Bump `rubocop/ast` requirement.
See rubocop#8765
  • Loading branch information
marcandre authored and mergify[bot] committed Sep 24, 2020
1 parent d514045 commit b1e1829
Showing 1 changed file with 7 additions and 27 deletions.
34 changes: 7 additions & 27 deletions lib/rubocop/cop/style/mixin_usage.rb
Expand Up @@ -50,41 +50,21 @@ class MixinUsage < Base
const)
PATTERN

def_node_matcher :wrapped_macro_scope?, <<~PATTERN
{({sclass class module block} ... ({begin if} ...))}
def_node_matcher :in_top_level_scope?, <<~PATTERN
{
root? # either at the top level
^[ {kwbegin begin if def} # or wrapped within one of these
#in_top_level_scope? ] # that is in top level scope
}
PATTERN

def on_send(node)
include_statement(node) do |statement|
return if node.argument? ||
accepted_include?(node) ||
belongs_to_class_or_module?(node)
return unless in_top_level_scope?(node)

add_offense(node, message: format(MSG, statement: statement))
end
end

private

def accepted_include?(node)
node.parent && (node.macro? || ascend_macro_scope?(node.parent))
end

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

ancestor.parent && ascend_macro_scope?(ancestor.parent)
end

def belongs_to_class_or_module?(node)
if !node.parent
false
else
return true if node.parent.class_type? || node.parent.module_type?

belongs_to_class_or_module?(node.parent)
end
end
end
end
end
Expand Down

0 comments on commit b1e1829

Please sign in to comment.