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

Cleanup code after bug fix related to associated comma #574

Closed
wants to merge 1 commit into from
Closed
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 {

// Should not delete this comment and the func below
t.Run("message", func(t *Foobar.T) {
fmt.Println("some logging statement")
})

// Should not delete this comment and the func below
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 {
// Should not delete this comment and the func below
t.Run("message", func(t *Foobar.T) {
isFlgEnabled := exp.BoolValue(staleFlagConst)
fmt.Println("some logging statement")
})

// Should not delete this comment and the func below
t.Run1(func(t *Foobar.T) {
isFlgEnabled1 := exp.BoolValue(staleFlagConst)
fmt.Println("some other logging statement")
}, "other_message" )


isFlagEnabledFunc := exp.BoolValue(staleFlagConst)

if !isFlagEnabledFunc {
Expand Down
Loading