Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor checks to use matchers #8

Merged
merged 2 commits into from
May 7, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 28 additions & 25 deletions django_codemod/codemods/django_40.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Main module."""
from typing import Union

from libcst import RemovalSentinel, Call, BaseExpression, Name
from libcst import matchers as m, RemovalSentinel, Call, BaseExpression, Name
from libcst._nodes.statement import Import, ImportFrom, BaseSmallStatement, ImportAlias
from libcst.codemod import VisitorBasedCodemodCommand

Expand All @@ -18,32 +18,35 @@ def leave_Import(
def leave_ImportFrom(
self, original_node: ImportFrom, updated_node: ImportFrom
) -> Union[BaseSmallStatement, RemovalSentinel]:
if updated_node.module and len(updated_node.module.children) == 3:
tops, _, last = updated_node.module.children
if len(tops.children) == 3:
top, _, middle = tops.children
if (
top.value == "django"
and middle.value == "utils"
and last.value == "encoding"
):
new_names = []
new_import_missing = True
new_import_alias = None
for import_alias in original_node.names:
if import_alias.evaluated_name == "force_text":
new_import_alias = ImportAlias(name=Name("force_str"))
else:
if import_alias.evaluated_name == "force_str":
new_import_missing = False
new_names.append(import_alias)
if new_import_missing and new_import_alias is not None:
new_names.append(new_import_alias)
new_names = list(sorted(new_names, key=lambda n: n.evaluated_name))
return ImportFrom(module=updated_node.module, names=new_names)
import_matches = m.matches(
updated_node,
m.ImportFrom(
module=m.Attribute(
attr=m.Name("encoding"),
value=m.Attribute(
value=m.Name("django"), attr=m.Name(value="utils")
),
),
),
)
if import_matches:
new_names = []
new_import_missing = True
new_import_alias = None
for import_alias in original_node.names:
if import_alias.evaluated_name == "force_text":
new_import_alias = ImportAlias(name=Name("force_str"))
else:
if import_alias.evaluated_name == "force_str":
new_import_missing = False
new_names.append(import_alias)
if new_import_missing and new_import_alias is not None:
new_names.append(new_import_alias)
new_names = list(sorted(new_names, key=lambda n: n.evaluated_name))
return ImportFrom(module=updated_node.module, names=new_names)
return super().leave_ImportFrom(original_node, updated_node)

def leave_Call(self, original_node: Call, updated_node: Call) -> BaseExpression:
if getattr(updated_node.func, "value", None) == "force_text":
if m.matches(updated_node, m.Call(func=m.Name("force_text"))):
return Call(args=updated_node.args, func=Name("force_str"))
return super().leave_Call(original_node, updated_node)