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

Only remove trailing comma if the last alias is removed #334

Merged
merged 1 commit into from
Jul 9, 2020
Merged
Show file tree
Hide file tree
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
7 changes: 7 additions & 0 deletions libcst/codemod/commands/tests/test_remove_unused_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,10 @@ def test_access_in_assignment(self) -> None:
b(0)[x] = False
"""
self.assertCodemod(before, before)

def test_no_formatting_if_no_unused_imports(self) -> None:
before = """
from m import (a, b,)
a(b, 'look at these ugly quotes')
"""
self.assertCodemod(before, before)
18 changes: 14 additions & 4 deletions libcst/codemod/visitors/_remove_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,17 +343,22 @@ def leave_Import(
names_to_keep.append(import_alias)
continue

# no changes
if names_to_keep == original_node.names:
return updated_node

# Now, either remove this statement or remove the imports we are
# deleting from this statement.
if len(names_to_keep) == 0:
return cst.RemoveFromParent()
else:

if names_to_keep[-1] != original_node.names[-1]:
# Remove trailing comma in order to not mess up import statements.
names_to_keep = [
*names_to_keep[:-1],
names_to_keep[-1].with_changes(comma=cst.MaybeSentinel.DEFAULT),
]
return updated_node.with_changes(names=names_to_keep)
return updated_node.with_changes(names=names_to_keep)

def leave_ImportFrom(
self, original_node: cst.ImportFrom, updated_node: cst.ImportFrom
Expand Down Expand Up @@ -399,14 +404,19 @@ def leave_ImportFrom(
names_to_keep.append(import_alias)
continue

# no changes
if names_to_keep == names:
return updated_node

# Now, either remove this statement or remove the imports we are
# deleting from this statement.
if len(names_to_keep) == 0:
return cst.RemoveFromParent()
else:

if names_to_keep[-1] != names[-1]:
# Remove trailing comma in order to not mess up import statements.
names_to_keep = [
*names_to_keep[:-1],
names_to_keep[-1].with_changes(comma=cst.MaybeSentinel.DEFAULT),
]
return updated_node.with_changes(names=names_to_keep)
return updated_node.with_changes(names=names_to_keep)
14 changes: 14 additions & 0 deletions libcst/codemod/visitors/tests/test_remove_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -797,3 +797,17 @@ def visit_ImportFrom(self, node: cst.ImportFrom) -> None:
after,
RemoveImportTransformer(CodemodContext()).transform_module(module).code,
)

def test_remove_comma(self) -> None:
"""
Trailing commas should be removed if and only if the last alias is removed.
"""
before = """
from m import (a, b,)
zsol marked this conversation as resolved.
Show resolved Hide resolved
import x, y
"""
after = """
from m import (b,)
import x
"""
self.assertCodemod(before, after, [("m", "a", None), ("y", None, None)])