-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Alias detection solution for codemods #106
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
from codemodder.codemods.base_codemod import ReviewGuidance | ||
from codemodder.codemods.api import SemgrepCodemod | ||
from codemodder.codemods.utils_mixin import NameResolutionMixin | ||
|
||
|
||
class UpgradeSSLContextMinimumVersion(SemgrepCodemod): | ||
class UpgradeSSLContextMinimumVersion(SemgrepCodemod, NameResolutionMixin): | ||
NAME = "upgrade-sslcontext-minimum-version" | ||
REVIEW_GUIDANCE = ReviewGuidance.MERGE_WITHOUT_REVIEW | ||
SUMMARY = "Upgrade SSLContext Minimum Version" | ||
|
@@ -19,6 +20,8 @@ class UpgradeSSLContextMinimumVersion(SemgrepCodemod): | |
}, | ||
] | ||
|
||
_module_name = "ssl" | ||
|
||
@classmethod | ||
def rule(cls): | ||
return """ | ||
|
@@ -45,6 +48,11 @@ def rule(cls): | |
""" | ||
|
||
def on_result_found(self, original_node, updated_node): | ||
maybe_name = self.get_aliased_prefix_name( | ||
original_node.value, self._module_name | ||
) | ||
maybe_name = maybe_name or self._module_name | ||
if maybe_name == self._module_name: | ||
self.add_needed_import(self._module_name) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. these new green lines added are the same (or almost?) in every codemod added so maybe make this a separate method to call to dedupe There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These address a very specific problem that are shared by those codemods. I don't want to add it to the api because the solution needs Metadata is calculated at the start of every transform on a as-need basis (libcst looks at Maybe creating a class for those types of codemods would be a good idea, but I'd rather do that when we try to rewrite those in pure libcst. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree the duplication here is not ideal. I think we need to revisit the whole concept of utility mixins for this reason, although I understand the reasons it was done this way. |
||
self.remove_unused_import(original_node) | ||
self.add_needed_import("ssl") | ||
return self.update_assign_rhs(updated_node, "ssl.TLSVersion.TLSv1_2") | ||
return self.update_assign_rhs(updated_node, f"{maybe_name}.TLSVersion.TLSv1_2") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
surprised pylint nor mypy didn't complain but all returns of a func should return the same len, so this should be (None, None)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The return type is annotated as
Optional[Tuple[...]]
, that's the reason.Returning
(None, None)
would mess withNone
checks sincebool((None,None)) == True
.