Skip to content
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

Rules for cleaning up case from switch statements of variables in an enum #397

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -431,6 +431,137 @@ query = """(
groups = ["guard_cleanup"]
replace_node = "guard_block"
replace = "@else_block"

# 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_variable_switch"
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"
Copy link
Collaborator

Choose a reason for hiding this comment

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

We no longer need this right?

# }
# }
# }
[[rules]]
name = "delete_enum_variable_switch_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 = ""
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_variable_switch_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
Expand Down
2 changes: 1 addition & 1 deletion src/tests/test_piranha_swift.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ 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",
Expand Down
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:
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:
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"
}
}
}