Skip to content

Commit 7a2a1ad

Browse files
Standardizing platform Name variances. (#834)
Fix the bug causing DocC to display repeated platforms due to variations in platform names between multiple SGFs and the info.plist. This change unifies platform name variations into their canonical form during symbol graph loading. rdar://116399438
1 parent 8f2fbc8 commit 7a2a1ad

File tree

3 files changed

+146
-7
lines changed

3 files changed

+146
-7
lines changed

Sources/SwiftDocC/Infrastructure/Symbol Graph/SymbolGraphLoader.swift

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -362,30 +362,35 @@ extension SymbolGraph.Symbol.Availability.AvailabilityItem {
362362
guard let domain = self.domain else {
363363
return self
364364
}
365+
366+
var newValue = self
367+
// To ensure the uniformity of platform availability names derived from SGFs,
368+
// we replace the original domain value with a value from the platform's name
369+
// since the platform name maps aliases to the canonical name.
370+
let platformName = PlatformName(operatingSystemName: domain.rawValue)
371+
newValue.domain = SymbolGraph.Symbol.Availability.Domain(rawValue: platformName.rawValue)
365372

366373
// If a symbol is unconditionally unavailable for a given domain,
367374
// don't add an introduced version here as it may cause it to
368375
// incorrectly display availability information
369376
guard !isUnconditionallyUnavailable else {
370-
return self
377+
return newValue
371378
}
372379

373380
// If this had an explicit introduced version from source, don't replace it.
374381
guard introducedVersion == nil else {
375-
return self
382+
return newValue
376383
}
377384

378-
let platformName = PlatformName(operatingSystemName: domain.rawValue)
379385
let fallbackPlatformName = fallbackPlatform.map(PlatformName.init(operatingSystemName:))
380386

381387
// Try to find a default version string for this availability
382388
// item's platform (a.k.a. domain)
383389
guard let platformVersion = defaults[platformName] ??
384390
fallbackPlatformName.flatMap({ defaults[$0] }) else {
385-
return self
391+
return newValue
386392
}
387393

388-
var newValue = self
389394
newValue.introducedVersion = platformVersion
390395
return newValue
391396
}

Sources/SwiftDocCTestUtilities/SymbolGraphCreation.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ extension XCTestCase {
2929
)
3030
}
3131

32-
public func makeSymbolGraphString(moduleName: String, symbols: String = "", relationships: String = "") -> String {
32+
public func makeSymbolGraphString(moduleName: String, symbols: String = "", relationships: String = "", patform: String = "") -> String {
3333
return """
3434
{
3535
"metadata": {
@@ -42,7 +42,7 @@ extension XCTestCase {
4242
},
4343
"module": {
4444
"name": "\(moduleName)",
45-
"platform": { }
45+
"platform": { \(patform) }
4646
},
4747
"relationships" : [
4848
\(relationships)

Tests/SwiftDocCTests/Infrastructure/SymbolGraph/SymbolGraphLoaderTests.swift

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,140 @@ class SymbolGraphLoaderTests: XCTestCase {
381381
)
382382
}
383383

384+
func testCanonicalPlatformNameUniformity() throws {
385+
386+
let testBundle = Folder(name: "TestBundle.docc", content: [
387+
TextFile(name: "Info.plist", utf8Content: """
388+
<plist version="1.0">
389+
<dict>
390+
<key>CFBundleDisplayName</key>
391+
<string>MyModule</string>
392+
<key>CFBundleIdentifier</key>
393+
<string>com.apple.MyModule</string>
394+
<key>CFBundleVersion</key>
395+
<string>0.1.0</string>
396+
<key>CDAppleDefaultAvailability</key>
397+
<dict>
398+
<key>MyModule</key>
399+
<array>
400+
<dict>
401+
<key>name</key>
402+
<string>Mac Catalyst</string>
403+
<key>version</key>
404+
<string>1.0</string>
405+
</dict>
406+
</array>
407+
</dict>
408+
</dict>
409+
</plist>
410+
"""),
411+
Folder(name: "Symbols", content: [
412+
TextFile(name: "MyModule-tvos-objc.symbols.json", utf8Content: makeSymbolGraphString(
413+
moduleName: "MyModule",
414+
symbols: """
415+
{
416+
"accessLevel" : "public",
417+
"availability" : [
418+
{
419+
"domain" : "tvos",
420+
"introduced" : {
421+
"major" : 15,
422+
"minor" : 2,
423+
"patch" : 0
424+
}
425+
},
426+
],
427+
"declarationFragments" : [],
428+
"functionSignature" : {
429+
"parameters" : [],
430+
"returns" : []
431+
},
432+
"identifier" : {
433+
"interfaceLanguage" : "objective-c",
434+
"precise" : "c:@F@A"
435+
},
436+
"kind" : {
437+
"displayName" : "Instance Property",
438+
"identifier" : "objective-c.property"
439+
},
440+
"names" : {
441+
"subHeading" : [],
442+
"title" : "A"
443+
},
444+
"pathComponents" : ["A"]
445+
}
446+
""",
447+
patform: """
448+
"operatingSystem" : {
449+
"minimumVersion" : {
450+
"major" : 12,
451+
"minor" : 0,
452+
"patch" : 0
453+
},
454+
"name" : "tvos"
455+
}
456+
"""
457+
)),
458+
TextFile(name: "MyModule-catalyst-objc.symbols.json", utf8Content: makeSymbolGraphString(moduleName: "MyModule", symbols: """
459+
{
460+
"accessLevel" : "public",
461+
"availability" : [
462+
{
463+
"domain" : "maccatalyst",
464+
"introduced" : {
465+
"major" : 15,
466+
"minor" : 2,
467+
"patch" : 0
468+
}
469+
},
470+
],
471+
"declarationFragments" : [],
472+
"functionSignature" : {
473+
"parameters" : [],
474+
"returns" : []
475+
},
476+
"identifier" : {
477+
"interfaceLanguage" : "objective-c",
478+
"precise" : "c:@F@A"
479+
},
480+
"kind" : {
481+
"displayName" : "Instance Property",
482+
"identifier" : "objective-c.property"
483+
},
484+
"names" : {
485+
"subHeading" : [],
486+
"title" : "A"
487+
},
488+
"pathComponents" : ["A"]
489+
}
490+
""",
491+
patform: """
492+
"operatingSystem" : {
493+
"minimumVersion" : {
494+
"major" : 12,
495+
"minor" : 0,
496+
"patch" : 0
497+
},
498+
"name" : "ios"
499+
}
500+
""")),
501+
]),
502+
])
503+
let tempURL = try createTemporaryDirectory()
504+
let bundleURL = try testBundle.write(inside: tempURL)
505+
let (_, _, context) = try loadBundle(from: bundleURL)
506+
guard let availability = (context.documentationCache["c:@F@A"]?.semantic as? Symbol)?.availability?.availability else {
507+
XCTFail("Did not find availability for symbol 'c:@F@A'")
508+
return
509+
}
510+
// Verify we use one canonical platform name 'macCatalyst' for both
511+
// 'Mac Catalyst' (info.plist) and 'maccatalyst' (SGF).
512+
XCTAssertTrue(availability.count == 2)
513+
XCTAssertTrue(availability.filter({ $0.domain?.rawValue == "macCatalyst" }).count == 1)
514+
XCTAssertTrue(availability.filter({ $0.domain?.rawValue == "maccatalyst" }).count == 0)
515+
516+
}
517+
384518
// MARK: - Helpers
385519

386520
private func makeSymbolGraphLoader(

0 commit comments

Comments
 (0)