Skip to content

Commit 936d3c8

Browse files
authored
fix: only add methods to the proper class (#21)
1 parent 9f3092c commit 936d3c8

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

Sources/CapacitorPluginSyntaxTools/AddPluginToClass.swift

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,19 @@ class AddPluginToClass: SyntaxRewriter {
1111
}
1212

1313
override func visit(_ node: ClassDeclSyntax) -> DeclSyntax {
14-
var newNode = capacitorPluginSyntax.addBridgedPluginConformance(node)
15-
newNode.memberBlock.members.insert(contentsOf: capacitorPluginSyntax.createMemberBlock(),
16-
at: node.memberBlock.members.startIndex)
17-
return DeclSyntax(newNode)
14+
guard let inheritedTypes = node.inheritanceClause?.inheritedTypes else {
15+
return DeclSyntax(node)
16+
}
17+
18+
if let inheritedType = inheritedTypes.first(where: { $0.isNamed("CAPPlugin") }) {
19+
var newNode = capacitorPluginSyntax.addBridgedPluginConformance(node)
20+
newNode.memberBlock.members.insert(contentsOf: capacitorPluginSyntax.createMemberBlock(),
21+
at: node.memberBlock.members.startIndex)
22+
return DeclSyntax(newNode)
23+
} else {
24+
return DeclSyntax(node)
25+
}
1826
}
27+
1928
}
29+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import Foundation
2+
import SwiftSyntax
3+
import SwiftParser
4+
5+
extension InheritedTypeSyntax {
6+
func isNamed(_ searchName: String) -> Bool {
7+
if let foundName = self.type.as(IdentifierTypeSyntax.self)?.name.text {
8+
return foundName == searchName
9+
} else {
10+
return false
11+
}
12+
}
13+
}

Sources/CapacitorPluginTools/CapacitorPluginPackage.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ public enum CapacitorPluginError: Error {
2222
}
2323
}
2424

25+
struct StandardError: TextOutputStream, Sendable {
26+
private static let handle = FileHandle.standardError
27+
28+
public func write(_ string: String) {
29+
Self.handle.write(Data(string.utf8))
30+
}
31+
}
32+
2533
public class CapacitorPluginPackage {
2634
public let pluginDirectoryName: String
2735
public let basePathURL: URL

0 commit comments

Comments
 (0)