Skip to content

Commit c5f99a2

Browse files
authored
Fix missing platforms in declaration fragments (#1314)
* Update tests for platform expansion in declaration fragments Updates test assertions to expect iOS + iPadOS + Mac Catalyst when iOS is present in declaration platforms, using Set-based comparisons for order-independent testing. The modified tests are temporarily skipped until the platform expansion functionality is implemented in the next commit. * Expand iOS platform to include iPadOS and Mac Catalyst in declaration fragments Automatically expands iOS to include its fallback platforms (iPadOS and Mac Catalyst) in declaration fragment platform arrays to ensure consistent platform representation. Uses the existing DefaultAvailability.fallbackPlatforms mapping to determine which platforms should be included when a base platform is present. rdar://158142013
1 parent eeabad0 commit c5f99a2

File tree

4 files changed

+31
-8
lines changed

4 files changed

+31
-8
lines changed

Sources/SwiftDocC/Model/Rendering/RenderSectionTranslator/DeclarationsSectionTranslator.swift

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
This source file is part of the Swift.org open source project
33

4-
Copyright (c) 2021-2024 Apple Inc. and the Swift project authors
4+
Copyright (c) 2021-2025 Apple Inc. and the Swift project authors
55
Licensed under Apache License v2.0 with Runtime Library Exception
66

77
See https://swift.org/LICENSE.txt for license information
@@ -187,6 +187,26 @@ struct DeclarationsSectionTranslator: RenderSectionTranslator {
187187
return declarations
188188
}
189189

190+
/// Returns the given platforms with any missing fallback platforms added.
191+
///
192+
/// This function uses the centralized `DefaultAvailability.fallbackPlatforms` mapping to ensure
193+
/// consistency with platform expansion logic used throughout the codebase.
194+
///
195+
/// For example, when iOS is present in the platforms array, this function adds iPadOS and Mac Catalyst
196+
/// if they are not already included.
197+
///
198+
/// - Parameter platforms: The original platforms array.
199+
/// - Returns: The platforms array with fallback platforms added where applicable.
200+
func expandPlatformsWithFallbacks(_ platforms: [PlatformName?]) -> [PlatformName?] {
201+
guard !platforms.isEmpty else { return platforms }
202+
203+
// Add fallback platforms if their primary platform is present but the fallback is missing
204+
let fallbacks = DefaultAvailability.fallbackPlatforms.compactMap { fallback, primary in
205+
platforms.contains(primary) && !platforms.contains(fallback) ? fallback : nil
206+
}
207+
return platforms + fallbacks
208+
}
209+
190210
func comparePlatformNames(_ lhs: PlatformName?, _ rhs: PlatformName?) -> Bool {
191211
guard let lhsValue = lhs, let rhsValue = rhs else {
192212
return lhs == nil
@@ -204,6 +224,8 @@ struct DeclarationsSectionTranslator: RenderSectionTranslator {
204224
]
205225
for pair in declaration {
206226
let (platforms, declaration) = pair
227+
let expandedPlatforms = expandPlatformsWithFallbacks(platforms)
228+
let platformNames = sortPlatformNames(expandedPlatforms)
207229

208230
let renderedTokens: [DeclarationRenderSection.Token]
209231
let otherDeclarations: DeclarationRenderSection.OtherDeclarations?
@@ -242,7 +264,7 @@ struct DeclarationsSectionTranslator: RenderSectionTranslator {
242264
declarations.append(
243265
DeclarationRenderSection(
244266
languages: languages,
245-
platforms: sortPlatformNames(platforms),
267+
platforms: platformNames,
246268
tokens: renderedTokens,
247269
otherDeclarations: otherDeclarations
248270
)
@@ -252,7 +274,8 @@ struct DeclarationsSectionTranslator: RenderSectionTranslator {
252274
if let alternateDeclarations = symbol.alternateDeclarationVariants[trait] {
253275
for pair in alternateDeclarations {
254276
let (platforms, decls) = pair
255-
let platformNames = sortPlatformNames(platforms)
277+
let expandedPlatforms = expandPlatformsWithFallbacks(platforms)
278+
let platformNames = sortPlatformNames(expandedPlatforms)
256279
for alternateDeclaration in decls {
257280
let renderedTokens = alternateDeclaration.declarationFragments.map(translateFragment)
258281

Tests/SwiftDocCTests/Model/SemaToRenderNodeTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -998,7 +998,7 @@ class SemaToRenderNodeTests: XCTestCase {
998998
return
999999
}
10001000

1001-
XCTAssertEqual(declarations.declarations[0].platforms, [PlatformName(operatingSystemName: "ios")])
1001+
XCTAssertEqual(Set(declarations.declarations[0].platforms), Set([PlatformName(operatingSystemName: "ios"), PlatformName.iPadOS, PlatformName.catalyst]))
10021002
XCTAssertEqual(declarations.declarations[0].tokens.count, 5)
10031003
XCTAssertEqual(declarations.declarations[0].tokens.map { $0.text }.joined(), "protocol MyProtocol : Hashable")
10041004
XCTAssertEqual(declarations.declarations[0].languages?.first, "swift")
@@ -1257,7 +1257,7 @@ class SemaToRenderNodeTests: XCTestCase {
12571257
return
12581258
}
12591259

1260-
XCTAssertEqual(declarations.declarations[0].platforms, [PlatformName(operatingSystemName: "ios")])
1260+
XCTAssertEqual(Set(declarations.declarations[0].platforms), Set([PlatformName(operatingSystemName: "ios"), PlatformName.iPadOS, PlatformName.catalyst]))
12611261
XCTAssertEqual(declarations.declarations[0].tokens.count, 5)
12621262
XCTAssertEqual(declarations.declarations[0].tokens.map { $0.text }.joined(), "protocol MyProtocol : Hashable")
12631263
XCTAssertEqual(declarations.declarations[0].languages?.first, "swift")

Tests/SwiftDocCTests/Rendering/DeclarationsRenderSectionTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ class DeclarationsRenderSectionTests: XCTestCase {
157157
let declarationsSection = try XCTUnwrap(renderNode.primaryContentSections.compactMap({ $0 as? DeclarationsRenderSection }).first)
158158

159159
XCTAssertEqual(declarationsSection.declarations.count, 2)
160-
XCTAssert(declarationsSection.declarations.allSatisfy({ $0.platforms == [.iOS, .macOS] }))
160+
XCTAssert(declarationsSection.declarations.allSatisfy({ Set($0.platforms) == Set([.iOS, .iPadOS, .macOS, .catalyst]) }))
161161
}
162162

163163
func testPlatformSpecificDeclarations() async throws {
@@ -223,7 +223,7 @@ class DeclarationsRenderSectionTests: XCTestCase {
223223
let declarationsSection = try XCTUnwrap(renderNode.primaryContentSections.compactMap({ $0 as? DeclarationsRenderSection }).first)
224224
XCTAssertEqual(declarationsSection.declarations.count, 2)
225225

226-
XCTAssertEqual(declarationsSection.declarations[0].platforms, [.iOS])
226+
XCTAssertEqual(Set(declarationsSection.declarations[0].platforms), Set([.iOS, .iPadOS, .catalyst]))
227227
XCTAssertEqual(declarationsSection.declarations[0].tokens.map(\.text).joined(),
228228
"init(_ content: OtherClass) throws")
229229
XCTAssertEqual(declarationsSection.declarations[1].platforms, [.macOS])

Tests/SwiftDocCTests/Rendering/RenderNodeTranslatorSymbolVariantsTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ class RenderNodeTranslatorSymbolVariantsTests: XCTestCase {
401401
},
402402
assertAfterApplyingVariant: { renderNode in
403403
let declarationSection = try declarationSection(in: renderNode)
404-
XCTAssertEqual(declarationSection.platforms, [.iOS])
404+
XCTAssertEqual(Set(declarationSection.platforms), Set([.iOS, .iPadOS, .catalyst]))
405405

406406
XCTAssertEqual(
407407
declarationSection.tokens,

0 commit comments

Comments
 (0)