diff --git a/src/codemodder/codemods/api/__init__.py b/src/codemodder/codemods/api/__init__.py index d3fc90f9c..c4944d5e9 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 82c628935..e717864a3 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 a578dc579..81b36c638 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 f03886107..64b6b6420 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 0af67284d..feafb5bfe 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 18ffad7f0..8f8d3d225 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 69ee49c2e..b88538295 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 52e14d487..439030f4c 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 cc2ed432a..8cd4f51bf 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 3acfcf4da..4fee3e114 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 8cd90f1ec..1c6627e33 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