Skip to content

Commit 5161a41

Browse files
BridgeJS: Remove support for static members in extensions
1 parent cf6c745 commit 5161a41

File tree

6 files changed

+65
-138
lines changed

6 files changed

+65
-138
lines changed

Plugins/BridgeJS/Sources/BridgeJSCore/SwiftToSkeleton.swift

Lines changed: 1 addition & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1856,8 +1856,6 @@ private final class ImportSwiftMacrosAPICollector: SyntaxAnyVisitor {
18561856
private let inputFilePath: String
18571857
private var jsClassNames: Set<String>
18581858
private let parent: SwiftToSkeleton
1859-
private var staticMethodsByType: [String: [ImportedFunctionSkeleton]] = [:]
1860-
18611859
// MARK: - State Management
18621860

18631861
enum State {
@@ -2118,21 +2116,19 @@ private final class ImportSwiftMacrosAPICollector: SyntaxAnyVisitor {
21182116

21192117
private func exitJSClass() {
21202118
if case .jsClassBody(let typeName) = state, let type = currentType, type.name == typeName {
2121-
let externalStaticMethods = staticMethodsByType[type.name] ?? []
21222119
importedTypes.append(
21232120
ImportedTypeSkeleton(
21242121
name: type.name,
21252122
jsName: type.jsName,
21262123
from: type.from,
21272124
constructor: type.constructor,
21282125
methods: type.methods,
2129-
staticMethods: type.staticMethods + externalStaticMethods,
2126+
staticMethods: type.staticMethods,
21302127
getters: type.getters,
21312128
setters: type.setters,
21322129
documentation: nil
21332130
)
21342131
)
2135-
staticMethodsByType[type.name] = nil
21362132
currentType = nil
21372133
}
21382134
stateStack.removeLast()
@@ -2172,12 +2168,6 @@ private final class ImportSwiftMacrosAPICollector: SyntaxAnyVisitor {
21722168

21732169
// MARK: - Visitor Methods
21742170

2175-
override func visit(_ node: ExtensionDeclSyntax) -> SyntaxVisitorContinueKind {
2176-
let typeName = node.extendedType.trimmedDescription
2177-
collectStaticMembers(in: node.memberBlock.members, typeName: typeName)
2178-
return .skipChildren
2179-
}
2180-
21812171
override func visit(_ node: FunctionDeclSyntax) -> SyntaxVisitorContinueKind {
21822172
switch state {
21832173
case .topLevel:
@@ -2318,45 +2308,6 @@ private final class ImportSwiftMacrosAPICollector: SyntaxAnyVisitor {
23182308
}
23192309
}
23202310

2321-
// MARK: - Member Collection
2322-
2323-
private func collectStaticMembers(in members: MemberBlockItemListSyntax, typeName: String) {
2324-
for member in members {
2325-
if let function = member.decl.as(FunctionDeclSyntax.self) {
2326-
if let jsFunction = AttributeChecker.firstJSFunctionAttribute(function.attributes),
2327-
let parsed = parseFunction(jsFunction, function) {
2328-
if jsClassNames.contains(typeName) {
2329-
if let index = importedTypes.firstIndex(where: { $0.name == typeName }) {
2330-
importedTypes[index].staticMethods.append(parsed)
2331-
} else {
2332-
importedTypes.append(ImportedTypeSkeleton(name: typeName, staticMethods: [parsed]))
2333-
}
2334-
} else {
2335-
importedFunctions.append(parsed)
2336-
}
2337-
} else if AttributeChecker.hasJSSetterAttribute(function.attributes) {
2338-
errors.append(
2339-
DiagnosticError(
2340-
node: function,
2341-
message:
2342-
"@JSSetter is not supported for static members. Use it only for instance members in @JSClass types."
2343-
)
2344-
)
2345-
}
2346-
} else if let variable = member.decl.as(VariableDeclSyntax.self),
2347-
AttributeChecker.hasJSGetterAttribute(variable.attributes)
2348-
{
2349-
errors.append(
2350-
DiagnosticError(
2351-
node: variable,
2352-
message:
2353-
"@JSGetter is not supported for static members. Use it only for instance members in @JSClass types."
2354-
)
2355-
)
2356-
}
2357-
}
2358-
}
2359-
23602311
// MARK: - Parsing Methods
23612312

23622313
private func parseConstructor(

Plugins/BridgeJS/Tests/BridgeJSToolTests/Inputs/MacroSwift/JSClassStaticFunctions.swift

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
1-
extension StaticBox {
2-
@JSFunction static func makeDefault() throws(JSException) -> StaticBox
3-
}
4-
51
@JSClass struct StaticBox {
62
@JSFunction static func create(_ value: Double) throws(JSException) -> StaticBox
73
@JSFunction func value() throws(JSException) -> Double
84
@JSFunction static func value() throws(JSException) -> Double
9-
}
10-
11-
extension StaticBox {
5+
@JSFunction static func makeDefault() throws(JSException) -> StaticBox
126
@JSFunction(jsName: "with-dashes") static func dashed() throws(JSException) -> StaticBox
137
}
148

Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/JSClassStaticFunctions.json

Lines changed: 23 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -24,43 +24,6 @@
2424

2525
],
2626
"types" : [
27-
{
28-
"getters" : [
29-
30-
],
31-
"methods" : [
32-
33-
],
34-
"name" : "StaticBox",
35-
"setters" : [
36-
37-
],
38-
"staticMethods" : [
39-
{
40-
"name" : "makeDefault",
41-
"parameters" : [
42-
43-
],
44-
"returnType" : {
45-
"jsObject" : {
46-
"_0" : "StaticBox"
47-
}
48-
}
49-
},
50-
{
51-
"jsName" : "with-dashes",
52-
"name" : "dashed",
53-
"parameters" : [
54-
55-
],
56-
"returnType" : {
57-
"jsObject" : {
58-
"_0" : "StaticBox"
59-
}
60-
}
61-
}
62-
]
63-
},
6427
{
6528
"getters" : [
6629

@@ -111,6 +74,29 @@
11174

11275
}
11376
}
77+
},
78+
{
79+
"name" : "makeDefault",
80+
"parameters" : [
81+
82+
],
83+
"returnType" : {
84+
"jsObject" : {
85+
"_0" : "StaticBox"
86+
}
87+
}
88+
},
89+
{
90+
"jsName" : "with-dashes",
91+
"name" : "dashed",
92+
"parameters" : [
93+
94+
],
95+
"returnType" : {
96+
"jsObject" : {
97+
"_0" : "StaticBox"
98+
}
99+
}
114100
}
115101
]
116102
},

Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/JSClassStaticFunctions.swift

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,35 @@
11
#if arch(wasm32)
2-
@_extern(wasm, module: "TestModule", name: "bjs_StaticBox_makeDefault_static")
3-
fileprivate func bjs_StaticBox_makeDefault_static() -> Int32
2+
@_extern(wasm, module: "TestModule", name: "bjs_StaticBox_create_static")
3+
fileprivate func bjs_StaticBox_create_static(_ value: Float64) -> Int32
44
#else
5-
fileprivate func bjs_StaticBox_makeDefault_static() -> Int32 {
5+
fileprivate func bjs_StaticBox_create_static(_ value: Float64) -> Int32 {
66
fatalError("Only available on WebAssembly")
77
}
88
#endif
99

1010
#if arch(wasm32)
11-
@_extern(wasm, module: "TestModule", name: "bjs_StaticBox_dashed_static")
12-
fileprivate func bjs_StaticBox_dashed_static() -> Int32
11+
@_extern(wasm, module: "TestModule", name: "bjs_StaticBox_value_static")
12+
fileprivate func bjs_StaticBox_value_static() -> Float64
1313
#else
14-
fileprivate func bjs_StaticBox_dashed_static() -> Int32 {
14+
fileprivate func bjs_StaticBox_value_static() -> Float64 {
1515
fatalError("Only available on WebAssembly")
1616
}
1717
#endif
1818

19-
func _$StaticBox_makeDefault() throws(JSException) -> StaticBox {
20-
let ret = bjs_StaticBox_makeDefault_static()
21-
if let error = _swift_js_take_exception() {
22-
throw error
23-
}
24-
return StaticBox.bridgeJSLiftReturn(ret)
25-
}
26-
27-
func _$StaticBox_dashed() throws(JSException) -> StaticBox {
28-
let ret = bjs_StaticBox_dashed_static()
29-
if let error = _swift_js_take_exception() {
30-
throw error
31-
}
32-
return StaticBox.bridgeJSLiftReturn(ret)
33-
}
34-
3519
#if arch(wasm32)
36-
@_extern(wasm, module: "TestModule", name: "bjs_StaticBox_create_static")
37-
fileprivate func bjs_StaticBox_create_static(_ value: Float64) -> Int32
20+
@_extern(wasm, module: "TestModule", name: "bjs_StaticBox_makeDefault_static")
21+
fileprivate func bjs_StaticBox_makeDefault_static() -> Int32
3822
#else
39-
fileprivate func bjs_StaticBox_create_static(_ value: Float64) -> Int32 {
23+
fileprivate func bjs_StaticBox_makeDefault_static() -> Int32 {
4024
fatalError("Only available on WebAssembly")
4125
}
4226
#endif
4327

4428
#if arch(wasm32)
45-
@_extern(wasm, module: "TestModule", name: "bjs_StaticBox_value_static")
46-
fileprivate func bjs_StaticBox_value_static() -> Float64
29+
@_extern(wasm, module: "TestModule", name: "bjs_StaticBox_dashed_static")
30+
fileprivate func bjs_StaticBox_dashed_static() -> Int32
4731
#else
48-
fileprivate func bjs_StaticBox_value_static() -> Float64 {
32+
fileprivate func bjs_StaticBox_dashed_static() -> Int32 {
4933
fatalError("Only available on WebAssembly")
5034
}
5135
#endif
@@ -76,6 +60,22 @@ func _$StaticBox_value() throws(JSException) -> Double {
7660
return Double.bridgeJSLiftReturn(ret)
7761
}
7862

63+
func _$StaticBox_makeDefault() throws(JSException) -> StaticBox {
64+
let ret = bjs_StaticBox_makeDefault_static()
65+
if let error = _swift_js_take_exception() {
66+
throw error
67+
}
68+
return StaticBox.bridgeJSLiftReturn(ret)
69+
}
70+
71+
func _$StaticBox_dashed() throws(JSException) -> StaticBox {
72+
let ret = bjs_StaticBox_dashed_static()
73+
if let error = _swift_js_take_exception() {
74+
throw error
75+
}
76+
return StaticBox.bridgeJSLiftReturn(ret)
77+
}
78+
7979
func _$StaticBox_value(_ self: JSObject) throws(JSException) -> Double {
8080
let selfValue = self.bridgeJSLowerParameter()
8181
let ret = bjs_StaticBox_value(selfValue)

Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/JSClassStaticFunctions.d.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
// To update this file, just rebuild your project or run
55
// `swift package bridge-js`.
66

7-
export interface StaticBox {
8-
}
97
export interface StaticBox {
108
value(): number;
119
}
@@ -14,13 +12,11 @@ export interface WithCtor {
1412
export type Exports = {
1513
}
1614
export type Imports = {
17-
StaticBox: {
18-
makeDefault(): StaticBox;
19-
"with-dashes"(): StaticBox;
20-
}
2115
StaticBox: {
2216
create(value: number): StaticBox;
2317
value(): number;
18+
makeDefault(): StaticBox;
19+
"with-dashes"(): StaticBox;
2420
}
2521
WithCtor: {
2622
new(value: number): WithCtor;

Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/JSClassStaticFunctions.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -203,37 +203,37 @@ export async function createInstantiator(options, swift) {
203203
return pointer || 0;
204204
}
205205
const TestModule = importObject["TestModule"] = importObject["TestModule"] || {};
206-
TestModule["bjs_StaticBox_makeDefault_static"] = function bjs_StaticBox_makeDefault_static() {
206+
TestModule["bjs_StaticBox_create_static"] = function bjs_StaticBox_create_static(value) {
207207
try {
208-
let ret = imports.StaticBox.makeDefault();
208+
let ret = imports.StaticBox.create(value);
209209
return swift.memory.retain(ret);
210210
} catch (error) {
211211
setException(error);
212212
return 0
213213
}
214214
}
215-
TestModule["bjs_StaticBox_dashed_static"] = function bjs_StaticBox_dashed_static() {
215+
TestModule["bjs_StaticBox_value_static"] = function bjs_StaticBox_value_static() {
216216
try {
217-
let ret = imports.StaticBox["with-dashes"]();
218-
return swift.memory.retain(ret);
217+
let ret = imports.StaticBox.value();
218+
return ret;
219219
} catch (error) {
220220
setException(error);
221221
return 0
222222
}
223223
}
224-
TestModule["bjs_StaticBox_create_static"] = function bjs_StaticBox_create_static(value) {
224+
TestModule["bjs_StaticBox_makeDefault_static"] = function bjs_StaticBox_makeDefault_static() {
225225
try {
226-
let ret = imports.StaticBox.create(value);
226+
let ret = imports.StaticBox.makeDefault();
227227
return swift.memory.retain(ret);
228228
} catch (error) {
229229
setException(error);
230230
return 0
231231
}
232232
}
233-
TestModule["bjs_StaticBox_value_static"] = function bjs_StaticBox_value_static() {
233+
TestModule["bjs_StaticBox_dashed_static"] = function bjs_StaticBox_dashed_static() {
234234
try {
235-
let ret = imports.StaticBox.value();
236-
return ret;
235+
let ret = imports.StaticBox["with-dashes"]();
236+
return swift.memory.retain(ret);
237237
} catch (error) {
238238
setException(error);
239239
return 0

0 commit comments

Comments
 (0)