-
Notifications
You must be signed in to change notification settings - Fork 196
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
Seeking help on feature cleanup #693
Comments
Can you post what code piranha produced by enabling debug mode? Looking at your rule, I can see a potential problem remove_feature_toggle_if_else = Rule(
name="remove_feature_toggle_if_else",
query="""(
(if_statement
condition: (identifier) @id
consequence: (block) @if_block
alternative: (block) @else_block
) @if_stmt
(#eq? @id "FEATURE_SEQUENTIAL_NUMBERS_FOR_WARRANTY")
)""",
replace_node="else_statement", # <——— this is not defined, you probably meant ‘else_block’
replace=""
is_seed_rule=True
) I suggest you use concrete syntax over tree sitter queries for simpler rules remove_feature_toggle_if_else = Rule(
name="remove_feature_toggle_if_else",
query="""cs if(FEATURE_SEQUENTIAL_NUMBERS_FOR_WARRANTY) :[if_block] else :[else_block]""",
replace_node="*", # everything
replace=":[if_block]"
is_seed_rule=True
) Take a look at our demo examples to get the gist of how it works (e.g., https://github.com/uber/piranha/blob/master/demo/stale_feature_flag_cleanup_demos_using_py_api.py) |
@danieltrt Thanks for your help I have below code from os.path import join, dirname, getmtime, exists feature_flag_dir = dirname(file) def run_java_ff_demo():
)
)
run_java_ff_demo() code before public void testFeatureToggle() { after public void testFeatureToggle() { |
To get rid of the extra braces, you need to unroll the pattern a bit more. Rule(...,
query=""" cs
if(isToggleEnabled(TenantConfigurationKey.FEATURE_SEQUENTIAL_NUMBERS_FOR_WARRANTY)) {
:[if_block+]
} else {
:[else_block+]
}",
...) Alternatively, simply replace with # Define the rule to replace the method call
r1 = Rule(
name="replace_method",
query="cs isToggleEnabled(TenantConfigurationKey.FEATURE_SEQUENTIAL_NUMBERS_FOR_WARRANTY)", # cs indicates we are using concrete syntax
replace_node="*",
replace="true",
is_seed_rule=True
)
# Define the edges for the rule graph.
# In this case, boolean_literal_cleanup is already defined for java [see src/cleanup_rules]
edge = OutgoingEdges("replace_method", to=["boolean_literal_cleanup"], scope="Parent")
# Create Piranha arguments
piranha_arguments = PiranhaArguments(
code_snippet=code,
language="java",
rule_graph=RuleGraph(rules=[r1], edges=[edge])
) |
Thanks @danieltrt Below rule worked for me remove_feature_toggle_if_else = Rule( I have another use case where in i want to remove test methods when my FEATURE_SEQUENTIAL_NUMBERS_FOR_WARRANTY is false @test
} and in below test I want to remove the when line
} Can you please suggest the rule |
@danieltrt I am having challenges to match my rule with Code @test test_method_match = Rule( when I am run piranha this rule can not able to match the code can you tell me what is the issue an how to verify my rule matches the code |
Hi Team,
Thanks in advance
We have features defined in enum file eg:
FEATURE_SEQUENTIAL_NUMBERS_FOR_WARRANTY("feature.2024.09.gpd.35102-warranty-sequence-number")
and we are using it
public String get(String requestNumberPrefix, String request) {
if (Boolean.parseBoolean(tenantConfiguration.get(TenantConfigurationKey.FEATURE_SEQUENTIAL_NUMBERS_FOR_WARRANTY))) {
log.info("request number prefix is {}", requestNumberPrefix);
Long nextSequenceId = getNextSequenceIdAndSave(requestNumberPrefix, request);
String sequenceNumber = String.format(REQUEST_NUMBER_FORMAT, nextSequenceId);
return requestNumberPrefix + sequenceNumber;
} else {
return generateRandomNumber();
}
}
now this feature FEATURE_SEQUENTIAL_NUMBERS_FOR_WARRANTY is enabled
I want to clean up this feature
I have below rules created
remove_feature_toggle_if_else = Rule(
name="remove_feature_toggle_if_else",
query="""(
(if_statement
condition: (identifier) @id
consequence: (block) @if_block
alternative: (block) @else_block
) @if_stmt
(#eq? @id "FEATURE_SEQUENTIAL_NUMBERS_FOR_WARRANTY")
)""", # Tree-sitter query to find the if-else block checking the feature toggle
replace_node="else_statement",
replace="", # Remove the entire if-else block
is_seed_rule=True
)
Set up Piranha arguments
piranha_arguments = PiranhaArguments(
code_snippet=path_to_codebase,
language="java",
rule_graph=RuleGraph(rules=[remove_feature_toggle_if_else], edges=[])
)
Execute Piranha
Execute Piranha
try:
piranha_summary = execute_piranha(piranha_arguments)
except Exception as e:
print("Error executing Piranha:", e)
when running python run_piranha.py I am getting below error
thread '' panicked at src\models\source_code_unit.rs:344:5:
Produced syntactically incorrect source code
I could not able to find what is the issue here and any help is much appreciated
The text was updated successfully, but these errors were encountered: