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

Variable Inline Fixes: Reorder and Add Tests #597

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/cleanup_rules/swift/edges.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,29 @@ to = [
[[edges]]
scope = "Function"
from = "delete_variable_declaration"
to = ["replace_identifier_with_value", "delete_parent_assignment"]
to = [
"replace_argument_with_value",
"replace_identifier_with_value",
"delete_parent_assignment",
]

[[edges]]
scope = "Class"
from = "delete_field_initialisation_init"
to = [
"replace_self_identifier_with_value",
"replace_argument_with_value",
"replace_identifier_with_value",
"replace_self_identifier_with_value",
"delete_field_declaration",
]

[[edges]]
scope = "Class"
from = "delete_field_initialisation"
to = [
"replace_self_identifier_with_value",
"replace_argument_with_value",
"replace_identifier_with_value",
"replace_self_identifier_with_value",
"delete_parent_assignment",
]

Expand Down
33 changes: 33 additions & 0 deletions src/cleanup_rules/swift/rules.toml
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,12 @@ not_contains = [
)
(#eq? @c1var "@identifier")
)""",
"""(
(value_argument
value: (simple_identifier) @svar
)
(#eq? @svar "@identifier")
)""",
# skip the rule if there is a declaration of the same variable
"""(
(property_declaration
Expand All @@ -769,6 +775,33 @@ not_contains = [
)""",
]

[[rules]]
name = "replace_argument_with_value"
query = """(
(call_expression
(call_suffix
(value_arguments
(value_argument
value: [
(simple_identifier) @revar
(navigation_expression
target: (self_expression)
suffix: (navigation_suffix
suffix: (simple_identifier) @revar
)
)
] @valere
)
)
)
)
(#eq? @revar "@hvariable")
)"""
replace_node = "valere"
replace = "@hvalue"
holes = ["hvariable", "hvalue"]
is_seed_rule = false

# rule to delete declaration of variable in class scope
[[rules]]
name = "delete_field_declaration"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,103 @@ class C2{
}
}
}

class C3 {
private lazy var someVar: SomeType = {
let a = placeholder_false
let b: VectorDrawable = a ? .caseA : .caseB
let c = vectorImageComboViewBuilder.buildComboView(for: b, anotherFor: someOtherVar)
c.someAttribute = true
return c
}()

private var someComputedVar: SomeVarType {
let localVar = placeholder_false
return shared {
SomeFunctionCall(a: localVar ? "a" : "b", c: c)
}
}

private var x = !placeholder_false

private func f3() -> String{
abc.subscribe(onNext: {(someVar: SomeVarType) in
switch someVar {
case .a:
return "someString"
case .b:
return "someOtherString"
case .c:
if self.x{
return "to_be_retained"
}
return "to_be_deleted"
case .d:
if x{
return "to_be_retained"
}
return "to_be_deleted"
default: "another_test_case"
}})
}

private func f4() -> String{
abc.subscribe(onNext: {(someVar: SomeVarType) in
if someCondition {
doSomeCalls()
} else {
let someInternalVar = "some_internal_var"
if someOtherCondition {
if self.x {
someFunctionCall()
} else {
someOtherFunctionCall()
}
}
}
})
}
}

class C4 {
private let localVar: Bool
var varA: A
var varB: B

private lazy var someComputedProperty: SomePropertyType {
let a = SomeFunctionCall(
firstVar: .a,
localVar: localVar,
secondVar: .b,
thirdVar: thirdVar
)
}

init(varA: A, varB: B){
self.localVar = placeholder_false
self.varA = varA
self.varB = varB

if localVar {
someFunctionCall(varA, varB, localVar)
} else {
someFunctionCall(varB, varA, localVar)
}
}

func ifInSwitch() -> String?{
switch varA {
case .a:
return "a"
case .b:
let someNestedVar = "some_nested_var"
if localVar {
doSomething()
} else {
doSOmethingElse()
}
default:
return nil
}
}
}