Skip to content

Commit

Permalink
Rules for enum cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Satyam1749 committed Feb 27, 2023
1 parent ae1fff3 commit b59af5e
Show file tree
Hide file tree
Showing 6 changed files with 326 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/cleanup_rules/swift/edges.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,8 @@ to = ["if_cleanup"]
scope = "Parent"
from = "statement_cleanup"
to = ["guard_cleanup"]

[[edges]]
scope = "Class"
from = "enum_entry_cleanup"
to = ["enum_entry_cleanup_from_variable"]
131 changes: 131 additions & 0 deletions src/cleanup_rules/swift/rules.toml
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,137 @@ replace_node = "guard_block"
replace = ""
is_seed_rule = false

# Next 3 rules cleanup an enum entry from all variables of the enum
# These 3 rules depend on order of the rules in this file

#
# for @stale_flag = one
# Before
# enum TestEnum {
# var v1: String {
# switch self {
# case .one:
# return "one"
# }
# }
# }
#
# After
# enum TestEnum {
# var v1: String {
# switch self {
# }
# }
# }
[[rules]]
name = "delete_enum_namespace"
query = """(
(enum_class_body
(property_declaration
computed_value: (computed_property
(statements
(switch_statement
expr: (self_expression)
(switch_entry
(switch_pattern
(pattern
(simple_identifier) @switch_case))@switch_pattern)@switch_entry)))))
(#eq? @switch_case "@stale_flag")
)"""
replace_node = "switch_entry"
replace = ""
holes = ["stale_flag"]
groups = ["enum_entry_cleanup_from_variable"]
is_seed_rule = false

#
# for @stale_flag = one
# Before
# enum TestEnum {
# var v1: String {
# switch self {
# case .two, .one:
# return "one"
# }
# }
# }
#
# After
# enum TestEnum {
# var v1: String {
# switch self {
# case .two, .two:
# return "one"
# }
# }
# }
[[rules]]
name = "delete_enum_namespace_previous_available"
query = """(
(enum_class_body
(property_declaration
computed_value: (computed_property
(statements
(switch_statement
expr: (self_expression)
(switch_entry
.(switch_pattern) @previous_pattern
(switch_pattern
(pattern
(simple_identifier) @switch_case))@switch_pattern))))))
(#eq? @switch_case "@stale_flag")
)"""
replace_node = "switch_pattern"
replace = "@previous_pattern"
holes = ["stale_flag"]
groups = ["enum_entry_cleanup_from_variable"]
is_seed_rule = false

#
# for @stale_flag = one
# Before
# enum TestEnum {
# var v1: String {
# switch self {
# case .one, .two:
# return "one"
# }
# }
# }
#
# After
# enum TestEnum {
# var v1: String {
# switch self {
# case .two:
# return "one"
# }
# }
# }
[[rules]]
name = "delete_enum_namespace_next_available"
query = """(
(enum_class_body
(property_declaration
computed_value: (computed_property
(statements
(switch_statement
expr: (self_expression)
(switch_entry
(switch_pattern
(pattern
(simple_identifier) @switch_case))@switch_pattern
.(switch_pattern) @next_pattern))))))
(#eq? @switch_case "@stale_flag")
)"""
replace_node = "switch_pattern"
replace = ""
holes = ["stale_flag"]
groups = ["enum_entry_cleanup_from_variable"]
is_seed_rule = false

