Skip to content

Minor fixes for SwiftIfConfig #2772

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 3 additions & 9 deletions Sources/SwiftIfConfig/IfConfigEvaluation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,12 @@ func evaluateIfConfig(
if let prefixOp = condition.as(PrefixOperatorExprSyntax.self),
prefixOp.operator.text == "!"
{
let (innerActive, innersyntaxErrorsAllowed, innerDiagnostics) = evaluateIfConfig(
let (innerActive, innerSyntaxErrorsAllowed, innerDiagnostics) = evaluateIfConfig(
condition: prefixOp.expression,
configuration: configuration
)

return (active: !innerActive, syntaxErrorsAllowed: innersyntaxErrorsAllowed, diagnostics: innerDiagnostics)
return (active: !innerActive, syntaxErrorsAllowed: innerSyntaxErrorsAllowed, diagnostics: innerDiagnostics)
}

// Logical '&&' and '||'.
Expand Down Expand Up @@ -334,13 +334,7 @@ func evaluateIfConfig(
let segment = stringLiteral.segments.first,
case .stringSegment(let stringSegment) = segment
else {
return recordError(
.requiresUnlabeledArgument(
name: "_compiler_version",
role: "version",
syntax: ExprSyntax(call)
)
)
return doVersionComparisonCheck(configuration.compilerVersion)
}

let versionString = stringSegment.content.text
Expand Down
12 changes: 6 additions & 6 deletions Sources/SwiftIfConfig/IfConfigRegionState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ public enum IfConfigRegionState {
case active

/// Evaluate the given `#if` condition using the given build configuration
/// to determine its state and identify any problems encountered along the
/// way.
/// to determine its state, whether syntax errors in inactive conditions are
/// permitted, and to identify any problems encountered along the way.
public static func evaluating(
_ condition: some ExprSyntaxProtocol,
in configuration: some BuildConfiguration
) -> (state: IfConfigRegionState, diagnostics: [Diagnostic]) {
) -> (state: IfConfigRegionState, syntaxErrorsAllowed: Bool, diagnostics: [Diagnostic]) {
// Apply operator folding for !/&&/||.
var foldingDiagnostics: [Diagnostic] = []
let foldedCondition = OperatorTable.logicalOperators.foldAll(condition) { error in
Expand All @@ -44,9 +44,9 @@ public enum IfConfigRegionState {

let diagnostics = foldingDiagnostics + evalDiagnostics
switch (active, syntaxErrorsAllowed) {
case (true, _): return (.active, diagnostics)
case (false, false): return (.inactive, diagnostics)
case (false, true): return (.unparsed, diagnostics)
case (true, _): return (.active, syntaxErrorsAllowed, diagnostics)
case (false, false): return (.inactive, syntaxErrorsAllowed, diagnostics)
case (false, true): return (.unparsed, syntaxErrorsAllowed, diagnostics)
}
}
}
2 changes: 1 addition & 1 deletion Sources/SwiftIfConfig/VersionTuple+Parsing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ extension VersionTuple {
components = []

// Version value are separated by periods.
let componentStrings = versionString.split(separator: ".")
let componentStrings = versionString.split(separator: ".", omittingEmptySubsequences: false)

/// Record a component after checking its value.
func recordComponent(_ value: Int) throws {
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftIfConfig/VersionTuple.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public struct VersionTuple: Sendable {
public init?(parsing string: String) {
self.components = []

for componentText in string.split(separator: ".") {
for componentText in string.split(separator: ".", omittingEmptySubsequences: false) {
guard let component = Int(componentText) else {
return nil
}
Expand Down
26 changes: 25 additions & 1 deletion Tests/SwiftIfConfigTest/EvaluateTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,25 @@ public class EvaluateTests: XCTestCase {
assertIfConfig("swift(>=5.5)", .active)
assertIfConfig("swift(<6)", .active)
assertIfConfig("swift(>=6)", .unparsed)
assertIfConfig(
"swift(>=...)",
.unparsed,
diagnostics: [
DiagnosticSpec(
message: "'swift' version check has invalid version ''",
line: 1,
column: 9
)
]
)
assertIfConfig("compiler(>=5.8)", .active)
assertIfConfig("compiler(>=5.9)", .active)
assertIfConfig("compiler(>=5.10)", .unparsed)
assertIfConfig(#"_compiler_version("5009.*.1")"#, .active)
assertIfConfig(#"_compiler_version("5009.*.3.2.3")"#, .unparsed)
assertIfConfig(#"_compiler_version("5010.*.0")"#, .unparsed)
assertIfConfig("_compiler_version(>=5.8)", .active)
assertIfConfig("_compiler_version(>=12.0)", .unparsed)
assertIfConfig("compiler(>=5.10) && 3.14159", .unparsed)
assertIfConfig(
"compiler(>=5.10) || 3.14159",
Expand All @@ -206,6 +219,17 @@ public class EvaluateTests: XCTestCase {
)
]
)
assertIfConfig(
#"_compiler_version("...")"#,
.unparsed,
diagnostics: [
DiagnosticSpec(
message: "found empty version component",
line: 1,
column: 20
)
]
)
}

func testCanImport() throws {
Expand Down Expand Up @@ -269,7 +293,7 @@ fileprivate func assertIfConfig(
// Evaluate the condition to check the state.
let actualDiagnostics: [Diagnostic]
let actualState: IfConfigRegionState
(actualState, actualDiagnostics) = IfConfigRegionState.evaluating(condition, in: configuration)
(actualState, _, actualDiagnostics) = IfConfigRegionState.evaluating(condition, in: configuration)
XCTAssertEqual(actualState, expectedState, file: file, line: line)

// Check the diagnostics.
Expand Down