Skip to content

Conversation

dvmarcilio
Copy link
Contributor

@dvmarcilio dvmarcilio commented Dec 2, 2022

Problem

Java delete_all_statements_after_return is only working for the 1st statement.
From the code below (considering that the experimentation.isToggleEnabled() call is replaced by false):

public String multiple_stmt_after_return() {
  if (!experimentation.isToggleEnabled(TestExperimentName.STALE_FLAG)) {
    return "not enabled";
  }

  System.out.println("remove 1");
  System.out.println("remove 2");
  System.out.println("remove 3");
  System.out.println("remove 4");
  System.out.println("remove 5");
  return "enabled";
}

We would expect the following:

public String multiple_stmt_after_return() {
  return "not enabled";
}

Instead, we are getting:

public String multiple_stmt_after_return() {
  return "not enabled";

  
  System.out.println("remove 2");
  System.out.println("remove 3");
  System.out.println("remove 4");
  System.out.println("remove 5");
  return "enabled";
}

Discussion

This is the query for delete_all_statements_after_return:

(
  (block  
    ((statement)* @pre)
    ((return_statement) @r)
    ((statement)+ @post)
  ) @b
)

For the code discussed in the Problem section, this is the match we get using tree-sitter CLI:

pattern: 0
  capture: b, start: (0, 43), end: (9, 1)
  capture: 1 - r, start: (1, 2), end: (1, 23), text: `return "not enabled";`
  capture: 2 - post, start: (3, 2), end: (3, 33), text: `System.out.println("remove 1");`
  capture: 2 - post, start: (4, 2), end: (4, 33), text: `System.out.println("remove 2");`
  capture: 2 - post, start: (5, 2), end: (5, 33), text: `System.out.println("remove 3");`
  capture: 2 - post, start: (6, 2), end: (6, 33), text: `System.out.println("remove 4");`
  capture: 2 - post, start: (7, 2), end: (7, 33), text: `System.out.println("remove 5");`
  capture: 2 - post, start: (8, 2), end: (8, 19), text: `return "enabled";`

So ((statement)+ @post) produces multiple one-line matches (same start/end row).
Perhaps the internal replace implementation expects a multi-line match for @post (as in (block) @b)?

Possible Solution

The self-edge solution from #265 would also work here, not requiring any modifications to the rust code.

[[edges]]
scope = "Parent"
from = "delete_all_statements_after_return"
to = ["delete_all_statements_after_return"]

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

Successfully merging this pull request may close these issues.

2 participants