Skip to content

Commit

Permalink
Added line includes excludes to SQLQueryParameterization
Browse files Browse the repository at this point in the history
  • Loading branch information
andrecsilva committed Oct 24, 2023
1 parent b24e489 commit 27e846b
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/core_codemods/sql_parameterization.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@
CodemodMetadata,
ReviewGuidance,
)
from codemodder.codemods.base_visitor import UtilsMixin
from codemodder.codemods.transformations.remove_empty_string_concatenation import (
RemoveEmptyStringConcatenation,
)
from codemodder.codemods.utils import Append, ReplaceNodes, get_function_name_node
from codemodder.codemods.utils_mixin import NameResolutionMixin
from codemodder.context import CodemodExecutionContext
from codemodder.file_context import FileContext

parameter_token = "?"

Expand All @@ -30,7 +33,7 @@
literal = literal_number | literal_string


class SQLQueryParameterization(BaseCodemod, Codemod):
class SQLQueryParameterization(BaseCodemod, UtilsMixin, Codemod):
SUMMARY = "Parameterize SQL queries."
METADATA = CodemodMetadata(
DESCRIPTION=SUMMARY,
Expand All @@ -55,12 +58,19 @@ class SQLQueryParameterization(BaseCodemod, Codemod):
ParentNodeProvider,
)

def __init__(self, context: CodemodContext, *codemod_args) -> None:
def __init__(
self,
context: CodemodContext,
execution_context: CodemodExecutionContext,
file_context: FileContext,
*codemod_args,
) -> None:
self.changed_nodes: dict[
cst.CSTNode, cst.CSTNode | cst.RemovalSentinel | cst.FlattenSentinel
] = {}
BaseCodemod.__init__(self, execution_context, file_context, *codemod_args)
UtilsMixin.__init__(self, context, {})
Codemod.__init__(self, context)
BaseCodemod.__init__(self, *codemod_args)

def _build_param_element(self, middle, index: int) -> cst.BaseExpression:
# TODO maybe a parameterized string would be better here
Expand All @@ -83,6 +93,11 @@ def transform_module_impl(self, tree: cst.Module) -> cst.Module:

result = tree
for call, query in find_queries.calls.items():
# filter by line includes/excludes
call_pos = self.node_position(call)
if not self.filter_by_path_includes_or_excludes(call_pos):
break

Check warning on line 99 in src/core_codemods/sql_parameterization.py

View check run for this annotation

Codecov / codecov/patch

src/core_codemods/sql_parameterization.py#L99

Added line #L99 was not covered by tests

ep = ExtractParameters(self.context, query)
tree.visit(ep)

Expand Down Expand Up @@ -342,6 +357,7 @@ def _is_injectable(self, expression: cst.CSTNode) -> bool:
return True

def _is_literal_start(self, node: cst.CSTNode, modulo_2: int) -> bool:
# TODO limited for now, won't include cases like "name = 'username_" + name + "_tail'"
match node:
case cst.SimpleString():
prefix = node.prefix.lower()
Expand Down

0 comments on commit 27e846b

Please sign in to comment.