Skip to content

Commit abf1c88

Browse files
authored
Revert "BridgeJS: Correctly emit @js methods in extensions" (#703)
Revert of #694. Required to unblock CI on #702.
1 parent 95bb3af commit abf1c88

33 files changed

+1
-1535
lines changed

Plugins/BridgeJS/Sources/BridgeJSCore/SwiftToSkeleton.swift

Lines changed: 1 addition & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,12 @@ public final class SwiftToSkeleton {
4343
var perSourceErrors: [(inputFilePath: String, errors: [DiagnosticError])] = []
4444
var importedFiles: [ImportedFileSkeleton] = []
4545
var exported = ExportedSkeleton(functions: [], classes: [], enums: [], exposeToGlobal: exposeToGlobal)
46-
var exportCollectors: [ExportSwiftAPICollector] = []
4746

4847
for (sourceFile, inputFilePath) in sourceFiles {
4948
progress.print("Processing \(inputFilePath)")
5049

5150
let exportCollector = ExportSwiftAPICollector(parent: self)
5251
exportCollector.walk(sourceFile)
53-
exportCollectors.append(exportCollector)
5452

5553
let typeNameCollector = ImportSwiftMacrosJSImportTypeNameCollector(viewMode: .sourceAccurate)
5654
typeNameCollector.walk(sourceFile)
@@ -76,15 +74,7 @@ public final class SwiftToSkeleton {
7674
if !importedFile.isEmpty {
7775
importedFiles.append(importedFile)
7876
}
79-
}
80-
81-
// Resolve extensions against all collectors. This needs to happen at this point so we can resolve both same file and cross file extensions.
82-
for source in exportCollectors {
83-
source.resolveDeferredExtensions(against: exportCollectors)
84-
}
85-
86-
for collector in exportCollectors {
87-
collector.finalize(&exported)
77+
exportCollector.finalize(&exported)
8878
}
8979

9080
if !perSourceErrors.isEmpty {
@@ -496,8 +486,6 @@ private final class ExportSwiftAPICollector: SyntaxAnyVisitor {
496486
var exportedStructNames: [String] = []
497487
var exportedStructByName: [String: ExportedStruct] = [:]
498488
var errors: [DiagnosticError] = []
499-
/// Extensions collected during the walk, to be resolved after all files have been walked
500-
var deferredExtensions: [ExtensionDeclSyntax] = []
501489

502490
func finalize(_ result: inout ExportedSkeleton) {
503491
result.functions.append(contentsOf: exportedFunctions)
@@ -1400,64 +1388,6 @@ private final class ExportSwiftAPICollector: SyntaxAnyVisitor {
14001388
}
14011389
}
14021390

1403-
override func visit(_ node: ExtensionDeclSyntax) -> SyntaxVisitorContinueKind {
1404-
// Defer until all type declarations in the module have been collected.
1405-
deferredExtensions.append(node)
1406-
return .skipChildren
1407-
}
1408-
1409-
func resolveDeferredExtensions(against collectors: [ExportSwiftAPICollector]) {
1410-
for ext in deferredExtensions {
1411-
var resolved = false
1412-
for collector in collectors {
1413-
if collector.resolveExtension(ext) {
1414-
resolved = true
1415-
break
1416-
}
1417-
}
1418-
if !resolved {
1419-
diagnose(
1420-
node: ext.extendedType,
1421-
message: "Unsupported type '\(ext.extendedType.trimmedDescription)'.",
1422-
hint: "You can only extend `@JS` annotated types defined in the same module"
1423-
)
1424-
}
1425-
}
1426-
}
1427-
1428-
/// Walks extension members under the matching type’s state, returning whether the type was found.
1429-
///
1430-
/// Note: The lookup scans dictionaries keyed by `makeKey(name:namespace:)`, matching only by
1431-
/// plain name. If two types share a name but differ by namespace, `.first(where:)` picks
1432-
/// whichever comes first. This is acceptable today since namespace collisions are unlikely,
1433-
/// but may need refinement if namespace-qualified extension resolution is added.
1434-
func resolveExtension(_ ext: ExtensionDeclSyntax) -> Bool {
1435-
let name = ext.extendedType.trimmedDescription
1436-
let state: State
1437-
if let entry = exportedClassByName.first(where: { $0.value.name == name }) {
1438-
state = .classBody(name: name, key: entry.key)
1439-
} else if let entry = exportedStructByName.first(where: { $0.value.name == name }) {
1440-
state = .structBody(name: name, key: entry.key)
1441-
} else if let entry = exportedEnumByName.first(where: { $0.value.name == name }) {
1442-
state = .enumBody(name: name, key: entry.key)
1443-
} else if exportedProtocolByName.values.contains(where: { $0.name == name }) {
1444-
diagnose(
1445-
node: ext.extendedType,
1446-
message: "Protocol extensions are not supported by BridgeJS.",
1447-
hint: "You cannot extend `@JS` protocol '\(name)' with additional members"
1448-
)
1449-
return true
1450-
} else {
1451-
return false
1452-
}
1453-
stateStack.push(state: state)
1454-
for member in ext.memberBlock.members {
1455-
walk(member)
1456-
}
1457-
stateStack.pop()
1458-
return true
1459-
}
1460-
14611391
override func visit(_ node: EnumDeclSyntax) -> SyntaxVisitorContinueKind {
14621392
guard let jsAttribute = node.attributes.firstJSAttribute else {
14631393
return .skipChildren

Plugins/BridgeJS/Tests/BridgeJSToolTests/BridgeJSCodegenTests.swift

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -167,23 +167,6 @@ import Testing
167167
try snapshotCodegen(skeleton: skeleton, name: "CrossFileFunctionTypes.ReverseOrder")
168168
}
169169

170-
@Test
171-
func codegenCrossFileExtension() throws {
172-
let swiftAPI = SwiftToSkeleton(progress: .silent, moduleName: "TestModule", exposeToGlobal: false)
173-
let classURL = Self.multifileInputsDirectory.appendingPathComponent("CrossFileExtensionClass.swift")
174-
swiftAPI.addSourceFile(
175-
Parser.parse(source: try String(contentsOf: classURL, encoding: .utf8)),
176-
inputFilePath: "CrossFileExtensionClass.swift"
177-
)
178-
let extensionURL = Self.multifileInputsDirectory.appendingPathComponent("CrossFileExtension.swift")
179-
swiftAPI.addSourceFile(
180-
Parser.parse(source: try String(contentsOf: extensionURL, encoding: .utf8)),
181-
inputFilePath: "CrossFileExtension.swift"
182-
)
183-
let skeleton = try swiftAPI.finalize()
184-
try snapshotCodegen(skeleton: skeleton, name: "CrossFileExtension")
185-
}
186-
187170
@Test
188171
func codegenSkipsEmptySkeletons() throws {
189172
let swiftAPI = SwiftToSkeleton(progress: .silent, moduleName: "TestModule", exposeToGlobal: false)

Plugins/BridgeJS/Tests/BridgeJSToolTests/Inputs/MacroSwift/Multifile/CrossFileExtension.swift

Lines changed: 0 additions & 5 deletions
This file was deleted.

Plugins/BridgeJS/Tests/BridgeJSToolTests/Inputs/MacroSwift/Multifile/CrossFileExtensionClass.swift

Lines changed: 0 additions & 4 deletions
This file was deleted.

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

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,3 @@ enum APIResult {
3838
}
3939
}
4040
}
41-
42-
extension MathUtils {
43-
@JS static func divide(a: Int, b: Int) -> Int {
44-
return a / b
45-
}
46-
47-
@JS static var pi: Double { 3.14159 }
48-
}
49-
50-
extension Calculator {
51-
@JS static func cube(value: Int) -> Int {
52-
return value * value * value
53-
}
54-
55-
@JS static var version: String { "1.0" }
56-
}

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

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,6 @@
1212
}
1313
}
1414

15-
extension Greeter {
16-
@JS func greetEnthusiastically() -> String {
17-
return "Hey, " + self.name + "!!!"
18-
}
19-
20-
@JS var nameCount: Int { name.count }
21-
22-
@JS static func greetAnonymously() -> String {
23-
return "Hello."
24-
}
25-
26-
@JS static var defaultGreeting: String { "Hello, world!" }
27-
}
28-
2915
@JS func takeGreeter(greeter: Greeter) {
3016
print(greeter.greet())
3117
}

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

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -60,26 +60,3 @@
6060
}
6161

6262
@JS func roundtripContainer(_ container: Container) -> Container
63-
64-
@JS struct Vector2D {
65-
var dx: Double
66-
var dy: Double
67-
}
68-
69-
extension Vector2D {
70-
@JS func magnitude() -> Double {
71-
return (dx * dx + dy * dy).squareRoot()
72-
}
73-
74-
@JS func scaled(by factor: Double) -> Vector2D {
75-
return Vector2D(dx: dx * factor, dy: dy * factor)
76-
}
77-
}
78-
79-
extension DataPoint {
80-
@JS static func origin() -> DataPoint {
81-
return DataPoint(x: 0, y: 0, label: "origin", optCount: nil, optFlag: nil)
82-
}
83-
84-
@JS static var dimensions: Int { 2 }
85-
}

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

Lines changed: 0 additions & 82 deletions
This file was deleted.

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

Lines changed: 0 additions & 60 deletions
This file was deleted.

0 commit comments

Comments
 (0)