# Dummy rule that acts as a junction for all boolean based cleanups
# Let's say you want to define rules from A -> B, A -> C, D -> B, D -> C, ...
Expand Down
4 changes: 2 additions & 2 deletions src/tests/test_piranha_swift.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ create_rewrite_tests! {
cleanup_comments = true,
global_tag_prefix ="universal_tag.".to_string(),
cleanup_comments_buffer = 3, delete_file_if_empty= false;
test_cleanup_rules_file: "cleanup_rules", 1,
test_cleanup_rules_file: "cleanup_rules", 2,
substitutions = substitutions! {
"stale_flag" => "stale_flag_one",
"treated" => "true",
"treated_complement" => "false"
},
cleanup_comments = true, delete_file_if_empty= false;
cleanup_comments = true, delete_file_if_empty = false;
}
12 changes: 12 additions & 0 deletions test-resources/swift/cleanup_rules/configurations/rules.toml
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,15 @@ query = """(
replace_node = "variable"
replace = "false"
groups = ["replace_expression_with_boolean_literal"]

[[rules]]
name = "delete_enum_entry"
query = """(
(enum_entry
name: (simple_identifier) @enum_case)@enum_entry
(#eq? @enum_case "@stale_flag")
)"""
replace_node = "enum_entry"
replace = ""
holes = ["stale_flag"]
groups = ["enum_entry_cleanup"]
83 changes: 83 additions & 0 deletions test-resources/swift/cleanup_rules/expected/TestEnum.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
enum TestFlag {
case stale_flag_two
case stale_flag_three

var var1: String {
switch self {
case .stale_flag_two, .stale_flag_three:
return "test"
}
}

var var2: String {
switch self {
case .stale_flag_three, .stale_flag_two:
return "test"
}
}
}

enum TestFlag2 {
case stale_flag_two
case stale_flag_three

var var1: String {
switch self {
case .stale_flag_two, .stale_flag_three, .stale_flag_two:
return "test"
}
}

var var2: String {
switch self {
case .stale_flag_two, .stale_flag_three:
return "test"
}
}
}

enum TestFlag3 {
case stale_flag_two
case stale_flag_three

var var1: String {
switch self {
case .stale_flag_three, .stale_flag_two:
return "test"
}
}

var var2: String {
switch self {
case .stale_flag_two, .stale_flag_three, .stale_flag_two:
return "test"
}
}
}


enum TestFlag4 {
case stale_flag_two
case stale_flag_three

var var1: String {
switch self {
case .stale_flag_three, .stale_flag_two:
return "test"
}
}

var var2: String {
switch self {
case .stale_flag_three, .stale_flag_two:
return "test"
}
}

var var3: String {
switch self {
case .stale_flag_three, .stale_flag_two:
return "test"
}
}
}
93 changes: 93 additions & 0 deletions test-resources/swift/cleanup_rules/input/TestEnum.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
enum TestFlag {
case stale_flag_one
case stale_flag_two
case stale_flag_three

var var1: String {
switch self {
case .stale_flag_two, .stale_flag_one, .stale_flag_three:
return "test"
}
}

var var2: String {
switch self {
case .stale_flag_one, .stale_flag_three, .stale_flag_two:
return "test"
}
}
}

enum TestFlag2 {
case stale_flag_one
case stale_flag_two
case stale_flag_three

var var1: String {
switch self {
case .stale_flag_two, .stale_flag_three, .stale_flag_one:
return "test"
}
}

var var2: String {
switch self {
case .stale_flag_two, .stale_flag_one, .stale_flag_three:
return "test"
}
}
}

enum TestFlag3 {
case stale_flag_one
case stale_flag_two
case stale_flag_three

var var1: String {
switch self {
case .stale_flag_one, .stale_flag_three, .stale_flag_two:
return "test"
}
}

var var2: String {
switch self {
case .stale_flag_two, .stale_flag_three, .stale_flag_one:
return "test"
}
}
}


enum TestFlag4 {
case stale_flag_one
case stale_flag_two
case stale_flag_three

var var1: String {
switch self {
case .stale_flag_one:
return "test2"
case .stale_flag_three, .stale_flag_two:
return "test"
}
}

var var2: String {
switch self {
case .stale_flag_one:
return "test2"
case .stale_flag_three, .stale_flag_two:
return "test"
}
}

var var3: String {
switch self {
case .stale_flag_one:
return "test2"
case .stale_flag_three, .stale_flag_two:
return "test"
}
}
}

0 comments on commit b59af5e

Please sign in to comment.