Skip to content

Commit

Permalink
Use children matching in AttributeDefinedStatically cop
Browse files Browse the repository at this point in the history
  • Loading branch information
pirj committed Aug 11, 2019
1 parent ce421f0 commit 3d908ff
Showing 1 changed file with 2 additions and 9 deletions.
Expand Up @@ -38,7 +38,7 @@ class AttributeDefinedStatically < Cop
def on_block(node)
factory_attributes(node).to_a.flatten.each do |attribute|
next unless offensive_receiver?(attribute.receiver, node)
next if proc?(attribute) || association?(attribute)
next if proc?(attribute) || association?(attribute.first_argument)

add_offense(attribute)
end
Expand Down Expand Up @@ -72,14 +72,7 @@ def proc?(attribute)
value_matcher(attribute).to_a.all?(&:block_pass_type?)
end

def association?(attribute)
argument = attribute.first_argument
argument.hash_type? && factory_key?(argument)
end

def factory_key?(hash_node)
hash_node.keys.any? { |key| key.sym_type? && key.value == :factory }
end
def_node_matcher :association?, '(hash <(pair (sym :factory) _) ...>)'

def autocorrect_replacing_parens(node)
left_braces, right_braces = braces(node)
Expand Down

0 comments on commit 3d908ff

Please sign in to comment.