Skip to content

Commit

Permalink
fix missing self or cls should have node selection logic
Browse files Browse the repository at this point in the history
  • Loading branch information
clavedeluna committed Mar 29, 2024
1 parent 5a4100a commit 99d35b2
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/core_codemods/fix_missing_self_or_cls.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ class FixMissingSelfOrClsTransformer(
def leave_FunctionDef(
self, original_node: cst.FunctionDef, updated_node: cst.FunctionDef
) -> cst.FunctionDef:
# TODO: add filter by include or exclude that works for nodes
# that that have different start/end numbers.
if not self.node_is_selected(original_node):
return original_node

if not self.find_immediate_class_def(original_node):
# If `original_node` is not inside a class, nothing to do.
Expand Down
16 changes: 16 additions & 0 deletions tests/codemods/test_fix_missing_self_or_cls.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,19 @@ def kls(**kwargs):
pass
"""
self.run_and_assert(tmpdir, input_code, input_code)

def test_exclude_line(self, tmpdir):
input_code = (
expected
) = """
class A:
def method():
pass
"""
lines_to_exclude = [3]
self.run_and_assert(
tmpdir,
input_code,
expected,
lines_to_exclude=lines_to_exclude,
)
16 changes: 8 additions & 8 deletions tests/codemods/test_sonar_fix_missing_self_or_cls.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@ def test_name(self):
def test_simple(self, tmpdir):
input_code = """
class A:
def method():
def instance_method():
pass
@classmethod
def clsmethod():
def class_method():
pass
"""
expected_output = """
class A:
def method(self):
def instance_method(self):
pass
@classmethod
def clsmethod(cls):
def class_method(cls):
pass
"""
issues = {
Expand All @@ -37,8 +37,8 @@ def clsmethod(cls):
"status": "OPEN",
"component": "code.py",
"textRange": {
"startLine": 2,
"endLine": 2,
"startLine": 3,
"endLine": 3,
"startOffset": 4,
"endOffset": 25,
},
Expand All @@ -48,8 +48,8 @@ def clsmethod(cls):
"status": "OPEN",
"component": "code.py",
"textRange": {
"startLine": 6,
"endLine": 6,
"startLine": 7,
"endLine": 7,
"startOffset": 4,
"endOffset": 22,
},
Expand Down
84 changes: 83 additions & 1 deletion tests/samples/sonar_issues.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"total":35,
"total":37,
"p":1,
"ps":500,
"paging":{
Expand All @@ -10,6 +10,78 @@
"effortTotal":1010,
"debtTotal":1010,
"issues":[
{
"key": "AY5_zeRwqkyzjj5fBCfH",
"rule": "python:S5719",
"severity": "BLOCKER",
"component": "pixee_codemodder-python:fix_missing_self_or_cls.py",
"project": "pixee_codemodder-python",
"line": 2,
"hash": "755b45e1ce3925b38846166df4cf5336",
"textRange": {
"startLine": 2,
"endLine": 2,
"startOffset": 4,
"endOffset": 25
},
"flows": [],
"status": "OPEN",
"message": "Add a \"self\" or class parameter",
"effort": "5min",
"debt": "5min",
"author":"[email protected]",
"tags":[
"tests"
],
"creationDate": "2024-03-27T13:05:32+0100",
"updateDate": "2024-03-27T13:05:53+0100",
"type": "BUG",
"organization": "pixee",
"cleanCodeAttribute": "LOGICAL",
"cleanCodeAttributeCategory": "INTENTIONAL",
"impacts": [
{
"softwareQuality": "RELIABILITY",
"severity": "HIGH"
}
]
},
{
"key": "AY5_zeRwqkyzjj5fBCfI",
"rule": "python:S5719",
"severity": "BLOCKER",
"component": "pixee_codemodder-python:fix_missing_self_or_cls.py",
"project": "pixee_codemodder-python",
"line": 6,
"hash": "cd77b347ec74d1b10e372774147ea204",
"textRange": {
"startLine": 6,
"endLine": 6,
"startOffset": 4,
"endOffset": 22
},
"flows": [],
"status": "OPEN",
"message": "Add a class parameter",
"effort": "5min",
"debt": "5min",
"author":"[email protected]",
"tags":[
"tests"
],
"creationDate": "2024-03-27T13:05:32+0100",
"updateDate": "2024-03-27T13:05:53+0100",
"type": "BUG",
"organization": "pixee",
"cleanCodeAttribute": "LOGICAL",
"cleanCodeAttributeCategory": "INTENTIONAL",
"impacts": [
{
"softwareQuality": "RELIABILITY",
"severity": "HIGH"
}
]
},
{
"key":"AY079772vDBaEBzdYL0s",
"rule":"python:S5905",
Expand Down Expand Up @@ -2407,6 +2479,16 @@
"name":"test_ancestorpatterns_mixin.py",
"longName":"tests/test_ancestorpatterns_mixin.py",
"path":"tests/test_ancestorpatterns_mixin.py"
},
{
"organization":"pixee",
"key":"pixee_codemodder-python:fix_missing_self_or_cls.py",
"uuid":"AY0797sgvDBaEBzdYL0o",
"enabled":true,
"qualifier":"FIL",
"name":"fix_missing_self_or_cls.py",
"longName":"fix_missing_self_or_cls.py",
"path":"fix_missing_self_or_cls.py"
}
],
"organizations":[
Expand Down

0 comments on commit 99d35b2

Please sign in to comment.