Skip to content

Commit 52a4e2f

Browse files
committed
BridgeJS: Diagnose async and throwing @js struct initializers
1 parent a0961dd commit 52a4e2f

2 files changed

Lines changed: 23 additions & 0 deletions

File tree

‎Plugins/BridgeJS/Sources/BridgeJSCore/SwiftToSkeleton.swift‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1986,6 +1986,14 @@ private final class ExportSwiftAPICollector: SyntaxAnyVisitor {
19861986
guard let effects = collectEffects(signature: node.signature) else {
19871987
return .skipChildren
19881988
}
1989+
if effects.isAsync || effects.isThrows {
1990+
diagnose(
1991+
node: node,
1992+
message: "@JS struct initializer cannot be async or throw",
1993+
hint: "BridgeJS calls this initializer to rebuild struct values passed from JavaScript"
1994+
)
1995+
return .skipChildren
1996+
}
19891997

19901998
let structAbiName = exportedStructByName[structKey]?.abiName ?? "unknown"
19911999
let constructor = ExportedConstructor(

‎Plugins/BridgeJS/Tests/BridgeJSToolTests/DiagnosticsTests.swift‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,21 @@ import Testing
535535
#expect(skeleton.exported != nil)
536536
}
537537

538+
@Test
539+
func throwingStructInitProducesDiagnostic() throws {
540+
let source = """
541+
@JS struct Point {
542+
var x: Double
543+
544+
@JS init(x: Double) throws(JSException) {
545+
self.x = x
546+
}
547+
}
548+
"""
549+
let diagnostics = try #require(moduleDiagnostics(source: source))
550+
#expect(diagnostics.description.contains("@JS struct initializer cannot be async or throw"))
551+
}
552+
538553
@Test
539554
func structWithoutExplicitInitSucceeds() throws {
540555
let source = """

0 commit comments

Comments
 (0)