Skip to content

Go Cleanup Built-in Rules#265

Merged
dvmarcilio merged 56 commits into
masterfrom
go_develop
Dec 19, 2022
Merged

Go Cleanup Built-in Rules#265
dvmarcilio merged 56 commits into
masterfrom
go_develop

Conversation

@dvmarcilio

Copy link
Copy Markdown
Contributor

Ongoing work on writing built-in cleanup rules for Go.

@dvmarcilio dvmarcilio changed the title Go Cleanup Built-in Rules [WIP] Go Cleanup Built-in Rules Nov 10, 2022
@dvmarcilio dvmarcilio force-pushed the go_develop branch 3 times, most recently from ee5aa14 to 1db699d Compare November 18, 2022 18:12
Comment thread polyglot/piranha/Cargo.toml Outdated
Comment thread polyglot/piranha/README.md Outdated
Comment thread polyglot/piranha/src/cleanup_rules/go/rules.toml
Comment thread polyglot/piranha/src/cleanup_rules/go/rules.toml
(binary_expression
left: [(true) (parenthesized_expression (true))]
operator:"&&"
right : (_)* @rhs

Copy link
Copy Markdown
Collaborator

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Thanks!

Comment thread polyglot/piranha/src/cleanup_rules/go/rules.toml Outdated
(
(binary_expression
left: (
[

Copy link
Copy Markdown
Collaborator

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 ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Thanks!

Comment thread polyglot/piranha/src/cleanup_rules/go/rules.toml
replace = "@nested.statements"
replace_node = "nested.block"

# This rule is not deleting multiple statements after return.

Copy link
Copy Markdown
Collaborator

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 ?

@dvmarcilio dvmarcilio Dec 2, 2022

Copy link
Copy Markdown
Contributor Author

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 \nterminator 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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use dummy node

Copy link
Copy Markdown
Contributor Author

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

Comment thread polyglot/piranha/src/cleanup_rules/go/rules.toml Outdated
Comment thread polyglot/piranha/src/cleanup_rules/go/rules.toml
Comment thread polyglot/piranha/src/cleanup_rules/go/rules.toml Outdated
query = """
(
(binary_expression
left : (_)* @lhs

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
left : (_)* @lhs
left : (_) @lhs

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Comment thread polyglot/piranha/src/cleanup_rules/go/rules.toml Outdated
Comment thread polyglot/piranha/src/cleanup_rules/go/rules.toml
)
"""
queries = [
"""

Copy link
Copy Markdown
Collaborator

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

Copy link
Copy Markdown
Contributor Author

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.

Comment thread polyglot/piranha/src/cleanup_rules/go/rules.toml Outdated
@dvmarcilio dvmarcilio marked this pull request as ready for review December 7, 2022 13:40
@dvmarcilio dvmarcilio changed the title [WIP] Go Cleanup Built-in Rules Go Cleanup Built-in Rules Dec 7, 2022
#
# Note that this rule *won't* rewrite when @lhs is a call.
[[rules]]
groups = ["boolean_expression_simplify"]

Copy link
Copy Markdown
Collaborator

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

Copy link
Copy Markdown
Contributor Author

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?

Copy link
Copy Markdown
Collaborator

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"

Copy link
Copy Markdown
Collaborator

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

Copy link
Copy Markdown
Contributor Author

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"

@ketkarameya ketkarameya Dec 11, 2022

Copy link
Copy Markdown
Collaborator

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)?

Copy link
Copy Markdown
Contributor Author

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 to stale_flag_name piranha_argument.
  • false_flag: replaces with false calls to BoolValue() with an identifier argument equal to @const_id (hole).

Now the test doesn't remove the constant declaration.

@ketkarameya ketkarameya left a comment

Copy link
Copy Markdown
Collaborator

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.

@dvmarcilio dvmarcilio merged commit e2c9338 into master Dec 19, 2022
@dvmarcilio dvmarcilio deleted the go_develop branch December 19, 2022 18:40
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