Skip to content

Commit 7d4fe42

Browse files
authored
Don't shadow VersionTupleSyntax.components. (#177)
Since we first wrote the code in VersionTupleSyntaxAdditions.swift, a `components` property has been added to `VersionTupleSyntax`. We shouldn't shadow it with our own property (and we should call it in order to more efficiently and correctly get version info than we do now.)
1 parent 694f738 commit 7d4fe42

File tree

2 files changed

+19
-23
lines changed

2 files changed

+19
-23
lines changed

Sources/TestingMacros/Support/Additions/VersionTupleSyntaxAdditions.swift

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public import SwiftSyntax
1313
extension VersionTupleSyntax {
1414
/// A type describing the major, minor, and patch components of a version
1515
/// tuple.
16-
struct Components: Comparable, CustomStringConvertible {
16+
struct ComponentValues: Comparable, CustomStringConvertible {
1717
/// The major component.
1818
var major: UInt64
1919

@@ -47,27 +47,23 @@ extension VersionTupleSyntax {
4747
}
4848
}
4949

50-
/// The major, minor, and patch components of this version tuple.
51-
var components: Components {
52-
#if swift(<6.0)
53-
let stringComponents = trimmedDescription.split(separator: "." as Character)
54-
guard let major = stringComponents.first.flatMap({ UInt64($0) }) else {
55-
return Components(major: 0)
50+
/// The numeric values of the major, minor, and patch components.
51+
var componentValues: ComponentValues {
52+
let components = components
53+
let startIndex = components.startIndex
54+
55+
let major = UInt64(major.text) ?? 0
56+
let minor: UInt64? = if components.count > 0 {
57+
UInt64(components[startIndex].number.text)
58+
} else {
59+
nil
5660
}
57-
var minor: UInt64?
58-
var patch: UInt64?
59-
if stringComponents.count > 1 {
60-
minor = UInt64(stringComponents[1])
61-
if stringComponents.count > 2 {
62-
patch = UInt64(stringComponents[2])
63-
}
61+
let patch: UInt64? = if components.count > 1 {
62+
UInt64(components[components.index(after: startIndex)].number.text)
63+
} else {
64+
nil
6465
}
65-
#else
66-
let major = UInt64(major.text) ?? 0
67-
let minor = minor.map(\.text).flatMap(UInt64.init)
68-
let patch = patch.map(\.text).flatMap(UInt64.init)
69-
#endif
7066

71-
return Components(major: major, minor: minor, patch: patch)
67+
return ComponentValues(major: major, minor: minor, patch: patch)
7268
}
7369
}

Sources/TestingMacros/Support/AvailabilityGuards.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ private func _createAvailabilityTraitExpr(
6565
when whenKeyword: Keyword,
6666
in context: some MacroExpansionContext
6767
) -> ExprSyntax {
68-
let version: ExprSyntax = availability.version.map(\.components).map { components in
68+
let version: ExprSyntax = availability.version.map(\.componentValues).map { components in
6969
"(\(literal: components.major), \(literal: components.minor), \(literal: components.patch))"
7070
} ?? "nil"
7171
let message = availability.message.map(\.trimmed).map(ExprSyntax.init) ?? "nil"
@@ -227,11 +227,11 @@ func createSyntaxNode(
227227
do {
228228
let introducedVersion = decl.availability(when: .introduced).lazy
229229
.filter(\.isSwift)
230-
.compactMap(\.version?.components)
230+
.compactMap(\.version?.componentValues)
231231
.max()
232232
let obsoletedVersion = decl.availability(when: .obsoleted).lazy
233233
.filter(\.isSwift)
234-
.compactMap(\.version?.components)
234+
.compactMap(\.version?.componentValues)
235235
.min()
236236

237237
let swiftVersionGuardExpr: ExprSyntax? = switch (introducedVersion, obsoletedVersion) {

0 commit comments

Comments
 (0)