Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make empty generated source files descriptive #2151

Merged
merged 7 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,23 @@ struct IDLToStructuredSwiftTranslator: Translator {
}
}

let fileDescription = FileDescription(
topComment: .preFormatted(codeGenerationRequest.leadingTrivia),
imports: try self.makeImports(
let imports: [ImportDescription]
if codeBlocks.isEmpty {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we check this instead? It's easy to imagine unconditionally adding a code block which would result in this check failing.

Suggested change
if codeBlocks.isEmpty {
if codeGenerationRequest.services.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
)

Expand Down
1 change: 1 addition & 0 deletions Sources/GRPCCodeGen/SourceGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public struct SourceGenerator: Sendable {
client: self.config.client,
server: self.config.server
)

let sourceFile = try textRenderer.render(structured: structuredSwiftRepresentation)

return sourceFile
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading