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

delete_all_statements_after_return isn't cleaning up multiple lines after return #652

Closed
Bennet-Sunder opened this issue Mar 19, 2024 · 3 comments

Comments

@Bennet-Sunder
Copy link
Contributor

Bennet-Sunder commented Mar 19, 2024

For this java file.

class SampleJava {
  public void sampleMethod(ExperimentInterface exp) {
    if (exp.isToggleEnabled(SAMPLE_STALE_FLAG)) {
      return true;
    }
    call_method();
    call_another_method();
  }
}

When I clean this up, It only cleans up the first line and not the second line after return.

Expected output

class SampleJava {
  public void sampleMethod(ExperimentInterface exp) {
     return true;
  }
}

Actual output

class SampleJava {
  public void sampleMethod(ExperimentInterface exp) {
    return true;
    
    call_another_method();
  }
}

These are the parameters passed

-s stale_flag_name=SAMPLE_STALE_FLAG -s treated=true -s treated_complement=false

When running the script with trace enabled I see that @post variable captures both the lines after return but

Found match  Match {
        matched_string: "call_method();",
        range: Range {
            start_byte: 695,
            end_byte: 709,
            start_point: Point {
                row: 14,
                column: 4,
            },
            end_point: Point {
                row: 14,
                column: 18,
            },
        },
        matches: {
            "pre": "",
            "b": "{\n    return true;\n    call_method();\n    call_another_method();\n  }",
            "r": "return true;",
            "post": "call_method();\ncall_another_method();",
        },
        associated_comma: None,
        associated_comments: [],
    }

And same is with the rewrite.

[2024-03-19T07:36:29Z TRACE polyglot_piranha::models::edit] Rewrite found : Edit {
        p_match: Match {
            matched_string: "call_method();",
            range: Range {
                start_byte: 695,
                end_byte: 709,
                start_point: Point {
                    row: 14,
                    column: 4,
                },
                end_point: Point {
                    row: 14,
                    column: 18,
                },
            },
            matches: {
                "r": "return true;",
                "post": "call_method();\ncall_another_method();",
                "pre": "",
                "b": "{\n    return true;\n    call_method();\n    call_another_method();\n  }",
            },
            associated_comma: None,
            associated_comments: [],
        },
        replacement_string: "",
        matched_rule: "delete_all_statements_after_return",
    }

here the @post variable captures is "call_method();\ncall_another_method();" but the matches string is only the first line "call_method();" which gets replaces with an empty string.

However if I modify the delete_all_statements_after_return rule and set the seed_rule as true, then it works as expected.

Any idea on how this can be fixed?

@Bennet-Sunder
Copy link
Contributor Author

@dvmarcilio @ketkarameya any inputs here would be helpful.

@ketkarameya
Copy link
Collaborator

Add a Parent edge from delete_all_statements_after_return to delete_all_statements_after_return

@Bennet-Sunder
Copy link
Contributor Author

Thanks @ketkarameya.

This worked perfectly fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants