Skip to content

Commit

Permalink
Move line_include/exclude to base codemod class
Browse files Browse the repository at this point in the history
  • Loading branch information
drdavella committed Sep 15, 2023
1 parent a8be7a2 commit 77d5aad
Show file tree
Hide file tree
Showing 11 changed files with 29 additions and 71 deletions.
8 changes: 1 addition & 7 deletions src/codemodder/codemods/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,7 @@ def __init__(
file_context: FileContext,
):
_SemgrepCodemod.__init__(self, execution_context, file_context)
BaseTransformer.__init__(
self,
codemod_context,
self._results,
file_context.line_exclude,
file_context.line_include,
)
BaseTransformer.__init__(self, codemod_context, self._results)

# TODO: there needs to be a way to generalize this so that it applies
# more broadly than to just a specific kind of node. There's probably a
Expand Down
8 changes: 8 additions & 0 deletions src/codemodder/codemods/base_codemod.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ def node_position(self, node):
def lineno_for_node(self, node):
return self.node_position(node).start.line

@property
def line_exclude(self):
return self.file_context.line_exclude

@property
def line_include(self):
return self.file_context.line_include


class SemgrepCodemod(BaseCodemod):
YAML_FILES: ClassVar[List[str]] = NotImplemented
Expand Down
4 changes: 1 addition & 3 deletions src/codemodder/codemods/base_visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
class UtilsMixin:
METADATA_DEPENDENCIES: Tuple[Any, ...] = (PositionProvider,)

def __init__(self, context, results, line_exclude=None, line_include=None):
def __init__(self, context, results):
super().__init__(context)
self.results = results
self.line_exclude = line_exclude or []
self.line_include = line_include or []

def filter_by_result(self, pos_to_match):
all_pos = [extract_pos_from_result(result) for result in self.results]
Expand Down
9 changes: 3 additions & 6 deletions src/codemodder/codemods/django_debug_flag_on.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,9 @@ class DebugFlagTransformer(BaseTransformer):
def __init__(
self, codemod_context: CodemodContext, file_context: FileContext, results
):
super().__init__(
codemod_context,
results,
file_context.line_exclude,
file_context.line_include,
)
super().__init__(codemod_context, results)
self.line_exclude = file_context.line_exclude
self.line_include = file_context.line_include
self.changes_in_file: List[Change] = []

def leave_Assign(
Expand Down
9 changes: 3 additions & 6 deletions src/codemodder/codemods/django_session_cookie_secure_off.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,9 @@ class SessionCookieSecureTransformer(BaseTransformer):
def __init__(
self, codemod_context: CodemodContext, file_context: FileContext, results
):
super().__init__(
codemod_context,
results,
file_context.line_exclude,
file_context.line_include,
)
super().__init__(codemod_context, results)
self.line_exclude = file_context.line_exclude
self.line_include = file_context.line_include
self.changes_in_file: List[Change] = []
self.flag_correctly_set = False

Expand Down
12 changes: 2 additions & 10 deletions src/codemodder/codemods/https_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
ReviewGuidance,
)
from codemodder.change import Change
from codemodder.context import CodemodExecutionContext
from codemodder.codemods.utils_mixin import NameResolutionMixin
from codemodder.file_context import FileContext
import libcst as cst
Expand All @@ -37,16 +36,9 @@ class HTTPSConnection(BaseCodemod, Codemod):
"urllib3.connectionpool.HTTPConnectionPool",
}

def __init__(
self,
codemod_context: CodemodContext,
execution_context: CodemodExecutionContext,
file_context: FileContext,
):
def __init__(self, codemod_context: CodemodContext, *codemod_args):
Codemod.__init__(self, codemod_context)
BaseCodemod.__init__(self, execution_context, file_context)
self.line_exclude = file_context.line_exclude
self.line_include = file_context.line_include
BaseCodemod.__init__(self, *codemod_args)

