Skip to content

Commit f3c3f14

Browse files
committed
Introduce warning to rename targetEnvironment(macabi) to macCatalyst
This matches the behavior of the compiler.
1 parent 1425e5a commit f3c3f14

File tree

3 files changed

+51
-5
lines changed

3 files changed

+51
-5
lines changed

Sources/SwiftIfConfig/IfConfigError.swift

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ enum IfConfigError: Error, CustomStringConvertible {
3232
case integerLiteralCondition(syntax: ExprSyntax, replacement: Bool)
3333
case likelySimulatorPlatform(syntax: ExprSyntax)
3434
case endiannessDoesNotMatch(syntax: ExprSyntax, argument: String)
35+
case macabiIsMacCatalyst(syntax: ExprSyntax)
3536

3637
var description: String {
3738
switch self {
@@ -83,6 +84,9 @@ enum IfConfigError: Error, CustomStringConvertible {
8384
return
8485
"platform condition appears to be testing for simulator environment; use 'targetEnvironment(simulator)' instead"
8586

87+
case .macabiIsMacCatalyst:
88+
return "'macabi' has been renamed to 'macCatalyst'"
89+
8690
case .endiannessDoesNotMatch:
8791
return "unknown endianness for build configuration '_endian' (must be 'big' or 'little')"
8892
}
@@ -105,7 +109,8 @@ enum IfConfigError: Error, CustomStringConvertible {
105109
.ignoredTrailingComponents(version: _, syntax: let syntax),
106110
.integerLiteralCondition(syntax: let syntax, replacement: _),
107111
.likelySimulatorPlatform(syntax: let syntax),
108-
.endiannessDoesNotMatch(syntax: let syntax, argument: _):
112+
.endiannessDoesNotMatch(syntax: let syntax, argument: _),
113+
.macabiIsMacCatalyst(syntax: let syntax):
109114
return Syntax(syntax)
110115

111116
case .unsupportedVersionOperator(name: _, operator: let op):
@@ -124,7 +129,7 @@ extension IfConfigError: DiagnosticMessage {
124129
var severity: SwiftDiagnostics.DiagnosticSeverity {
125130
switch self {
126131
case .compilerVersionSecondComponentNotWildcard, .ignoredTrailingComponents,
127-
.likelySimulatorPlatform, .endiannessDoesNotMatch:
132+
.likelySimulatorPlatform, .endiannessDoesNotMatch, .macabiIsMacCatalyst:
128133
return .warning
129134
default: return .error
130135
}
@@ -171,6 +176,21 @@ extension IfConfigError: DiagnosticMessage {
171176
)
172177
}
173178

179+
// For the targetEnvironment(macabi) -> macCatalyst rename we have a Fix-It.
180+
if case .macabiIsMacCatalyst(syntax: let syntax) = self {
181+
return Diagnostic(
182+
node: syntax,
183+
message: self,
184+
fixIt: .replace(
185+
message: SimpleFixItMessage(
186+
message: "replace with 'macCatalyst'"
187+
),
188+
oldNode: syntax,
189+
newNode: "macCatalyst" as ExprSyntax
190+
)
191+
)
192+
}
193+
174194
return Diagnostic(node: syntax, message: self)
175195
}
176196
}

Sources/SwiftIfConfig/IfConfigEvaluation.swift

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,13 +203,23 @@ func evaluateIfConfig(
203203
) -> (active: Bool, syntaxErrorsAllowed: Bool, diagnostics: [Diagnostic]) {
204204
// Ensure that we have a single argument that is a simple identifier.
205205
guard let argExpr = call.arguments.singleUnlabeledExpression,
206-
let arg = argExpr.simpleIdentifierExpr
206+
var arg = argExpr.simpleIdentifierExpr
207207
else {
208208
return recordError(
209209
.requiresUnlabeledArgument(name: fnName, role: role, syntax: ExprSyntax(call))
210210
)
211211
}
212212

213+
// The historical "macabi" environment has been renamed to "macCatalyst".
214+
if role == "environment" && arg == "macabi" {
215+
extraDiagnostics.append(
216+
IfConfigError.macabiIsMacCatalyst(syntax: argExpr)
217+
.asDiagnostic
218+
)
219+
220+
arg = "macCatalyst"
221+
}
222+
213223
return checkConfiguration(at: argExpr) {
214224
(active: try body(arg), syntaxErrorsAllowed: fn.syntaxErrorsAllowed)
215225
}
@@ -304,8 +314,8 @@ func evaluateIfConfig(
304314
} else {
305315
// Complain about unknown endianness
306316
extraDiagnostics.append(
307-
contentsOf: IfConfigError.endiannessDoesNotMatch(syntax: argExpr, argument: arg)
308-
.asDiagnostics(at: argExpr)
317+
IfConfigError.endiannessDoesNotMatch(syntax: argExpr, argument: arg)
318+
.asDiagnostic
309319
)
310320

311321
isActive = false

Tests/SwiftIfConfigTest/EvaluateTests.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,22 @@ public class EvaluateTests: XCTestCase {
183183
)
184184
]
185185
)
186+
187+
assertIfConfig(
188+
"targetEnvironment(macabi)",
189+
.inactive,
190+
diagnostics: [
191+
DiagnosticSpec(
192+
message: "'macabi' has been renamed to 'macCatalyst'",
193+
line: 1,
194+
column: 19,
195+
severity: .warning,
196+
fixIts: [
197+
FixItSpec(message: "replace with 'macCatalyst'")
198+
]
199+
)
200+
]
201+
)
186202
}
187203

188204
func testVersions() throws {

0 commit comments

Comments
 (0)