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

Bug Fix: Avoid over-deletion when looking for associated comma #573

Merged
merged 3 commits into from
Aug 22, 2023
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
39 changes: 36 additions & 3 deletions src/models/matches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,11 @@ impl Match {
} else {
current_node.prev_sibling()
} {
let content = sibling.utf8_text(code.as_bytes()).unwrap();
// Check if the sibling is a comment
if !found_comma && content.trim().eq(",") {
// Check if the sibling is a comma
if !found_comma
&& self.is_comma(code, &sibling)
&& self.is_comma_safe_to_delete(&sibling, trailing)
{
// Add the comma to the associated matches
self.associated_comma = Some(sibling.range().into());
current_node = sibling;
Expand Down Expand Up @@ -160,6 +162,37 @@ impl Match {
*piranha_arguments.cleanup_comments()
&& piranha_arguments.language().comment_nodes().contains(&kind)
}
// Checks if the given node is a comma
fn is_comma(&self, code: &str, node: &Node) -> bool {
let content = node.utf8_text(code.as_bytes()).unwrap();
return content.trim().eq(",");
}

/// Checks whether it is safe to delete the provided comma node, by checking if
/// no neighbor node lies between the comma and the of nodes intended to be deleted.
///
/// When `trailing` is true, it considers the previous node sibling (wrt the comma
/// Otherwise, it considers the next sibling (wrt the comma)
fn is_comma_safe_to_delete(&self, comma: &Node, trailing: bool) -> bool {
let (start_range, end_range) = self.get_first_and_last_associated_ranges();

if trailing {
if let Some(prev_node) = comma.prev_sibling() {
// Ensure that the previous node's end byte is not beyond the deleted node's end byte
if prev_node.end_byte() > end_range.end_byte {
return false;
}
}
} else if let Some(next_node) = comma.next_sibling() {
// Ensure that the next node's start byte is not before the deleted node's start byte
if next_node.start_byte() < start_range.start_byte {
return false;
}
}

// It is safe to delete the comma node
true
}

/// Checks if the given comment is safe to delete.
fn _is_comment_safe_to_delete(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,18 @@ func callerFunc() {

// should not replace the function name
func isFlagEnabledFunc() bool {

// After deleting the assignment, this comment and the leading `func {...` is not deleted
t.Run("message", func(t *Foobar.T) {
fmt.Println("some logging statement")
})

// After deleting the assignment, this comment and the trailing `},` is not deleted
t.Run1(func(t *Foobar.T) {
fmt.Println("some other logging statement")
}, "other_message" )


fmt.Println("not enabled")
return false
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,19 @@ func callerFunc() {

// should not replace the function name
func isFlagEnabledFunc() bool {
// After deleting the assignment, this comment and the leading `func {...` is not deleted
t.Run("message", func(t *Foobar.T) {
isFlgEnabled := exp.BoolValue(staleFlagConst)
fmt.Println("some logging statement")
})

// After deleting the assignment, this comment and the trailing `},` is not deleted
t.Run1(func(t *Foobar.T) {
fmt.Println("some other logging statement")
isFlgEnabled1 := exp.BoolValue(staleFlagConst)
}, "other_message" )
Copy link
Contributor

Choose a reason for hiding this comment

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

In both these examples, the comma is before the deleted node, but the change handles both before and after, right? Any test case case you can think of for the "after" case?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

aah I see ur point. I actually had a test case like that, but I had it slightly wrong. I reordered the statements here.
In our earlier logic we would delete }, in addition to isFlgEnabled1 := exp.BoolValue(staleFlagConst)

But now we don't, because we see that there is a node which ends between the ending of deleted node and comma

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok, but this change makes the comment above outdated, not?

// Should not delete this comment and the func below

It should now say something about the trailing },

Copy link
Collaborator Author

@ketkarameya ketkarameya Aug 22, 2023

Choose a reason for hiding this comment

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

good catch. Addressed. 0c68a1f



isFlagEnabledFunc := exp.BoolValue(staleFlagConst)

if !isFlagEnabledFunc {
Expand Down
Loading