|
| 1 | +import Foundation |
| 2 | +import SwiftParser |
| 3 | +import SwiftSyntax |
| 4 | +import Testing |
| 5 | + |
| 6 | +@testable import BridgeJSCore |
| 7 | +@testable import BridgeJSSkeleton |
| 8 | + |
| 9 | +@Suite struct GenericExportDiagnosticsTests { |
| 10 | + |
| 11 | + @Test |
| 12 | + func genericParameterRequiresBridgeableConstraint() { |
| 13 | + expectDiagnostic( |
| 14 | + source: """ |
| 15 | + @JS public func identity<T>(_ value: T) -> T { value } |
| 16 | + """, |
| 17 | + contains: "Generic parameter 'T' must be constrained to '_BridgedSwiftGenericBridgeable'" |
| 18 | + ) |
| 19 | + } |
| 20 | + |
| 21 | + @Test |
| 22 | + func genericWhereClauseUnsupported() { |
| 23 | + expectDiagnostic( |
| 24 | + source: """ |
| 25 | + @JS public func identity<T: _BridgedSwiftGenericBridgeable>(_ value: T) -> T where T: Sendable { value } |
| 26 | + """, |
| 27 | + contains: "'where' clauses are not supported" |
| 28 | + ) |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + func asyncGenericExportUnsupported() { |
| 33 | + expectDiagnostic( |
| 34 | + source: """ |
| 35 | + @JS public func f<T: _BridgedSwiftGenericBridgeable>(_ v: T) async -> T { v } |
| 36 | + """, |
| 37 | + contains: "Generic @JS functions cannot be 'async' yet." |
| 38 | + ) |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + func throwsGenericExportUnsupported() { |
| 43 | + expectDiagnostic( |
| 44 | + source: """ |
| 45 | + @JS public func f<T: _BridgedSwiftGenericBridgeable>(_ v: T) throws(JSException) -> T { v } |
| 46 | + """, |
| 47 | + contains: "Generic @JS functions cannot be 'throws' yet." |
| 48 | + ) |
| 49 | + } |
| 50 | + |
| 51 | + @Test(arguments: [ |
| 52 | + ("[[T]]", "@JS public func f<T: _BridgedSwiftGenericBridgeable>(_ v: [[T]]) {}"), |
| 53 | + ("[T?]", "@JS public func f<T: _BridgedSwiftGenericBridgeable>(_ v: [T?]) {}"), |
| 54 | + ("T??", "@JS public func f<T: _BridgedSwiftGenericBridgeable>(_ v: T??) {}"), |
| 55 | + ("[Int: T]", "@JS public func f<T: _BridgedSwiftGenericBridgeable>(_ v: [Int: T]) {}"), |
| 56 | + ]) |
| 57 | + func unsupportedGenericWrapperFormsInParameter(label: String, source: String) { |
| 58 | + expectDiagnostic( |
| 59 | + source: source, |
| 60 | + contains: "may only be used as a bare type" |
| 61 | + ) |
| 62 | + } |
| 63 | + |
| 64 | + @Test(arguments: [ |
| 65 | + ("[[T]]", "@JS public func f<T: _BridgedSwiftGenericBridgeable>(_ v: T) -> [[T]] { [[v]] }"), |
| 66 | + ("[T?]", "@JS public func f<T: _BridgedSwiftGenericBridgeable>(_ v: T) -> [T?] { [v] }"), |
| 67 | + ("T??", "@JS public func f<T: _BridgedSwiftGenericBridgeable>(_ v: T) -> T?? { v }"), |
| 68 | + ("[Int: T]", "@JS public func f<T: _BridgedSwiftGenericBridgeable>(_ v: T) -> [Int: T] { [0: v] }"), |
| 69 | + ]) |
| 70 | + func unsupportedGenericWrapperFormsInReturn(label: String, source: String) { |
| 71 | + expectDiagnostic( |
| 72 | + source: source, |
| 73 | + contains: "may only be used as a bare type" |
| 74 | + ) |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + func fullyUnusedGenericParameterRejected() { |
| 79 | + expectDiagnostic( |
| 80 | + source: """ |
| 81 | + @JS public func combine<T: _BridgedSwiftGenericBridgeable, U: _BridgedSwiftGenericBridgeable>(_ a: T) -> T { a } |
| 82 | + """, |
| 83 | + contains: "must be used in at least one parameter" |
| 84 | + ) |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + func genericParameterMustBeUsedAsParameter() { |
| 89 | + expectDiagnostic( |
| 90 | + source: """ |
| 91 | + @JS public func f<T: _BridgedSwiftGenericBridgeable>() -> T { fatalError() } |
| 92 | + """, |
| 93 | + contains: "must be used in at least one parameter" |
| 94 | + ) |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + func genericConcreteReturnUnsupported() { |
| 99 | + expectDiagnostic( |
| 100 | + source: """ |
| 101 | + @JS public func f<T: _BridgedSwiftGenericBridgeable>(_ v: T) -> String { "" } |
| 102 | + """, |
| 103 | + contains: "must return the generic type" |
| 104 | + ) |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + func genericInstanceMethodAsyncIsRejected() { |
| 109 | + expectDiagnostic( |
| 110 | + source: """ |
| 111 | + @JS class Box { |
| 112 | + @JS init() {} |
| 113 | + @JS func wrap<T: _BridgedSwiftGenericBridgeable>(_ v: T) async -> T { v } |
| 114 | + } |
| 115 | + """, |
| 116 | + contains: "Generic @JS functions cannot be 'async' yet." |
| 117 | + ) |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + func genericInstanceMethodThrowsIsRejected() { |
| 122 | + expectDiagnostic( |
| 123 | + source: """ |
| 124 | + @JS class Box { |
| 125 | + @JS init() {} |
| 126 | + @JS func wrap<T: _BridgedSwiftGenericBridgeable>(_ v: T) throws(JSException) -> T { v } |
| 127 | + } |
| 128 | + """, |
| 129 | + contains: "Generic @JS functions cannot be 'throws' yet." |
| 130 | + ) |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + func genericMethodWhereClauseIsRejected() { |
| 135 | + expectDiagnostic( |
| 136 | + source: """ |
| 137 | + @JS class Box { |
| 138 | + @JS init() {} |
| 139 | + @JS func wrap<T: _BridgedSwiftGenericBridgeable>(_ v: T) -> T where T: Sendable { v } |
| 140 | + } |
| 141 | + """, |
| 142 | + contains: "'where' clauses are not supported on generic @JS functions." |
| 143 | + ) |
| 144 | + } |
| 145 | + |
| 146 | + @Test |
| 147 | + func genericMethodUnconstrainedParamIsRejected() { |
| 148 | + expectDiagnostic( |
| 149 | + source: """ |
| 150 | + @JS class Box { |
| 151 | + @JS init() {} |
| 152 | + @JS func f<T>(_ v: T) -> T { v } |
| 153 | + } |
| 154 | + """, |
| 155 | + contains: |
| 156 | + "Generic parameter 'T' must be constrained to '_BridgedSwiftGenericBridgeable' to be used with @JS." |
| 157 | + ) |
| 158 | + } |
| 159 | + |
| 160 | + @Test |
| 161 | + func genericMethodConcreteReturnIsRejected() { |
| 162 | + expectDiagnostic( |
| 163 | + source: """ |
| 164 | + @JS class Box { |
| 165 | + @JS init() {} |
| 166 | + @JS func count<T: _BridgedSwiftGenericBridgeable>(_ v: T) -> Int { 0 } |
| 167 | + } |
| 168 | + """, |
| 169 | + contains: "must return the generic type" |
| 170 | + ) |
| 171 | + } |
| 172 | + |
| 173 | + @Test |
| 174 | + func genericEnumInstanceMethodIsRejected() { |
| 175 | + expectDiagnostic( |
| 176 | + source: """ |
| 177 | + @JS enum E { |
| 178 | + case a |
| 179 | + @JS func f<T: _BridgedSwiftGenericBridgeable>(_ v: T) -> T { v } |
| 180 | + } |
| 181 | + """, |
| 182 | + contains: "Only static functions are supported in enums" |
| 183 | + ) |
| 184 | + } |
| 185 | + |
| 186 | + @Test |
| 187 | + func genericTypedPropertyIsRejectedAsUnsupportedType() { |
| 188 | + expectDiagnostic( |
| 189 | + source: """ |
| 190 | + @JS final class Box { |
| 191 | + @JS init() {} |
| 192 | + @JS var value: T |
| 193 | + } |
| 194 | + """, |
| 195 | + contains: "Unsupported type 'T'." |
| 196 | + ) |
| 197 | + } |
| 198 | +} |
0 commit comments