From 5a99731c8f4a93a632d261d7aa90e6a98a43c069 Mon Sep 17 00:00:00 2001 From: Adarsh Narayan Pandey Date: Tue, 5 Sep 2023 17:56:59 +0530 Subject: [PATCH] Variable Inline Fixes: Reorder and Add Tests --- src/cleanup_rules/swift/edges.toml | 12 ++- src/cleanup_rules/swift/rules.toml | 33 ++++++ .../input/AdhocVariableInlining.swift | 100 ++++++++++++++++++ 3 files changed, 142 insertions(+), 3 deletions(-) diff --git a/src/cleanup_rules/swift/edges.toml b/src/cleanup_rules/swift/edges.toml index fdf35dde9..ea742a6c0 100644 --- a/src/cleanup_rules/swift/edges.toml +++ b/src/cleanup_rules/swift/edges.toml @@ -30,14 +30,19 @@ 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", ] @@ -45,8 +50,9 @@ to = [ 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", ] diff --git a/src/cleanup_rules/swift/rules.toml b/src/cleanup_rules/swift/rules.toml index 59d14292e..5c6edd5c5 100644 --- a/src/cleanup_rules/swift/rules.toml +++ b/src/cleanup_rules/swift/rules.toml @@ -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 @@ -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" diff --git a/test-resources/swift/variable_inline/adhoc_variable_inline/input/AdhocVariableInlining.swift b/test-resources/swift/variable_inline/adhoc_variable_inline/input/AdhocVariableInlining.swift index 82ef7186c..824725288 100644 --- a/test-resources/swift/variable_inline/adhoc_variable_inline/input/AdhocVariableInlining.swift +++ b/test-resources/swift/variable_inline/adhoc_variable_inline/input/AdhocVariableInlining.swift @@ -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 + } + } +}