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

Use 1-based line numbers for RegexTransformer changes #759

Merged
merged 2 commits into from
Jul 30, 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
10 changes: 7 additions & 3 deletions src/codemodder/codemods/regex_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@


class RegexTransformerPipeline(BaseTransformerPipeline):
pattern: Pattern
pattern: Pattern | str
replacement: str
change_description: str

def __init__(self, pattern: Pattern, replacement: str, change_description: str):
def __init__(
self, pattern: Pattern | str, replacement: str, change_description: str
):
super().__init__()
self.pattern = pattern
self.replacement = replacement
Expand All @@ -27,6 +29,7 @@ def apply(
file_context: FileContext,
results: list[Result] | None,
) -> ChangeSet | None:
del results
Copy link
Contributor

Choose a reason for hiding this comment

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

What's the significance of deleting this reference? Is it to communicate that this parameter is intentionally unused and only exists to satisfy the API requirements of the caller?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes that's right. It makes explicit to the to the type checker that this parameter is intentionally unused.


changes = []
updated_lines = []
Expand All @@ -35,12 +38,13 @@ def apply(
original_lines = f.readlines()

for lineno, line in enumerate(original_lines):
# TODO: use results to filter out which lines to change
changed_line = re.sub(self.pattern, self.replacement, line)
updated_lines.append(changed_line)
if line != changed_line:
changes.append(
Change(
lineNumber=lineno,
lineNumber=lineno + 1,
description=self.change_description,
findings=file_context.get_findings_for_location(lineno),
)
Expand Down
6 changes: 6 additions & 0 deletions src/codemodder/codetf.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ class Change(BaseModel):
packageActions: Optional[list[PackageAction]] = None
findings: Optional[list[Finding]] = None

@model_validator(mode="after")
def validate_lineNumber(self):
if self.lineNumber < 1:
raise ValueError("lineNumber must be greater than 0")
return self


class AIMetadata(BaseModel):
provider: Optional[str] = None
Expand Down
5 changes: 5 additions & 0 deletions tests/test_codetf.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ def test_change_diffside(side):
assert change.model_dump()["diffSide"] == side


def test_change_invalid_line_number():
with pytest.raises(ValueError):
Change(lineNumber=0, description="Change 1 to 2")


def test_write_codetf(tmpdir, mocker, codetf_schema):
path = tmpdir / "test.codetf.json"

Expand Down
5 changes: 3 additions & 2 deletions tests/test_regex_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def test_transformer_no_change(mocker, caplog, tmp_path_factory):
dry_run=True,
verbose=False,
registry=mocker.MagicMock(),
providers=None,
providers=mocker.MagicMock(),
repo_manager=mocker.MagicMock(),
path_include=[],
path_exclude=[],
Expand Down Expand Up @@ -53,7 +53,7 @@ def test_transformer(mocker, tmp_path_factory):
dry_run=False,
verbose=False,
registry=mocker.MagicMock(),
providers=None,
providers=mocker.MagicMock(),
repo_manager=mocker.MagicMock(),
path_include=[],
path_exclude=[],
Expand All @@ -69,3 +69,4 @@ def test_transformer(mocker, tmp_path_factory):
)
assert changeset is not None
assert code.read_text() == text.replace("hello", "bye")
assert changeset.changes[0].lineNumber == 1
Loading