-
Notifications
You must be signed in to change notification settings - Fork 201
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
Go Cleanup Built-in Rules #265
Conversation
ee5aa14
to
1db699d
Compare
1db699d
to
df23eba
Compare
(binary_expression | ||
left: [(true) (parenthesized_expression (true))] | ||
operator:"&&" | ||
right : (_)* @rhs |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need *
? There has to be one RHS in the binary expression right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. Thanks!
( | ||
(binary_expression | ||
left: ( | ||
[ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if we just use a wildcard for @lhs
and @rhs
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. Thanks!
replace = "@nested.statements" | ||
replace_node = "nested.block" | ||
|
||
# This rule is not deleting multiple statements after return. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IS that some issue with the grammar? Can we look into it ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can now successfully match all statements after return_statement
in a similar way as e.g., Java and Javascript.
Accounting for the \n
terminator did the trick:
(((statement) "\\n"?)+ @post)
However, replace is only working for the first statement. This is also happening in Java (draft PR #284).
The self-edge makes it work for the current implementation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use dummy node
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Introduced dummy node return_cleanup
and now we have a cycle
c10f4a8
to
50e4937
Compare
query = """ | ||
( | ||
(binary_expression | ||
left : (_)* @lhs |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
left : (_)* @lhs | |
left : (_) @lhs |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
) | ||
""" | ||
queries = [ | ||
""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add more constraints.
Delete var declaration if all assignments' rhs
eq init
of var decl;
Delete all assignments if no var decl exists in scope
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It turns out that this delete_parent_assignment
rule was doing nothing. I removed it, and everything is working as expected.
Well-spotted that the constraint was not working.
50e4937
to
db4c6e6
Compare
# | ||
# Note that this rule *won't* rewrite when @lhs is a call. | ||
[[rules]] | ||
groups = ["boolean_expression_simplify"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have to think of a strategy to prevent it from simplifying after <something> || true
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ketkarameya should we deal with this in this PR? or do we open a new issue after this is accepted?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think a follow up would be ok. We can address this alongside the fix for java/kt
##### | ||
# Dummy rule to introduce a cycle for `delete_statement_after_return` | ||
[[rules]] | ||
name = "return_cleanup" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think there is some naming convention for dummy rules in the readme
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Renamed to return_statement_cleanup
holes = ["stale_flag_name"] | ||
|
||
[[rules]] | ||
name = "replace_identifier_with_str_literal" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what is the purpose of this rule (And the above one)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the review, @ketkarameya. I agree that this rule is just noise. I restructured this test to have two rules:
find_const_str_literal
: finds a constant (@const_id
with a value equal tostale_flag_name
piranha_argument.false_flag
: replaces withfalse
calls toBoolValue()
with anidentifier
argument equal to@const_id
(hole).
Now the test doesn't remove the constant declaration.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have left a small comment on the tests. Otherwise this PR looks good to me.
...ot/piranha/test-resources/go/feature_flag/system_1/const_same_file/configurations/rules.toml
Outdated
Show resolved
Hide resolved
- Having the `\n` terminator reproduces the exact matching as languages such as Java and JavaScript
- `@treated` for flag value - `groups` in the end of rule definition - rule that matches first declared first - `false_flag` -> `update_feature_flag_api`
ab1c9fb
to
4ce2547
Compare
Ongoing work on writing built-in cleanup rules for Go.