Skip to content

Commit 49b3993

Browse files
committed
swift format
1 parent 3c4aee6 commit 49b3993

File tree

7 files changed

+154
-148
lines changed

7 files changed

+154
-148
lines changed

Sources/SwiftTSGo/BuildInMemory.swift

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public func build(
5151
return .directory(["src"])
5252
}
5353
if path == "/project/src" {
54-
return .directory(sourceFiles.map { $0.name })
54+
return .directory(sourceFiles.map(\.name))
5555
}
5656
if let source = sourceFiles.first(where: { ("/project/src/" + $0.name) == path }) {
5757
return .file(source.content)
@@ -73,39 +73,39 @@ public func build(
7373

7474
// Create a wrapper resolver that handles tsconfig.json generation and delegation
7575
let wrapperResolver: @Sendable (String) async throws -> FileResolver? = { path in
76-
76+
7777
// 1) Exact match for tsconfig.json - try resolver first, then fallback to config param
7878
if path == "/project/tsconfig.json" {
7979
// First try the provided resolver
8080
if let resolverResult = try await resolver(path) {
8181
return resolverResult
8282
}
83-
83+
8484
// Fallback: generate from config param
8585
let encoder = JSONEncoder()
8686
encoder.outputFormatting = [.prettyPrinted, .sortedKeys]
8787
let configData = try encoder.encode(config)
8888
let configString = String(data: configData, encoding: .utf8) ?? "{}"
8989
return .file(configString)
9090
}
91-
91+
9292
// 2) Root directory - call resolver and ensure tsconfig.json is included
9393
if path == "/project" {
9494
var files: [String] = []
95-
95+
9696
if let resolved = try await resolver(path) {
97-
if case .directory(let upstreamFiles) = resolved {
97+
if case let .directory(upstreamFiles) = resolved {
9898
files = upstreamFiles
9999
}
100100
}
101-
101+
102102
if !files.contains("tsconfig.json") {
103103
files.append("tsconfig.json")
104104
}
105-
105+
106106
return .directory(files)
107107
}
108-
108+
109109
// 3) Everything else - delegate to the resolver param
110110
return try await resolver(path)
111111
}
@@ -117,7 +117,7 @@ public func build(
117117
configFile: "",
118118
resolver: wrapperResolver
119119
)
120-
120+
121121
guard let cResult else {
122122
return InMemoryBuildResult(
123123
success: false,
@@ -130,6 +130,6 @@ public func build(
130130
]
131131
)
132132
}
133-
133+
134134
return processResult(cResult)
135135
}

Sources/SwiftTSGo/DynamicResolver.swift

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import TSCBridge
44
// MARK: - Dynamic File Resolver Callback Management
55

66
/// Swift callback registry for dynamic file resolvers (like plugin registry)
7-
private nonisolated(unsafe) var resolverCallbackRegistry: [UnsafeRawPointer: @Sendable (String) async throws -> FileResolver?] = [:]
7+
private nonisolated(unsafe) var resolverCallbackRegistry: [UnsafeRawPointer: @Sendable (String) async throws
8+
-> FileResolver?] = [:]
89
private let resolverRegistryLock = NSLock()
910

1011
// MARK: - Swift Bridge Functions (like plugin bridge functions)
@@ -17,20 +18,21 @@ public func swiftFileResolveCallback(
1718
) -> UnsafeMutablePointer<c_file_resolve_result>? {
1819
resolverRegistryLock.lock()
1920
defer { resolverRegistryLock.unlock() }
20-
21+
2122
guard let callbackData,
2223
let args,
23-
let resolverCallback = resolverCallbackRegistry[callbackData] else {
24+
let resolverCallback = resolverCallbackRegistry[callbackData]
25+
else {
2426
return nil
2527
}
26-
28+
2729
// Convert C args to Swift
2830
let path = String(cString: args.pointee.path)
29-
31+
3032
// Execute async callback with semaphore blocking (like plugin callbacks)
3133
let semaphore = DispatchSemaphore(value: 0)
3234
nonisolated(unsafe) var result: FileResolver?
33-
35+
3436
Task { @Sendable in
3537
defer { semaphore.signal() }
3638
do {
@@ -39,9 +41,9 @@ public func swiftFileResolveCallback(
3941
result = nil
4042
}
4143
}
42-
44+
4345
semaphore.wait()
44-
46+
4547
// Convert result to C and return
4648
return result?.cValue
4749
}
@@ -52,26 +54,27 @@ extension FileResolver {
5254
/// Convert to C representation
5355
var cValue: UnsafeMutablePointer<c_file_resolve_result> {
5456
let cResult = UnsafeMutablePointer<c_file_resolve_result>.allocate(capacity: 1)
55-
57+
5658
switch self {
57-
case .file(let content):
59+
case let .file(content):
5860
// File case
5961
cResult.pointee.exists = 1
6062
cResult.pointee.content = strdup(content)
6163
cResult.pointee.content_length = Int32(content.utf8.count)
6264
cResult.pointee.directory_files = nil
6365
cResult.pointee.directory_files_count = 0
64-
65-
case .directory(let files):
66+
67+
case let .directory(files):
6668
// Directory case
6769
cResult.pointee.exists = 2
6870
cResult.pointee.content = nil
6971
cResult.pointee.content_length = 0
70-
72+
7173
if !files.isEmpty {
7274
cResult.pointee.directory_files_count = Int32(files.count)
73-
cResult.pointee.directory_files = UnsafeMutablePointer<UnsafeMutablePointer<CChar>?>.allocate(capacity: files.count)
74-
75+
cResult.pointee.directory_files = UnsafeMutablePointer<UnsafeMutablePointer<CChar>?>
76+
.allocate(capacity: files.count)
77+
7578
for (index, file) in files.enumerated() {
7679
cResult.pointee.directory_files[index] = strdup(file)
7780
}
@@ -80,21 +83,23 @@ extension FileResolver {
8083
cResult.pointee.directory_files_count = 0
8184
}
8285
}
83-
86+
8487
return cResult
8588
}
8689
}
8790

8891
// MARK: - Callback Registration (like plugin registration)
8992

9093
/// Register a Swift resolver callback and return a callback ID (like plugin registration)
91-
public func registerDynamicResolver(_ resolver: @escaping @Sendable (String) async throws -> FileResolver?) -> UnsafeRawPointer {
94+
public func registerDynamicResolver(_ resolver: @escaping @Sendable (String) async throws -> FileResolver?)
95+
-> UnsafeRawPointer
96+
{
9297
let callbackID = UnsafeRawPointer(Unmanaged.passUnretained(NSObject()).toOpaque())
93-
98+
9499
resolverRegistryLock.lock()
95100
resolverCallbackRegistry[callbackID] = resolver
96101
resolverRegistryLock.unlock()
97-
102+
98103
return callbackID
99104
}
100105

@@ -114,18 +119,17 @@ public func buildWithDynamicResolver(
114119
configFile: String = "",
115120
resolver: @escaping @Sendable (String) async throws -> FileResolver?
116121
) -> UnsafeMutablePointer<c_build_result>? {
117-
118122
// Register the resolver callback (like plugin registration)
119123
let callbackID = registerDynamicResolver(resolver)
120124
defer { unregisterDynamicResolver(callbackID) }
121-
125+
122126
// Create resolver callbacks structure (like plugin structure)
123127
let cCallbacks = UnsafeMutablePointer<c_resolver_callbacks>.allocate(capacity: 1)
124128
defer { cCallbacks.deallocate() }
125-
129+
126130
cCallbacks.pointee.resolver = swiftFileResolveCallback
127131
cCallbacks.pointee.resolver_data = UnsafeMutableRawPointer(mutating: callbackID)
128-
132+
129133
// Call the dynamic build function
130134
return projectPath.withCString { projectPathPtr in
131135
configFile.withCString { configFilePtr in
@@ -139,4 +143,4 @@ public func buildWithDynamicResolver(
139143
}
140144
}
141145

142-
// MARK: - C String Helper (using existing helper from BuildInMemory.swift)
146+
// MARK: - C String Helper (using existing helper from BuildInMemory.swift)

Sources/SwiftTSGo/ESBuildBuild.swift

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ public struct ESBuildBuildOptions {
601601

602602
// Plugins configuration
603603
if !plugins.isEmpty {
604-
print("Build has \(plugins.count) plugins configured")
604+
// print("Build has \(plugins.count) plugins configured")
605605

606606
// Create C plugins array
607607
options.pointee.plugins_count = Int32(plugins.count)
@@ -1004,31 +1004,34 @@ public func esbuildBuild(
10041004
options: ESBuildBuildOptions = ESBuildBuildOptions(),
10051005
resolver: @escaping @Sendable (String) async throws -> FileResolver?
10061006
) async throws -> ESBuildBuildResult? {
1007-
10081007
// Create a resolver plugin that uses the provided callback
10091008
let resolverPlugin = ESBuildPlugin(name: "dynamic-resolver") { build in
1010-
1009+
let namespace = "swifttsgo"
1010+
10111011
// Handle all file resolution
10121012
build.onResolve(filter: ".*", namespace: nil) { args in
10131013
do {
10141014
// Try to resolve the import path
10151015
let resolvedPath = try await resolveImportPath(args.path, from: args.importer, resolver: resolver)
10161016
if let path = resolvedPath {
1017-
return ESBuildOnResolveResult(path: path)
1017+
return ESBuildOnResolveResult(namespace: namespace, path: path)
10181018
}
10191019
} catch {
10201020
return ESBuildOnResolveResult(
1021-
errors: [ESBuildPluginMessage(text: "Failed to resolve '\(args.path)': \(error.localizedDescription)")]
1021+
errors: [
1022+
ESBuildPluginMessage(text: "Failed to resolve '\(args.path)': \(error.localizedDescription)"),
1023+
]
10221024
)
10231025
}
10241026
return nil
10251027
}
1026-
1028+
10271029
// Handle all file loading
1028-
build.onLoad(filter: ".*", namespace: nil) { args in
1030+
build.onLoad(filter: ".*", namespace: namespace) { args in
10291031
do {
1032+
print("resolver.onLoad", args.path)
10301033
if let fileResolver = try await resolver(args.path) {
1031-
if case .file(let content) = fileResolver {
1034+
if case let .file(content) = fileResolver {
10321035
return ESBuildOnLoadResult(
10331036
contents: content,
10341037
loader: detectLoader(for: args.path)
@@ -1043,11 +1046,11 @@ public func esbuildBuild(
10431046
return nil
10441047
}
10451048
}
1046-
1049+
10471050
// Add the resolver plugin to the build options
10481051
var buildOptions = options
10491052
buildOptions.plugins = buildOptions.plugins + [resolverPlugin]
1050-
1053+
10511054
return esbuildBuild(options: buildOptions)
10521055
}
10531056

@@ -1057,23 +1060,22 @@ private func resolveImportPath(
10571060
from importer: String?,
10581061
resolver: @Sendable (String) async throws -> FileResolver?
10591062
) async throws -> String? {
1060-
10611063
// Handle absolute paths
10621064
if importPath.hasPrefix("/") {
10631065
return try await resolver(importPath) != nil ? importPath : nil
10641066
}
1065-
1067+
10661068
// Handle relative paths
10671069
if importPath.hasPrefix("./") || importPath.hasPrefix("../") {
1068-
guard let importer = importer else { return nil }
1069-
1070+
guard let importer else { return nil }
1071+
10701072
let importerDir = (importer as NSString).deletingLastPathComponent
10711073
let resolvedPath = (importerDir as NSString).appendingPathComponent(importPath)
10721074
let normalizedPath = (resolvedPath as NSString).standardizingPath
1073-
1075+
10741076
return try await resolver(normalizedPath) != nil ? normalizedPath : nil
10751077
}
1076-
1078+
10771079
// Handle node_modules style imports (simplified)
10781080
// For now, just check if the path exists as-is
10791081
return try await resolver(importPath) != nil ? importPath : nil
@@ -1082,7 +1084,7 @@ private func resolveImportPath(
10821084
/// Detect the appropriate loader based on file extension
10831085
private func detectLoader(for path: String) -> ESBuildLoader {
10841086
let ext = (path as NSString).pathExtension.lowercased()
1085-
1087+
10861088
switch ext {
10871089
case "js": return .js
10881090
case "jsx": return .jsx
@@ -1091,7 +1093,7 @@ private func detectLoader(for path: String) -> ESBuildLoader {
10911093
case "json": return .json
10921094
case "css": return .css
10931095
case "txt": return .text
1094-
default: return .js // Default to JS
1096+
default: return .js // Default to JS
10951097
}
10961098
}
10971099

@@ -1104,12 +1106,11 @@ public func esbuildBuild(
11041106
files: [String: String],
11051107
options: ESBuildBuildOptions = ESBuildBuildOptions()
11061108
) async throws -> ESBuildBuildResult? {
1107-
11081109
try await esbuildBuild(options: options) { path in
11091110
if let content = files[path] {
11101111
return .file(content)
11111112
}
1112-
1113+
11131114
// Check if it's a directory by looking for child files
11141115
let childFiles = files.keys
11151116
.filter { $0.hasPrefix(path + "/") }
@@ -1121,11 +1122,11 @@ public func esbuildBuild(
11211122
}
11221123
return nil
11231124
}
1124-
1125+
11251126
if !childFiles.isEmpty {
11261127
return .directory(childFiles)
11271128
}
1128-
1129+
11291130
return nil
11301131
}
11311132
}

Sources/SwiftTSGo/ESBuildPluginCallbacks.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,9 @@ extension ESBuildPlugin {
330330
}
331331

332332
// print(
333-
// "Plugin '\(name)' registered with \(storage.resolveCallbacks.count) async resolve callbacks, \(storage.loadCallbacks.count) async load callbacks, \(storage.startCallbacks.count) async start callbacks, \(storage.endCallbacks.count) async end callbacks"
333+
// "Plugin '\(name)' registered with \(storage.resolveCallbacks.count) async resolve callbacks,
334+
// \(storage.loadCallbacks.count) async load callbacks, \(storage.startCallbacks.count) async start
335+
// callbacks, \(storage.endCallbacks.count) async end callbacks"
334336
// )
335337

336338
return cPlugin
0 Bytes
Binary file not shown.

Tests/SwiftTSGoTests/BuildInMemoryTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ private func buildWithSimpleResolver(
2626
}
2727
return nil
2828
}
29-
29+
3030
// Find all subdirectories that are children of this directory
3131
let childDirs = directories
3232
.filter { $0.hasPrefix(path + "/") }
@@ -38,7 +38,7 @@ private func buildWithSimpleResolver(
3838
}
3939
return nil
4040
}
41-
41+
4242
let allChildren = childFiles + childDirs
4343
return .directory(allChildren)
4444
}
@@ -80,7 +80,7 @@ struct BuildInMemoryTests {
8080
compilerOptions.module = .commonjs
8181
compilerOptions.noEmit = true
8282
compilerOptions.strict = true
83-
83+
8484
let config = TSConfig(compilerOptions: compilerOptions, include: ["src/**/*"])
8585
let result = try await build(config: config, sources)
8686

0 commit comments

Comments
 (0)