Skip to content

Commit

Permalink
Use hash 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 83369eb commit 5e52386
Showing 1 changed file with 2 additions and 9 deletions.
Original file line number Diff line number Diff line change
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 5e52386

Please sign in to comment.