Skip to content
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

Correct node position for FuncDef nodes and fix sonar codemods #423

Merged
merged 7 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/codemodder/codemods/base_visitor.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from typing import ClassVar, Collection

import libcst as cst
from libcst import MetadataDependent
from libcst._position import CodePosition, CodeRange
from libcst.codemod import ContextAwareVisitor, VisitorBasedCodemodCommand
from libcst.metadata import PositionProvider, ProviderT

Expand Down Expand Up @@ -48,7 +50,18 @@ def node_is_selected(self, node) -> bool:

def node_position(self, node):
# See https://github.com/Instagram/LibCST/blob/main/libcst/_metadata_dependent.py#L112
return self.get_metadata(PositionProvider, node)
match node:
case cst.FunctionDef():
# By default a function's position includes the entire
# function definition. Instead, we will only use the first line
# of the function definition.
params_end = self.get_metadata(PositionProvider, node.params).end
return CodeRange(
start=self.get_metadata(PositionProvider, node).start,
end=CodePosition(params_end.line, params_end.column + 1),
)
Comment on lines +54 to +62
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rater have a general solution instead making one for every node type. I suggest changing either includes/excludes match or node_is_selected to match match any node that begins on a given line.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I agree with a more general solution like that. In that case, why do we care about start/end columns at all right now? Is there not a reason why we do column matching in match_location nearby code? If there is, then that more general solution of just matching start line doesn't make much sense.

case _:
return self.get_metadata(PositionProvider, node)

def lineno_for_node(self, node):
return self.node_position(node).start.line
Expand Down
19 changes: 0 additions & 19 deletions src/codemodder/codemods/imported_call_modifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,22 +92,3 @@ def leave_Call(self, original_node: cst.Call, updated_node: cst.Call):
)

return updated_node

def filter_by_path_includes_or_excludes(self, pos_to_match):
"""
Returns False if the node, whose position in the file is pos_to_match, matches any of the lines specified in the path-includes or path-excludes flags.
"""
# excludes takes precedence if defined
if self.line_exclude:
return not any(match_line(pos_to_match, line) for line in self.line_exclude)
if self.line_include:
return any(match_line(pos_to_match, line) for line in self.line_include)
return True

def node_position(self, node):
# See https://github.com/Instagram/LibCST/blob/main/libcst/_metadata_dependent.py#L112
return self.get_metadata(PositionProvider, node)


def match_line(pos, line):
return pos.start.line == line and pos.end.line == line
5 changes: 0 additions & 5 deletions src/codemodder/codemods/libcst_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from libcst._position import CodeRange
from libcst.codemod import CodemodContext
from libcst.codemod.visitors import AddImportsVisitor, RemoveImportsVisitor
from libcst.metadata import PositionProvider

from codemodder.codemods.base_transformer import BaseTransformerPipeline
from codemodder.codemods.base_visitor import BaseTransformer
Expand Down Expand Up @@ -98,10 +97,6 @@ def leave_ClassDef(
) -> cst.ClassDef:
return self._new_or_updated_node(original_node, updated_node)

def node_position(self, node):
# See https://github.com/Instagram/LibCST/blob/main/libcst/_metadata_dependent.py#L112
return self.get_metadata(PositionProvider, node)

def add_change(self, node, description: str, start: bool = True):
position = self.node_position(node)
self.add_change_from_position(position, description, start)
Expand Down
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
4 changes: 2 additions & 2 deletions src/core_codemods/fix_mutable_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ def leave_FunctionDef(
updated_node: cst.FunctionDef,
):
"""Transforms function definitions with mutable default parameters"""
# 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 updated_node