def transform_module_impl(self, tree: cst.Module) -> cst.Module:
visitor = ConnectionPollVisitor(self.context, self.file_context)
Expand Down
13 changes: 2 additions & 11 deletions src/codemodder/codemods/order_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@
ReviewGuidance,
)
from codemodder.change import Change
from codemodder.context import CodemodExecutionContext
from codemodder.codemods.transformations.clean_imports import (
GatherTopLevelImportBlocks,
OrderImportsBlocksTransform,
)
from codemodder.file_context import FileContext
import libcst as cst
from libcst.codemod import Codemod, CodemodContext

Expand All @@ -26,16 +24,9 @@ class OrderImports(BaseCodemod, Codemod):

METADATA_DEPENDENCIES = (PositionProvider,)

def __init__(
self,
codemod_context: CodemodContext,
execution_context: CodemodExecutionContext,
file_context: FileContext,
):
def __init__(self, codemod_context: CodemodContext, *codemod_args):
Codemod.__init__(self, codemod_context)
BaseCodemod.__init__(self, execution_context, file_context)
self.line_exclude = file_context.line_exclude
self.line_include = file_context.line_include
BaseCodemod.__init__(self, *codemod_args)

def transform_module_impl(self, tree: cst.Module) -> cst.Module:
top_imports_visitor = GatherTopLevelImportBlocks()
Expand Down
14 changes: 2 additions & 12 deletions src/codemodder/codemods/remove_unused_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
ReviewGuidance,
)
from codemodder.change import Change
from codemodder.context import CodemodExecutionContext
from codemodder.codemods.transformations.remove_unused_imports import (
RemoveUnusedImportsTransformer,
)
from codemodder.file_context import FileContext
import libcst as cst
from libcst.codemod import Codemod, CodemodContext

Expand All @@ -26,17 +24,9 @@ class RemoveUnusedImports(BaseCodemod, Codemod):

METADATA_DEPENDENCIES = (PositionProvider, ScopeProvider, QualifiedNameProvider)

def __init__(
self,
codemod_context: CodemodContext,
execution_context: CodemodExecutionContext,
file_context: FileContext,
):
def __init__(self, codemod_context: CodemodContext, *codemod_args):
Codemod.__init__(self, codemod_context)
BaseCodemod.__init__(self, execution_context, file_context)
# TODO: these should be moved to the base codemod class (as properties)
self.line_exclude = file_context.line_exclude
self.line_include = file_context.line_include
BaseCodemod.__init__(self, *codemod_args)

def transform_module_impl(self, tree: cst.Module) -> cst.Module:
gather_unused_visitor = GatherUnusedImportsVisitor(self.context)
Expand Down
8 changes: 1 addition & 7 deletions src/codemodder/codemods/upgrade_sslcontext_tls.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,7 @@ def __init__(
file_context: FileContext,
):
SemgrepCodemod.__init__(self, execution_context, file_context)
BaseTransformer.__init__(
self,
codemod_context,
self._results,
file_context.line_exclude,
file_context.line_include,
)
BaseTransformer.__init__(self, codemod_context, self._results)

# TODO: apply unused import remover

Expand Down
9 changes: 3 additions & 6 deletions src/codemodder/codemods/url_sandbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,9 @@ class FindRequestCallsAndImports(BaseVisitor):
def __init__(
self, codemod_context: CodemodContext, file_context: FileContext, results
):
super().__init__(
codemod_context,
results,
file_context.line_exclude,
file_context.line_include,
)
super().__init__(codemod_context, results)
self.line_exclude = file_context.line_exclude
self.line_include = file_context.line_include
self.nodes_to_change: dict[
cst.CSTNode, Union[cst.CSTNode, cst.FlattenSentinel, cst.RemovalSentinel]
] = {}
Expand Down
6 changes: 3 additions & 3 deletions tests/codemods/test_base_visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ class DeleteStatementLinesCodemod(BaseTransformer):
METADATA_DEPENDENCIES = (PositionProvider,)

def __init__(self, context, results, line_exclude=None, line_include=None):
BaseTransformer.__init__(
self, context, [], line_exclude or [], line_include or []
)
BaseTransformer.__init__(self, context, results)
self.line_exclude = line_exclude or []
self.line_include = line_include or []

def filter_by_result(self, pos_to_match):
return True
Expand Down

0 comments on commit 77d5aad

Please sign in to comment.