Skip to content

Commit

Permalink
Compute platform beta status when using @Available
Browse files Browse the repository at this point in the history
Changes:
- Sets `AvailabilityRenderItem.isBeta` when `AvailabilityRenderItem.init?(_ :current:)` is called
- Makes `AvailabilityRenderItem.isBeta`, `AvailabilityRenderItem. introduced` and `AvailabilityRenderItem.name`  into `let` properties as they are integral to availability rendering.

Unifies the behavior in `AvailabilityRenderItem` so that we always compute whether a platform is in beta, regardless of whether the availability information comes from the symbol graph or comes from the `@Available` directive.

This fixes an issue where sample code articles defined with `@Available` directives would never display a beta badge regardless of whether the platform was configured to be a beta platform.

Additionally, makes `isBeta` a `let` property to avoid any future bugs, as the code will now no longer compile unless this property is set in an initializer.

Resolves rdar://129355087.
  • Loading branch information
anferbui committed Jun 19, 2024
1 parent 7292d52 commit 98d4c8a
Showing 1 changed file with 13 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ extension VersionTriplet {
/// Availability information of a symbol on a specific platform.
public struct AvailabilityRenderItem: Codable, Hashable, Equatable {
/// The name of the platform on which the symbol is available.
public var name: String?
public let name: String?

/// The version of the platform SDK introducing the symbol.
public var introduced: String?
public let introduced: String?

/// The version of the platform SDK deprecating the symbol.
public var deprecated: String?
Expand All @@ -82,7 +82,7 @@ public struct AvailabilityRenderItem: Codable, Hashable, Equatable {
public var unconditionallyUnavailable: Bool?

/// If `true`, the symbol is introduced in a beta version of this platform.
public var isBeta: Bool?
public let isBeta: Bool?

private enum CodingKeys: String, CodingKey {
case name, introducedAt, deprecatedAt, obsoletedAt, message, renamed, deprecated, unavailable
Expand Down Expand Up @@ -131,12 +131,7 @@ public struct AvailabilityRenderItem: Codable, Hashable, Equatable {
renamed = availability.renamed
unconditionallyUnavailable = availability.isUnconditionallyUnavailable
unconditionallyDeprecated = availability.isUnconditionallyDeprecated

if let introducedVersion, let current, current.beta, introducedVersion == current.version {
isBeta = true
} else {
isBeta = false
}
isBeta = AvailabilityRenderItem.isBeta(introduced: introducedVersion, current: current)
}

init?(_ availability: Metadata.Availability, current: PlatformVersion?) {
Expand All @@ -146,6 +141,15 @@ public struct AvailabilityRenderItem: Codable, Hashable, Equatable {
let platformName = PlatformName(metadataPlatform: availability.platform)
name = platformName?.displayName
introduced = availability.introduced.stringRepresentation(precisionUpToNonsignificant: .minor)
isBeta = AvailabilityRenderItem.isBeta(introduced: availability.introduced, current: current)
}

static func isBeta(introduced: VersionTriplet?, current: PlatformVersion?) -> Bool {
guard let introduced, let current, current.beta, introduced == current.version else {
return false
}

return true
}

/// Creates a new item with the given platform name and version string.
Expand Down

0 comments on commit 98d4c8a

Please sign in to comment.