Skip to content

Commit

Permalink
add include/exclude check to codemods that did not have it
Browse files Browse the repository at this point in the history
  • Loading branch information
clavedeluna committed Jan 9, 2024
1 parent 269469d commit 5b733dc
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/core_codemods/django_receiver_on_top.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ def leave_FunctionDef(
) -> Union[
cst.BaseStatement, cst.FlattenSentinel[cst.BaseStatement], cst.RemovalSentinel
]:
if not self.filter_by_path_includes_or_excludes(
self.node_position(original_node)
):
return original_node

maybe_receiver_with_index = None
for i, decorator in enumerate(original_node.decorators):
true_name = self.find_base_name(decorator.decorator)
Expand Down
5 changes: 5 additions & 0 deletions src/core_codemods/exception_without_raise.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ def leave_SimpleStatementLine(
) -> Union[
cst.BaseStatement, cst.FlattenSentinel[cst.BaseStatement], cst.RemovalSentinel
]:
if not self.filter_by_path_includes_or_excludes(
self.node_position(original_node)
):
return original_node

match original_node:
case cst.SimpleStatementLine(
body=[cst.Expr(cst.Name() | cst.Attribute() as name)]
Expand Down
5 changes: 5 additions & 0 deletions src/core_codemods/fix_deprecated_abstractproperty.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ class FixDeprecatedAbstractproperty(BaseCodemod, NameResolutionMixin):
def leave_Decorator(
self, original_node: cst.Decorator, updated_node: cst.Decorator
):
if not self.filter_by_path_includes_or_excludes(
self.node_position(original_node)
):
return original_node

if (
base_name := self.find_base_name(original_node.decorator)
) and base_name == "abc.abstractproperty":
Expand Down
4 changes: 4 additions & 0 deletions src/core_codemods/fix_mutable_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ def leave_FunctionDef(
updated_node: cst.FunctionDef,
):
"""Transforms function definitions with mutable default parameters"""
if not self.filter_by_path_includes_or_excludes(
self.node_position(original_node)
):
return original_node
(
updated_params,
new_var_decls,
Expand Down
12 changes: 11 additions & 1 deletion src/core_codemods/https_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ def updated_args(self, original_args):
return new_args

def update_attribute(self, true_name, original_node, updated_node, new_args):
del true_name, original_node
del true_name
if not self.filter_by_path_includes_or_excludes(
self.node_position(original_node)
):
return original_node

return updated_node.with_changes(
args=new_args,
func=updated_node.func.with_changes(
Expand All @@ -34,6 +39,11 @@ def update_attribute(self, true_name, original_node, updated_node, new_args):

def update_simple_name(self, true_name, original_node, updated_node, new_args):
del true_name
if not self.filter_by_path_includes_or_excludes(
self.node_position(original_node)
):
return original_node

AddImportsVisitor.add_needed_import(self.context, "urllib3")
RemoveImportsVisitor.remove_unused_import_by_node(self.context, original_node)
return updated_node.with_changes(
Expand Down
5 changes: 5 additions & 0 deletions src/core_codemods/remove_unnecessary_f_str.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ def _check_formatted_string(
updated_node: cst.FormattedString,
):
transformed_node = super()._check_formatted_string(_original_node, updated_node)
if not self.filter_by_path_includes_or_excludes(
self.node_position(transformed_node)
):
return transformed_node

if not _original_node.deep_equals(transformed_node):
self.report_change(_original_node)
return transformed_node
5 changes: 5 additions & 0 deletions src/core_codemods/use_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ class UseGenerator(BaseCodemod, NameResolutionMixin):
]

def leave_Call(self, original_node: cst.Call, updated_node: cst.Call):
if not self.filter_by_path_includes_or_excludes(
self.node_position(original_node)
):
return original_node

match original_node.func:
# NOTE: could also support things like `list` and `tuple`
# but it's a less compelling use case
Expand Down
15 changes: 15 additions & 0 deletions src/core_codemods/use_walrus_if.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ def visit_If(self, node: cst.If):
)

def leave_If(self, original_node, updated_node):
if not self.filter_by_path_includes_or_excludes(
self.node_position(original_node)
):
return original_node

if (result := self._if_stack.pop()) is not None:
position, named_expr = result
is_name = m.matches(updated_node.test, m.Name())
Expand All @@ -134,6 +139,11 @@ def leave_If(self, original_node, updated_node):

def leave_Assign(self, original_node: cst.Assign, updated_node: cst.Assign):
del updated_node
if not self.filter_by_path_includes_or_excludes(
self.node_position(original_node)
):
return original_node

if named_expr := self.assigns.get(original_node):
position = self.node_position(original_node)
self._modify_next_if.append((position, named_expr))
Expand All @@ -147,6 +157,11 @@ def leave_SimpleStatementLine(self, original_node, updated_node):
This feels like a bug in libCST but we'll work around it for now.
"""
if not self.filter_by_path_includes_or_excludes(
self.node_position(original_node)
):
return original_node

if not updated_node.body:
trailing_whitespace = (
(
Expand Down

0 comments on commit 5b733dc

Please sign in to comment.