Skip to content

Commit

Permalink
additional cleanup rules (#670)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bennet-Sunder authored Jun 2, 2024
1 parent 749fb4a commit 4bf4682
Show file tree
Hide file tree
Showing 15 changed files with 663 additions and 27 deletions.
277 changes: 271 additions & 6 deletions src/cleanup_rules/ruby/rules.toml
Original file line number Diff line number Diff line change
Expand Up @@ -343,8 +343,13 @@ name = "replace_not_false"
query = """
(
(unary
operand: (false))
@unary_expression)
[
operand: (false)
operand: (parenthesized_statements (false))
]
)
@unary_expression
)
"""
replace = "true"
replace_node = "unary_expression"
Expand All @@ -361,7 +366,11 @@ name = "replace_not_true"
query = """
(
(unary
operand: (true))
[
operand: (true)
operand: (parenthesized_statements (true))
]
)
@unary_expression)
"""
replace = "false"
Expand Down Expand Up @@ -762,6 +771,262 @@ query = """
replace = ""
replace_node = "pair"

# Before :
# after_commit :do_something, on: :create, if: Proc.new { true }
# After :
# after_commit :do_something, on: :create
#
[[rules]]
name = "remove_Proc_if_block_true"
groups = ["block_removal"]
query = """
(
(call
arguments: (argument_list
(pair
key: (hash_key_symbol) @operator
value: (call
receiver: (constant) @receiver
block: (block
body: (block_body
(true)
)
)
)
)@proc_pair
)@arguments
)@call
(#eq? @operator "if")
(#eq? @receiver "Proc")
)
"""
replace = ""
replace_node = "proc_pair"

# Before :
# after_commit :do_something, on: :create, if: proc { true }
# After :
# after_commit :do_something, on: :create
#
[[rules]]
name = "remove_proc_if_block_true"
groups = ["block_removal"]
query = """
(
(call
arguments: (argument_list
(pair
key: (hash_key_symbol) @operator
value: (call
method: (identifier) @receiver
block: (block
body: (block_body
(true)
)
)
)
)@proc_pair
)
)@call
(#eq? @operator "if")
(#eq? @receiver "proc")
)
"""
replace = ""
replace_node = "proc_pair"

# Before :
# after_commit :do_something, on: :create, if: Proc.new { false }
# After :
#
#
[[rules]]
name = "remove_Proc_if_block_false"
groups = ["block_removal"]
query = """
(
(call
arguments: (argument_list
(pair
key: (hash_key_symbol) @operator
value: (call
receiver: (constant) @receiver
block: (block
body: (block_body
(false)
)
)
)
)@proc_pair
)@arguments
)@call
(#eq? @operator "if")
(#eq? @receiver "Proc")
)
"""
replace = ""
replace_node = "call"

# Before :
# after_commit :do_something, on: :create, if: proc { false }
# After :
#
#
[[rules]]
name = "remove_proc_if_block_false"
groups = ["block_removal"]
query = """
(
(call
arguments: (argument_list
(pair
key: (hash_key_symbol) @operator
value: (call
method: (identifier) @receiver
block: (block
body: (block_body
(false)
)
)
)
)@proc_pair
)
)@call
(#eq? @operator "if")
(#eq? @receiver "proc")
)
"""
replace = ""
replace_node = "call"

# Before :
# after_commit :do_something, on: :create, unless: Proc.new { true }
# After :
#
#
[[rules]]
name = "remove_Proc_unless_block_true"
groups = ["block_removal"]
query = """
(
(call
arguments: (argument_list
(pair
key: (hash_key_symbol) @operator
value: (call
receiver: (constant) @receiver
block: (block
body: (block_body
(true)
)
)
)
)@proc_pair
)@arguments
)@call
(#eq? @operator "unless")
(#eq? @receiver "Proc")
)
"""
replace = ""
replace_node = "call"

# Before :
# after_commit :do_something, on: :create, unless: proc { true }
# After :
#
#
[[rules]]
name = "remove_proc_unless_block_true"
groups = ["block_removal"]
query = """
(
(call
arguments: (argument_list
(pair
key: (hash_key_symbol) @operator
value: (call
method: (identifier) @receiver
block: (block
body: (block_body
(true)
)
)
)
)@proc_pair
)
)@call
(#eq? @operator "unless")
(#eq? @receiver "proc")
)
"""
replace = ""
replace_node = "call"

# Before :
# after_commit :do_something, on: :create, unless: Proc.new { false }
# After :
# after_commit :do_something, on: :create
#
[[rules]]
name = "remove_Proc_unless_block_false"
groups = ["block_removal"]
query = """
(
(call
arguments: (argument_list
(pair
key: (hash_key_symbol) @operator
value: (call
receiver: (constant) @receiver
block: (block
body: (block_body
(false)
)
)
)
)@proc_pair
)@arguments
)@call
(#eq? @operator "unless")
(#eq? @receiver "Proc")
)
"""
replace = ""
replace_node = "proc_pair"

# Before :
# after_commit :do_something, on: :create, unless: proc { false }
# After :
# after_commit :do_something, on: :create
#
[[rules]]
name = "remove_proc_unless_block_false"
groups = ["block_removal"]
query = """
(
(call
arguments: (argument_list
(pair
key: (hash_key_symbol) @operator
value: (call
method: (identifier) @receiver
block: (block
body: (block_body
(false)
)
)
)
)@proc_pair
)
)@call
(#eq? @operator "unless")
(#eq? @receiver "proc")
)
"""
replace = ""
replace_node = "proc_pair"

# Before :
# before(:all) { }
# After :
Expand Down Expand Up @@ -831,7 +1096,7 @@ name = "delete_variable_declaration"
query = """
(
(assignment
left: (identifier) @variable_name
left: [(identifier) (instance_variable)] @variable_name
right: [(true) (false)] @init
)@variable_declaration
)
Expand All @@ -852,7 +1117,7 @@ not_contains = ["""
right: (_) @a.rhs
)@assignment
(#eq? @a.lhs "@variable_name")
(#not-eq? @a.rhs "@init")
(#not-eq? @a.rhs "@init")
)
"""]

Expand All @@ -862,7 +1127,7 @@ not_contains = ["""
name = "replace_identifier_with_value"
query = """
(
(identifier) @identifier
[(identifier) (instance_variable)] @identifier
(#eq? @identifier "@variable_name")
)
"""
Expand Down
4 changes: 3 additions & 1 deletion src/tests/test_piranha_ruby.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ create_rewrite_tests! {
test_simplify_rspec_block_expressions: "simplify_rspec_block_expressions", 1;
test_simplify_if_lambda_conditional_statements: "simplify_if_lambda_conditional_statements", 1;
test_simplify_unless_lambda_conditional_statements: "simplify_unless_lambda_conditional_statements", 1;
test_simplify_if_proc_conditional_statements: "simplify_if_proc_conditional_statements", 1;
test_simplify_unless_proc_conditional_statements: "simplify_if_proc_conditional_statements", 1;
test_delete_lines_after_return: "delete_lines_after_return", 1;
test_simplify_local_variable_assigned_flag_check: "simplify_local_variable_assigned_flag_check", 1;
simplify_variable_assigned_flag_check: "simplify_variable_assigned_flag_check", 1;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ def test_boolean_not_false
do_something
end

def test_parenthesized_boolean_not_true

end

def test_parenthesized_boolean_not_false
do_something
end


def test_parenthesized_expression_true
do_something
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ def test_boolean_not_false
do_something if !false
end

def test_parenthesized_boolean_not_true
do_something if !(true)
end

def test_parenthesized_boolean_not_false
do_something if !(false)
end

def test_parenthesized_expression_true
do_something if (true)
end
Expand Down
Loading

0 comments on commit 4bf4682

Please sign in to comment.