(
updated_params,
Expand Down
22 changes: 2 additions & 20 deletions src/core_codemods/order_imports.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import libcst as cst
from libcst.metadata import PositionProvider

from codemodder.codemods.base_visitor import UtilsMixin
from codemodder.codemods.transformations.clean_imports import (
GatherTopLevelImportBlocks,
OrderImportsBlocksTransform,
)
from core_codemods.api import Metadata, ReviewGuidance, SimpleCodemod


class OrderImports(SimpleCodemod):
class OrderImports(SimpleCodemod, UtilsMixin):
metadata = Metadata(
name="order-imports",
summary="Order Imports",
Expand Down Expand Up @@ -46,22 +47,3 @@ def transform_module_impl(self, tree: cst.Module) -> cst.Module:
)
return result_tree
return tree

def filter_by_path_includes_or_excludes(self, pos_to_match):
"""
Returns False if the node, whose position in the file is pos_to_match, matches any of the lines specified in the path-includes or path-excludes flags.
"""
# excludes takes precedence if defined
if self.line_exclude:
return not any(match_line(pos_to_match, line) for line in self.line_exclude)
if self.line_include:
return any(match_line(pos_to_match, line) for line in self.line_include)
return True

def node_position(self, node):
# See https://github.com/Instagram/LibCST/blob/main/libcst/_metadata_dependent.py#L112
return self.get_metadata(PositionProvider, node)


def match_line(pos, line):
return pos.start.line == line and pos.end.line == line
66 changes: 66 additions & 0 deletions tests/codemods/test_base_visitor.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from collections import defaultdict
from textwrap import dedent

import libcst as cst
from libcst._position import CodePosition, CodeRange
from libcst.codemod import CodemodContext
from libcst.metadata import PositionProvider

Expand Down Expand Up @@ -29,6 +31,30 @@ def leave_SimpleStatementLine(
return original_node


class AssertPositionCodemod(BaseTransformer):
METADATA_DEPENDENCIES = (PositionProvider,)

def __init__(
self,
context,
results,
expected_node_position,
line_exclude=None,
line_include=None,
):
BaseTransformer.__init__(
self, context, results, line_include or [], line_exclude or []
)
self.expected_node_position = expected_node_position

def leave_FunctionDef(
self, original_node: cst.FunctionDef, updated_node: cst.FunctionDef
) -> cst.FunctionDef:
pos_to_match = self.node_position(original_node)
assert pos_to_match == self.expected_node_position
return updated_node


class TestBaseVisitor:
def run_and_assert(self, input_code, expected, line_exclude, line_include):
input_tree = cst.parse_module(input_code)
Expand Down Expand Up @@ -59,3 +85,43 @@ def test_includes_excludes(self):
line_exclude = [1]
line_include = [1]
self.run_and_assert(input_code, expected, line_exclude, line_include)


class TestNodePosition:
def run_and_assert(self, input_code, expected_pos):
input_tree = cst.parse_module(dedent(input_code))
command_instance = AssertPositionCodemod(
CodemodContext(), defaultdict(list), expected_pos
)
command_instance.transform_module(input_tree)

def test_funcdef(self):
input_code = """
def hello():
pass
"""
expected_pos = CodeRange(
start=CodePosition(line=2, column=0), end=CodePosition(line=2, column=11)
)
self.run_and_assert(input_code, expected_pos)

def test_instance(self):
input_code = """
class MyClass:
def instance_method():
print("instance_method")
"""
expected_pos = CodeRange(
start=CodePosition(line=3, column=4), end=CodePosition(line=3, column=25)
)
self.run_and_assert(input_code, expected_pos)

def test_funcdef_args(self):
input_code = """
def hello(one, *args, **kwargs):
pass
"""
expected_pos = CodeRange(
start=CodePosition(line=2, column=0), end=CodePosition(line=2, column=31)
)
self.run_and_assert(input_code, expected_pos)
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,
)
15 changes: 15 additions & 0 deletions tests/codemods/test_fix_mutable_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,3 +292,18 @@ def foo(self, bar=None):
pass
"""
self.run_and_assert(tmpdir, input_code, expected_output)

def test_exclude_line(self, tmpdir):
input_code = (
expected
) = """
def foo(one, *args, bar=[]):
print(bar)
"""
lines_to_exclude = [2]
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
2,502 changes: 2,501 additions & 1 deletion tests/samples/sonar_issues.json

Large diffs are not rendered by default.

Loading