Skip to content

Commit 36980e6

Browse files
committed
Private methods attributed with @objc are also exposed to the Obj-C runtime. Closes #238
1 parent 88368b3 commit 36980e6

File tree

6 files changed

+40
-0
lines changed

6 files changed

+40
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
##### Bug Fixes
1212

1313
- Fix indexing failure on unhandled declaration kinds, such as 'commentTag'.
14+
- `--retain-objc-accessible` also retains private declarations explicitly attributed with `@objc`.
1415

1516
## 2.3.1 (2020-12-06)
1617

Sources/PeripheryKit/Analyzer/Analyzer.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public final class Analyzer {
4343
EntryPointAttributeRetainer.self,
4444
EntryPointFileRetainer.self,
4545
PubliclyAccessibleRetainer.self,
46+
ObjCAccessibleRetainer.self,
4647
XCTestRetainer.self,
4748
SwiftUIRetainer.self,
4849
StructImplicitConstructorPropertyRetainer.self,
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import Foundation
2+
import Shared
3+
4+
final class ObjCAccessibleRetainer: SourceGraphVisitor {
5+
static func make(graph: SourceGraph) -> Self {
6+
return self.init(graph: graph, configuration: inject())
7+
}
8+
9+
private let graph: SourceGraph
10+
private let configuration: Configuration
11+
12+
required init(graph: SourceGraph, configuration: Configuration) {
13+
self.graph = graph
14+
self.configuration = configuration
15+
}
16+
17+
func visit() {
18+
guard configuration.retainObjcAccessible else { return }
19+
20+
Declaration.Kind.accessibleKinds
21+
.flatMap {
22+
graph.declarations(ofKind: $0)
23+
}
24+
.filter {
25+
$0.attributes.contains("objc") ||
26+
$0.attributes.contains("objc.name") ||
27+
$0.attributes.contains("objcMembers")
28+
}
29+
.forEach {
30+
$0.isObjcAccessible = true
31+
graph.markRetained($0)
32+
}
33+
}
34+
}

Tests/PeripheryTests/RetentionTest.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,6 +1159,7 @@ class RetentionTest: SourceGraphTestCase {
11591159
XCTAssertReferenced((.class, "FixtureClass22"))
11601160
XCTAssertReferenced((.varInstance, "someVar"))
11611161
XCTAssertReferenced((.functionMethodInstance, "someMethod()"))
1162+
XCTAssertReferenced((.functionMethodInstance, "somePrivateMethod()"))
11621163
}
11631164
}
11641165

@@ -1181,6 +1182,7 @@ class RetentionTest: SourceGraphTestCase {
11811182
XCTAssertReferenced((.class, "FixtureClass25"))
11821183
XCTAssertReferenced((.varInstance, "someVar"))
11831184
XCTAssertReferenced((.functionMethodInstance, "someMethod()"))
1185+
XCTAssertNotReferenced((.functionMethodInstance, "somePrivateMethod()"))
11841186
}
11851187
}
11861188
#endif

Tests/RetentionFixtures/testObjcMembersAnnotationRetainsMembers.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ import Foundation
33
@objcMembers class FixtureClass25: NSObject {
44
var someVar: String?
55
func someMethod() {}
6+
private func somePrivateMethod() {} // @objcMembers doesn't apply to private methods.
67
}

Tests/RetentionFixtures/testRetainsObjcAnnotatedMembers.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ import Foundation
33
@objc class FixtureClass22: NSObject {
44
@objc var someVar: String?
55
@objc func someMethod() {}
6+
@objc private func somePrivateMethod() {}
67
}

0 commit comments

Comments
 (0)