From 77d5aadeea5848b11c50612d7091b12390b588f3 Mon Sep 17 00:00:00 2001 From: Daniel D'Avella Date: Thu, 14 Sep 2023 13:19:15 -0400 Subject: [PATCH] Move line_include/exclude to base codemod class --- src/codemodder/codemods/api/__init__.py | 8 +------- src/codemodder/codemods/base_codemod.py | 8 ++++++++ src/codemodder/codemods/base_visitor.py | 4 +--- src/codemodder/codemods/django_debug_flag_on.py | 9 +++------ .../codemods/django_session_cookie_secure_off.py | 9 +++------ src/codemodder/codemods/https_connection.py | 12 ++---------- src/codemodder/codemods/order_imports.py | 13 ++----------- src/codemodder/codemods/remove_unused_imports.py | 14 ++------------ src/codemodder/codemods/upgrade_sslcontext_tls.py | 8 +------- src/codemodder/codemods/url_sandbox.py | 9 +++------ tests/codemods/test_base_visitor.py | 6 +++--- 11 files changed, 29 insertions(+), 71 deletions(-) diff --git a/src/codemodder/codemods/api/__init__.py b/src/codemodder/codemods/api/__init__.py index d3fc90f9..c4944d5e 100644 --- a/src/codemodder/codemods/api/__init__.py +++ b/src/codemodder/codemods/api/__init__.py @@ -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 diff --git a/src/codemodder/codemods/base_codemod.py b/src/codemodder/codemods/base_codemod.py index 82c62893..e717864a 100644 --- a/src/codemodder/codemods/base_codemod.py +++ b/src/codemodder/codemods/base_codemod.py @@ -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 diff --git a/src/codemodder/codemods/base_visitor.py b/src/codemodder/codemods/base_visitor.py index a578dc57..81b36c63 100644 --- a/src/codemodder/codemods/base_visitor.py +++ b/src/codemodder/codemods/base_visitor.py @@ -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] diff --git a/src/codemodder/codemods/django_debug_flag_on.py b/src/codemodder/codemods/django_debug_flag_on.py index f0388610..64b6b642 100644 --- a/src/codemodder/codemods/django_debug_flag_on.py +++ b/src/codemodder/codemods/django_debug_flag_on.py @@ -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( diff --git a/src/codemodder/codemods/django_session_cookie_secure_off.py b/src/codemodder/codemods/django_session_cookie_secure_off.py index 0af67284..feafb5bf 100644 --- a/src/codemodder/codemods/django_session_cookie_secure_off.py +++ b/src/codemodder/codemods/django_session_cookie_secure_off.py @@ -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 diff --git a/src/codemodder/codemods/https_connection.py b/src/codemodder/codemods/https_connection.py index 18ffad7f..8f8d3d22 100644 --- a/src/codemodder/codemods/https_connection.py +++ b/src/codemodder/codemods/https_connection.py @@ -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 @@ -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) diff --git a/src/codemodder/codemods/order_imports.py b/src/codemodder/codemods/order_imports.py index 69ee49c2..b8853829 100644 --- a/src/codemodder/codemods/order_imports.py +++ b/src/codemodder/codemods/order_imports.py @@ -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 @@ -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() diff --git a/src/codemodder/codemods/remove_unused_imports.py b/src/codemodder/codemods/remove_unused_imports.py index 52e14d48..439030f4 100644 --- a/src/codemodder/codemods/remove_unused_imports.py +++ b/src/codemodder/codemods/remove_unused_imports.py @@ -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 @@ -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) diff --git a/src/codemodder/codemods/upgrade_sslcontext_tls.py b/src/codemodder/codemods/upgrade_sslcontext_tls.py index cc2ed432..8cd4f51b 100644 --- a/src/codemodder/codemods/upgrade_sslcontext_tls.py +++ b/src/codemodder/codemods/upgrade_sslcontext_tls.py @@ -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 diff --git a/src/codemodder/codemods/url_sandbox.py b/src/codemodder/codemods/url_sandbox.py index 3acfcf4d..4fee3e11 100644 --- a/src/codemodder/codemods/url_sandbox.py +++ b/src/codemodder/codemods/url_sandbox.py @@ -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] ] = {} diff --git a/tests/codemods/test_base_visitor.py b/tests/codemods/test_base_visitor.py index 8cd90f1e..1c6627e3 100644 --- a/tests/codemods/test_base_visitor.py +++ b/tests/codemods/test_base_visitor.py @@ -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