Skip to content

Commit dd1c226

Browse files
committed
Replace the diagnostic handlers throughout SwiftIfConfig with [Diagnostic] returns
This provides a more consistent diagnostics story.
1 parent fcce246 commit dd1c226

File tree

8 files changed

+163
-151
lines changed

8 files changed

+163
-151
lines changed

Sources/SwiftIfConfig/ConfiguredRegionState.swift

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,29 +24,29 @@ public enum ConfiguredRegionState {
2424
/// The region is active and is part of the compiled program.
2525
case active
2626

27-
/// Evaluate the given `#if` condition using the given build configuration, throwing an error if there is
28-
/// insufficient information to make a determination.
29-
public init(
30-
condition: some ExprSyntaxProtocol,
31-
configuration: some BuildConfiguration,
32-
diagnosticHandler: ((Diagnostic) -> Void)? = nil
33-
) throws {
27+
/// Evaluate the given `#if` condition using the given build configuration
28+
/// to determine its state and identify any problems encountered along the
29+
/// way.
30+
public static func evaluating(
31+
_ condition: some ExprSyntaxProtocol,
32+
in configuration: some BuildConfiguration
33+
) -> (state: ConfiguredRegionState, diagnostics: [Diagnostic]) {
3434
// Apply operator folding for !/&&/||.
35-
let foldedCondition = try OperatorTable.logicalOperators.foldAll(condition) { error in
36-
diagnosticHandler?(error.asDiagnostic)
37-
throw error
35+
var foldingDiagnostics: [Diagnostic] = []
36+
let foldedCondition = OperatorTable.logicalOperators.foldAll(condition) { error in
37+
foldingDiagnostics.append(contentsOf: error.asDiagnostics(at: condition))
3838
}.cast(ExprSyntax.self)
3939

40-
let (active, versioned) = try evaluateIfConfig(
40+
let (active, versioned, evalDiagnostics) = evaluateIfConfig(
4141
condition: foldedCondition,
42-
configuration: configuration,
43-
diagnosticHandler: diagnosticHandler
42+
configuration: configuration
4443
)
4544

45+
let diagnostics = foldingDiagnostics + evalDiagnostics
4646
switch (active, versioned) {
47-
case (true, _): self = .active
48-
case (false, false): self = .inactive
49-
case (false, true): self = .unparsed
47+
case (true, _): return (.active, diagnostics)
48+
case (false, false): return (.inactive, diagnostics)
49+
case (false, true): return (.unparsed, diagnostics)
5050
}
5151
}
5252
}

Sources/SwiftIfConfig/ConfiguredRegions.swift

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,9 @@ fileprivate class ConfiguredRegionVisitor<Configuration: BuildConfiguration>: Sy
8080
}
8181

8282
// For inactive clauses, distinguish between inactive and unparsed.
83-
let isVersioned =
84-
(try? clause.isVersioned(
85-
configuration: configuration,
86-
diagnosticHandler: nil
87-
)) ?? true
83+
let isVersioned = clause.isVersioned(
84+
configuration: configuration
85+
).versioned
8886

8987
// If this is within an active region, or this is an unparsed region,
9088
// record it.

Sources/SwiftIfConfig/IfConfigDecl+IfConfig.swift

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,12 @@ extension IfConfigDeclSyntax {
4444
}
4545

4646
// If this condition evaluates true, return this clause.
47-
let isActive =
48-
(try? evaluateIfConfig(
49-
condition: condition,
50-
configuration: configuration
51-
) {
52-
diagnostics.append($0)
53-
})?.active ?? false
47+
let (isActive, _, localDiagnostics) = evaluateIfConfig(
48+
condition: condition,
49+
configuration: configuration
50+
)
51+
diagnostics.append(contentsOf: localDiagnostics)
52+
5453
if isActive {
5554
return (clause, diagnostics: diagnostics)
5655
}

0 commit comments

Comments
 (0)