Skip to content

Commit d24bbd5

Browse files
authored
Merge pull request #2770 from DougGregor/ifconfig-cleanups
Address more code review feedback for the SwiftIfConfig library
2 parents c09c9a1 + dad947a commit d24bbd5

File tree

4 files changed

+43
-9
lines changed

4 files changed

+43
-9
lines changed

Sources/SwiftIfConfig/BuildConfiguration.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ public enum CanImportVersion {
4444
/// be imported is a complicated task only implemented in the Swift compiler.
4545
/// Therefore, queries are permitted to throw an error to report when they
4646
/// cannot answer a query, in which case this error will be reported to
47-
/// the caller.
47+
/// the caller and the condition will be treated as being "false", so the
48+
/// code covered by the condition will be inactive.
4849
public protocol BuildConfiguration {
4950
/// Determine whether a given custom build condition has been set.
5051
///

Sources/SwiftIfConfig/ConfiguredRegions.swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,18 @@ extension SyntaxProtocol {
2424
/// func f()
2525
/// #elseif B
2626
/// func g()
27+
/// #elseif compiler(>= 12.0)
28+
/// please print the number after 41
2729
/// #endif
2830
/// #else
2931
/// #endif
3032
///
3133
/// If the configuration options `DEBUG` and `B` are provided, but `A` is not,
32-
/// the results will be contain:
33-
/// - Active region for the `#if DEBUG`
34-
/// - Inactive region for the `#if A`
35-
/// - Active region for the `#elseif B`
34+
/// and the compiler version is less than 12.0, the results will be contain:
35+
/// - Active region for the `#if DEBUG`.
36+
/// - Inactive region for the `#if A`.
37+
/// - Active region for the `#elseif B`.
38+
/// - Unparsed region for the `#elseif compiler(>= 12.0)`.
3639
/// - Inactive region for the final `#else`.
3740
public func configuredRegions(
3841
in configuration: some BuildConfiguration

Sources/SwiftIfConfig/SyntaxProtocol+IfConfig.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,19 @@ extension SyntaxProtocol {
2323
/// #if DEBUG
2424
/// #if A
2525
/// func f()
26-
/// #elseif B
27-
/// func g()
26+
/// #elseif B
27+
/// func g()
28+
/// #elseif compiler(>= 12.0)
29+
/// please print the number after 41
2830
/// #endif
2931
/// #endif
3032
///
3133
/// a call to `isActive` on the syntax node for the function `g` would return `active` when the
3234
/// configuration options `DEBUG` and `B` are provided, but `A` is not.
35+
///
36+
/// If the compiler version is smaller than 12.0, then `isActive` on any of the tokens within
37+
/// that `#elseif` block would return "unparsed", because that syntax should not (conceptually)
38+
/// be parsed.
3339
public func isActive(
3440
in configuration: some BuildConfiguration
3541
) -> (state: IfConfigRegionState, diagnostics: [Diagnostic]) {

Tests/SwiftIfConfigTest/EvaluateTests.swift

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,9 +216,9 @@ public class EvaluateTests: XCTestCase {
216216
assertIfConfig("canImport(SwiftSyntax, _version: 5.10)", .inactive)
217217
assertIfConfig(#"canImport(SwiftSyntax, _version: "5.9")"#, .active)
218218
assertIfConfig("canImport(SwiftSyntax, _underlyingVersion: 5009)", .active)
219-
assertIfConfig("canImport(SwiftSyntax, _underlyingVersion: 5009.10", .inactive)
219+
assertIfConfig("canImport(SwiftSyntax, _underlyingVersion: 5009.10)", .inactive)
220220
assertIfConfig(
221-
"canImport(SwiftSyntax, _underlyingVersion: 5009.10.5.4.2.3.5",
221+
"canImport(SwiftSyntax, _underlyingVersion: 5009.10.5.4.2.3.5)",
222222
.inactive,
223223
diagnostics: [
224224
DiagnosticSpec(
@@ -229,6 +229,30 @@ public class EvaluateTests: XCTestCase {
229229
)
230230
]
231231
)
232+
assertIfConfig(
233+
"canImport(SwiftSyntax, _version: 20A301)",
234+
.unparsed,
235+
diagnostics: [
236+
DiagnosticSpec(
237+
message: "'canImport' version check has invalid version '20A301'",
238+
line: 1,
239+
column: 34,
240+
severity: .error
241+
)
242+
]
243+
)
244+
assertIfConfig(
245+
#"canImport(SwiftSyntax, _version: "20A301")"#,
246+
.unparsed,
247+
diagnostics: [
248+
DiagnosticSpec(
249+
message: #"'canImport' version check has invalid version '"20A301"'"#,
250+
line: 1,
251+
column: 34,
252+
severity: .error
253+
)
254+
]
255+
)
232256
}
233257
}
234258

0 commit comments

Comments
 (0)