Skip to content

Commit

Permalink
feat: add find_keyword_arg utility
Browse files Browse the repository at this point in the history
  • Loading branch information
akx authored and browniebroke committed Oct 16, 2020
1 parent c3fecb3 commit 111dd37
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
Empty file.
12 changes: 12 additions & 0 deletions django_codemod/utils/calls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from typing import Optional

from libcst import Arg
from libcst import matchers as m


def find_keyword_arg(args, keyword_name: str) -> Optional[Arg]:
matcher = m.Arg(keyword=m.Name(keyword_name))
for arg in args:
if m.matches(arg, matcher):
return arg
return None
6 changes: 3 additions & 3 deletions django_codemod/visitors/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from libcst.codemod.visitors import AddImportsVisitor

from django_codemod.constants import DJANGO_1_9, DJANGO_1_11, DJANGO_2_0, DJANGO_2_1
from django_codemod.utils.calls import find_keyword_arg
from django_codemod.visitors.base import BaseDjCodemodTransformer, module_matcher


Expand Down Expand Up @@ -148,9 +149,8 @@ def is_one_to_one_field(node: Call) -> bool:

def has_on_delete(node: Call) -> bool:
# if on_delete exists in any kwarg we return True
for arg in node.args:
if m.matches(arg, m.Arg(keyword=m.Name("on_delete"))):
return True
if find_keyword_arg(node.args, "on_delete"):
return True

# if there are two or more nodes and there are no keywords
# then we can assume that positional arguments are being used
Expand Down

0 comments on commit 111dd37

Please sign in to comment.