-
Notifications
You must be signed in to change notification settings - Fork 197
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
Kotlin cleanup rules to support !true &&
and !false ||
expressions
#694
base: master
Are you sure you want to change the base?
Kotlin cleanup rules to support !true &&
and !false ||
expressions
#694
Conversation
- simplify not true in conjunction expression - simplify not true in disjunction expression
Hi @daveight The problem here is not |
Hello, I totally agree with you. However for my project introducing this Is there a way to configure this order in Piranha for an external rule? |
I think you can just inject these custom rules at run-time instead. from polyglot_piranha import execute_piranha, PiranhaArguments, Rule, RuleGraph, OutgoingEdges
# Toy code snippet
code = """
class Sample {
fun test_fn() {
if (!feature.isEnabled(FEATURE_A) && condition != "constant") {
// if branch
} else {
// else branch
}
}
}
"""
r1 = Rule(
name="replace_method",
query="""cs !:[x].isEnabled(@stale_flag_name) && :[rest+]
""", # if :[x].isEnabled(@stale_flag_name) is now permanently true
replace_node="*",
replace="false",
holes={"stale_flag_name"},
is_seed_rule=True
)
edge = OutgoingEdges("replace_method", to=["boolean_literal_cleanup"], scope="Parent")
# Create Piranha arguments
piranha_arguments = PiranhaArguments(
code_snippet=code,
language="kt",
rule_graph=RuleGraph(rules=[r1], edges=[edge],),
substitutions={"stale_flag_name": "FEATURE_A"}
)
piranha_summary = execute_piranha(piranha_arguments)
print(piranha_summary[0].content) But to answer your question, polyglotpiranha works with a stack of global rules. The ones that show up first in the list are executed first. |
@daveight Did it work for you? |
Hello @danieltrt thanks for the reply. Is there a reference to learn this syntax? I am using tree-sitter queries, but this syntax I am not aware of. Thanks. |
I noticed an issue when processing boolean expressions:
if (!feature.isEnabled(FEATURE_A) && condition != "constant")
Piranha refactors it like this:
if (!condition != "constant")
which is syntactically and logically incorrectI implemented two additional rules to fix it:
!true && ...
=> the whole boolean expression will be refactored tofalse
!false || ...
=> the whole boolean expression will be refactored totrue
Looking forward to a feedback / review. Thanks!