-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathremove_unused_imports.py
More file actions
67 lines (58 loc) · 2.57 KB
/
remove_unused_imports.py
File metadata and controls
67 lines (58 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import libcst as cst
from libcst.codemod.visitors import GatherUnusedImportsVisitor
from libcst.metadata import (
ParentNodeProvider,
PositionProvider,
QualifiedNameProvider,
ScopeProvider,
)
from codemodder.codemods.check_annotations import is_disabled_by_annotations
from codemodder.codemods.transformations.remove_unused_imports import (
RemoveUnusedImportsTransformer,
)
from core_codemods.api import Metadata, ReviewGuidance, SimpleCodemod
class RemoveUnusedImports(SimpleCodemod):
metadata = Metadata(
name="unused-imports",
summary="Remove Unused Imports",
review_guidance=ReviewGuidance.MERGE_WITHOUT_REVIEW,
)
change_description = "Unused import."
METADATA_DEPENDENCIES = (
PositionProvider,
ScopeProvider,
QualifiedNameProvider,
ParentNodeProvider,
)
IGNORE_ANNOTATIONS = ["unused-import", "F401", "W0611"]
def transform_module_impl(self, tree: cst.Module) -> cst.Module:
# Do nothing in __init__.py files
if self.file_context.file_path.name == "__init__.py":
return tree
gather_unused_visitor = GatherUnusedImportsVisitor(self.context)
tree.visit(gather_unused_visitor)
# filter the gathered imports by line excludes/includes
filtered_unused_imports = set()
for import_alias, importt in gather_unused_visitor.unused_imports:
pos = self.get_metadata(PositionProvider, import_alias)
if self.filter_by_path_includes_or_excludes(pos):
if not is_disabled_by_annotations(
importt,
self.metadata, # type: ignore
messages=self.IGNORE_ANNOTATIONS,
):
self.add_change_from_position(pos, self.change_description)
filtered_unused_imports.add((import_alias, importt))
return tree.visit(RemoveUnusedImportsTransformer(filtered_unused_imports))
def filter_by_path_includes_or_excludes(self, pos_to_match) -> bool:
"""
Returns True if the node, whose position in the file is pos_to_match, matches any of the lines specified in the path-includes or path-excludes flags.
"""
# excludes takes precedence if defined
if self.line_exclude:
return not any(match_line(pos_to_match, line) for line in self.line_exclude)
if self.line_include:
return any(match_line(pos_to_match, line) for line in self.line_include)
return True
def match_line(pos, line):
return pos.start.line == line and pos.end.line == line