diff --git a/Sources/GRPCCodeGen/Internal/Translator/IDLToStructuredSwiftTranslator.swift b/Sources/GRPCCodeGen/Internal/Translator/IDLToStructuredSwiftTranslator.swift index 839ef0fa1..f29019dad 100644 --- a/Sources/GRPCCodeGen/Internal/Translator/IDLToStructuredSwiftTranslator.swift +++ b/Sources/GRPCCodeGen/Internal/Translator/IDLToStructuredSwiftTranslator.swift @@ -72,13 +72,23 @@ struct IDLToStructuredSwiftTranslator: Translator { } } - let fileDescription = FileDescription( - topComment: .preFormatted(codeGenerationRequest.leadingTrivia), - imports: try self.makeImports( + let imports: [ImportDescription] + if codeBlocks.isEmpty { + imports = [] + codeBlocks.append( + CodeBlock(comment: .inline("This file contained no services.")) + ) + } else { + imports = try self.makeImports( dependencies: codeGenerationRequest.dependencies, accessLevel: accessLevel, accessLevelOnImports: accessLevelOnImports - ), + ) + } + + let fileDescription = FileDescription( + topComment: .preFormatted(codeGenerationRequest.leadingTrivia), + imports: imports, codeBlocks: codeBlocks ) diff --git a/Sources/GRPCCodeGen/SourceGenerator.swift b/Sources/GRPCCodeGen/SourceGenerator.swift index 709a96e9d..5a7b68572 100644 --- a/Sources/GRPCCodeGen/SourceGenerator.swift +++ b/Sources/GRPCCodeGen/SourceGenerator.swift @@ -87,7 +87,7 @@ public struct SourceGenerator: Sendable { let translator = IDLToStructuredSwiftTranslator() let textRenderer = TextBasedRenderer(indentation: self.config.indentation) - var structuredSwiftRepresentation = try translator.translate( + let structuredSwiftRepresentation = try translator.translate( codeGenerationRequest: request, accessLevel: self.config.accessLevel, accessLevelOnImports: self.config.accessLevelOnImports, @@ -95,13 +95,6 @@ public struct SourceGenerator: Sendable { server: self.config.server ) - if structuredSwiftRepresentation.file.contents.codeBlocks.isEmpty { - structuredSwiftRepresentation.file.contents.imports = [] - structuredSwiftRepresentation.file.contents.codeBlocks.append( - CodeBlock(comment: .inline("This file contained no services.")) - ) - } - let sourceFile = try textRenderer.render(structured: structuredSwiftRepresentation) return sourceFile diff --git a/Tests/GRPCCodeGenTests/Internal/Translator/IDLToStructuredSwiftTranslatorSnippetBasedTests.swift b/Tests/GRPCCodeGenTests/Internal/Translator/IDLToStructuredSwiftTranslatorSnippetBasedTests.swift index a64fe0451..dfc09e84b 100644 --- a/Tests/GRPCCodeGenTests/Internal/Translator/IDLToStructuredSwiftTranslatorSnippetBasedTests.swift +++ b/Tests/GRPCCodeGenTests/Internal/Translator/IDLToStructuredSwiftTranslatorSnippetBasedTests.swift @@ -298,6 +298,25 @@ final class IDLToStructuredSwiftTranslatorSnippetBasedTests: XCTestCase { ) } + func testEmptyFileGeneration() throws { + let expectedSwift = + """ + /// Some really exciting license header 2023. + + + // This file contained no services. + """ + try self.assertIDLToStructuredSwiftTranslation( + codeGenerationRequest: makeCodeGenerationRequest( + services: [], + dependencies: [] + ), + expectedSwift: expectedSwift, + accessLevel: .public, + server: true + ) + } + private func assertIDLToStructuredSwiftTranslation( codeGenerationRequest: CodeGenerationRequest, expectedSwift: String,