Skip to content

Commit

Permalink
Swift: Fix local variable declaration inside constructor (#620)
Browse files Browse the repository at this point in the history
  • Loading branch information
Adarsh-NP authored Sep 28, 2023
1 parent 907aa9a commit 6ad874f
Show file tree
Hide file tree
Showing 20 changed files with 325 additions and 88 deletions.
6 changes: 6 additions & 0 deletions src/cleanup_rules/swift/edges.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,14 @@ to = [
"delete_field_initialisation",
"delete_field_initialisation_init",
"delete_variable_declaration",
"delete_variable_declaration_init",
]

[[edges]]
scope = "Constructor"
from = "delete_variable_declaration_init"
to = ["replace_identifier_with_value", "delete_parent_assignment"]

[[edges]]
scope = "Function"
from = "delete_variable_declaration"
Expand Down
50 changes: 47 additions & 3 deletions src/cleanup_rules/swift/rules.toml
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,50 @@ not_contains = ["""(
(#not-eq? @val "@hvalue")
)"""]

# delete variables declared in a function scope
[[rules]]
name = "delete_variable_declaration_init"
query = """(
(property_declaration
name: (pattern
bound_identifier: (simple_identifier) @hvariable
)
value: (boolean_literal) @hvalue
) @property_declaration
)"""
replace_node = "property_declaration"
replace = ""
is_seed_rule = false

[[rules.filters]]
enclosing_node = "(init_declaration) @cfd"

# skip the rule if the variable is assigned with some other value in the class scope
[[rules.filters]]
enclosing_node = "(class_declaration) @cd"
not_contains = ["""(
(parameter
name: (simple_identifier) @c1var
)
(#eq? @c1var "@hvariable")
)""", """(
(assignment
target: (directly_assignable_expression
[ (navigation_expression
target: (self_expression)
suffix: (navigation_suffix
suffix: (simple_identifier) @var
)
)
(simple_identifier) @var
]
)
result: (boolean_literal) @val
)
(#eq? @var "@hvariable")
(#not-eq? @val "@hvalue")
)"""]

# rule to delete assignments like a = true subject to the mentioned filters
[[rules]]
name = "delete_parent_assignment"
Expand Down Expand Up @@ -739,14 +783,14 @@ is_seed_rule = false
[[rules.filters]]
enclosing_node = "(class_declaration) @cd"
not_contains = [
# skip the rule if the variable is one of the parameters of some function
# skip the rule if the variable is one of the parameters of some function
"""(
(parameter
name: (simple_identifier) @c1var
)
(#eq? @c1var "@identifier")
)""",
# skip the rule if there is a declaration of the same variable
# skip the rule if there is a declaration of the same variable
"""(
(property_declaration
name: (pattern
Expand All @@ -757,7 +801,7 @@ not_contains = [
(#eq? @variable "@identifier")
)""",

# skip the rule if it is accessed in the self.x manner
# skip the rule if it is accessed in the self.x manner
"""(
(navigation_expression
target: (self_expression)
Expand Down
6 changes: 3 additions & 3 deletions src/cleanup_rules/swift/scope_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ enclosing_node = """(
(init_declaration
(parameter)* @params
body: (function_body)
)
) @int
)"""
scope = """(
(init_declaration)
(init_declaration
(parameter)* @parameters
body: (function_body)
)
) @sint
(#eq? @parameters "@params")
)"""

Expand Down
7 changes: 7 additions & 0 deletions src/tests/test_piranha_swift.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,13 @@ fn test_adhoc_variable_inline_file() {
execute_piranha_for_swift("variable_inline/adhoc_variable_inline", vec![], 1);
}

#[test]
#[ignore] // Long running test
fn test_constructor_variable_inline_file() {
super::initialize();
execute_piranha_for_swift("variable_inline/constructor_variable_inline", vec![], 5);
}

#[test]
#[ignore] // Long running test
fn test_delete_everything_after_return() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,8 @@ class C2{

init(){
if something{
var b = true
if b {
a = true
doSomething()
}
a = true
doSomething()
} else {
a = false
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright (c) 2023 Uber Technologies, Inc.
#
# <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
# except in compliance with the License. You may obtain a copy of the License at
# <p>http://www.apache.org/licenses/LICENSE-2.0
#
# <p>Unless required by applicable law or agreed to in writing, software distributed under the
# License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
# express or implied. See the License for the specific language governing permissions and
# limitations under the License.

[[edges]]
scope = "Parent"
from = "replace_expression_with_boolean_literal"
to = ["variable_inline_cleanup"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Copyright (c) 2023 Uber Technologies, Inc.
#
# <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
# except in compliance with the License. You may obtain a copy of the License at
# <p>http://www.apache.org/licenses/LICENSE-2.0
#
# <p>Unless required by applicable law or agreed to in writing, software distributed under the
# License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
# express or implied. See the License for the specific language governing permissions and
# limitations under the License.

# Before
# placeholder_false
# After
# false
#
[[rules]]
name = "test_rule_replace_false_placeholder"
query = """(
(simple_identifier) @variable
(#eq? @variable "placeholder_false")
)"""
replace_node = "variable"
replace = "false"
groups = ["replace_expression_with_boolean_literal"]

# Before
# placeholder_false
# After
# false
#
[[rules]]
name = "test_rule_replace_true_placeholder"
query = """(
(simple_identifier) @variable
(#eq? @variable "placeholder_true")
)"""
replace_node = "variable"
replace = "true"
groups = ["replace_expression_with_boolean_literal"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) 2023 Uber Technologies, Inc.

// <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
// except in compliance with the License. You may obtain a copy of the License at
// <p>http://www.apache.org/licenses/LICENSE-2.0

// <p>Unless required by applicable law or agreed to in writing, software distributed under the
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing permissions and
// limitations under the License.

// rule to test:
// 1. edge from boolean_literal_cleanup to variable_inline_cleanup
// 2. variable cleanup local to the init

class C1 {
init() {
super.init(someParameter: someOtherVar)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) 2023 Uber Technologies, Inc.

// <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
// except in compliance with the License. You may obtain a copy of the License at
// <p>http://www.apache.org/licenses/LICENSE-2.0

// <p>Unless required by applicable law or agreed to in writing, software distributed under the
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing permissions and
// limitations under the License.

// rule to test:
// 1. edge from boolean_literal_cleanup to variable_inline_cleanup
// 2. variable cleanup local to the init

class C1 {
init() {
super.init(someParameter: someVar)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) 2023 Uber Technologies, Inc.

// <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
// except in compliance with the License. You may obtain a copy of the License at
// <p>http://www.apache.org/licenses/LICENSE-2.0

// <p>Unless required by applicable law or agreed to in writing, software distributed under the
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing permissions and
// limitations under the License.

// rule to test:
// 1. edge from boolean_literal_cleanup to variable_inline_cleanup
// 2. variable cleanup local to the init

class C1 {
init() {
super.init(someParameter: someOtherVar)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) 2023 Uber Technologies, Inc.

// <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
// except in compliance with the License. You may obtain a copy of the License at
// <p>http://www.apache.org/licenses/LICENSE-2.0

// <p>Unless required by applicable law or agreed to in writing, software distributed under the
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing permissions and
// limitations under the License.

// rule to test:
// 1. edge from boolean_literal_cleanup to variable_inline_cleanup
// 2. variable cleanup local to the init

class C1 {
init() {
super.init(someParameter: someFunctionCall(a: true))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) 2023 Uber Technologies, Inc.

// <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
// except in compliance with the License. You may obtain a copy of the License at
// <p>http://www.apache.org/licenses/LICENSE-2.0

// <p>Unless required by applicable law or agreed to in writing, software distributed under the
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing permissions and
// limitations under the License.

// rule to test:
// 1. edge from boolean_literal_cleanup to variable_inline_cleanup
// 2. variable cleanup local to the init

class C1 {
init() {
super.init(someParameter: someFunctionCall(a: true))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) 2023 Uber Technologies, Inc.

// <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
// except in compliance with the License. You may obtain a copy of the License at
// <p>http://www.apache.org/licenses/LICENSE-2.0

// <p>Unless required by applicable law or agreed to in writing, software distributed under the
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing permissions and
// limitations under the License.

// rule to test:
// 1. edge from boolean_literal_cleanup to variable_inline_cleanup
// 2. variable cleanup local to the init

class C1 {
init() {
let a = !placeholder_true
super.init(someParameter: a ? someVar : someOtherVar)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) 2023 Uber Technologies, Inc.

// <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
// except in compliance with the License. You may obtain a copy of the License at
// <p>http://www.apache.org/licenses/LICENSE-2.0

// <p>Unless required by applicable law or agreed to in writing, software distributed under the
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing permissions and
// limitations under the License.

// rule to test:
// 1. edge from boolean_literal_cleanup to variable_inline_cleanup
// 2. variable cleanup local to the init

class C1 {
init() {
let a = placeholder_false
super.init(someParameter: !a ? someVar : someOtherVar)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) 2023 Uber Technologies, Inc.

// <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
// except in compliance with the License. You may obtain a copy of the License at
// <p>http://www.apache.org/licenses/LICENSE-2.0

// <p>Unless required by applicable law or agreed to in writing, software distributed under the
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing permissions and
// limitations under the License.

// rule to test:
// 1. edge from boolean_literal_cleanup to variable_inline_cleanup
// 2. variable cleanup local to the init

class C1 {
init() {
let a = placeholder_true
super.init(someParameter: !a ? someVar : someOtherVar)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) 2023 Uber Technologies, Inc.

// <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
// except in compliance with the License. You may obtain a copy of the License at
// <p>http://www.apache.org/licenses/LICENSE-2.0

// <p>Unless required by applicable law or agreed to in writing, software distributed under the
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing permissions and
// limitations under the License.

// rule to test:
// 1. edge from boolean_literal_cleanup to variable_inline_cleanup
// 2. variable cleanup local to the init

class C1 {
init() {
let a = !placeholder_false
super.init(someParameter: someFunctionCall(a: a))
}
}
Loading

0 comments on commit 6ad874f

Please sign in to comment.