Skip to content

Commit 14b6718

Browse files
committed
Format source
1 parent f004511 commit 14b6718

File tree

7 files changed

+73
-59
lines changed

7 files changed

+73
-59
lines changed

Sources/SwiftIfConfig/IfConfigEvaluation.swift

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,6 @@ extension VersionTuple {
158158
}
159159
}
160160

161-
162161
/// Evaluate the condition of an `#if`.
163162
private func evaluateIfConfig(
164163
condition: ExprSyntax,
@@ -188,14 +187,16 @@ private func evaluateIfConfig(
188187

189188
// Logical '!'.
190189
if let prefixOp = condition.as(PrefixOperatorExprSyntax.self),
191-
prefixOp.operatorToken?.text == "!" {
190+
prefixOp.operatorToken?.text == "!"
191+
{
192192
return try !evaluateIfConfig(condition: prefixOp.postfixExpression, configuration: configuration)
193193
}
194194

195195
// Logical '&&' and '||'.
196196
if let binOp = condition.as(InfixOperatorExprSyntax.self),
197-
let op = binOp.operatorOperand.as(BinaryOperatorExprSyntax.self),
198-
(op.operatorToken.text == "&&" || op.operatorToken.text == "||") {
197+
let op = binOp.operatorOperand.as(BinaryOperatorExprSyntax.self),
198+
(op.operatorToken.text == "&&" || op.operatorToken.text == "||")
199+
{
199200
// Evaluate the left-hand side.
200201
let lhsResult = try evaluateIfConfig(condition: binOp.leftOperand, configuration: configuration)
201202

@@ -212,20 +213,23 @@ private func evaluateIfConfig(
212213

213214
// Look through parentheses.
214215
if let tuple = condition.as(TupleExprSyntax.self), tuple.isParentheses,
215-
let element = tuple.elements.first {
216+
let element = tuple.elements.first
217+
{
216218
return try evaluateIfConfig(condition: element.expression, configuration: configuration)
217219
}
218220

219221
// Calls syntax is for operations.
220222
if let call = condition.as(FunctionCallExprSyntax.self),
221-
let fnName = call.calledExpression.simpleIdentifierExpr,
222-
let fn = IfConfigFunctions(rawValue: fnName) {
223+
let fnName = call.calledExpression.simpleIdentifierExpr,
224+
let fn = IfConfigFunctions(rawValue: fnName)
225+
{
223226

224227
/// Perform a check for an operation that takes a single identifier argument.
225228
func doSingleIdentifierArgumentCheck(_ body: (String, ExprSyntax) -> Bool?) throws -> Bool? {
226229
// Ensure that we have a single argument that is a simple identifier.
227230
guard let argExpr = call.argumentList.singleUnlabeledExpression,
228-
let arg = argExpr.simpleIdentifierExpr else { return nil }
231+
let arg = argExpr.simpleIdentifierExpr
232+
else { return nil }
229233

230234
guard let result = body(arg, ExprSyntax(argExpr)) else {
231235
throw IfConfigError.unhandledFunction(name: fnName, syntax: ExprSyntax(call))
@@ -239,8 +243,9 @@ private func evaluateIfConfig(
239243
// Ensure that we have a single unlabeled argument that is either >= or < as a prefix
240244
// operator applied to a version.
241245
guard let argExpr = call.argumentList.singleUnlabeledExpression,
242-
let unaryArg = argExpr.as(PrefixOperatorExprSyntax.self),
243-
let opToken = unaryArg.operatorToken else {
246+
let unaryArg = argExpr.as(PrefixOperatorExprSyntax.self),
247+
let opToken = unaryArg.operatorToken
248+
else {
244249
return nil
245250
}
246251

@@ -289,8 +294,9 @@ private func evaluateIfConfig(
289294
// Ensure that we have a single argument that is a simple identifier,
290295
// either "little" or "big".
291296
guard let argExpr = call.argumentList.singleUnlabeledExpression,
292-
let arg = argExpr.simpleIdentifierExpr,
293-
let expectedEndianness = Endianness(rawValue: arg) else {
297+
let arg = argExpr.simpleIdentifierExpr,
298+
let expectedEndianness = Endianness(rawValue: arg)
299+
else {
294300
result = nil
295301
break
296302
}
@@ -306,10 +312,11 @@ private func evaluateIfConfig(
306312
// Ensure that we have a single argument that is a simple identifier, which
307313
// is an underscore followed by an integer.
308314
guard let argExpr = call.argumentList.singleUnlabeledExpression,
309-
let arg = argExpr.simpleIdentifierExpr,
310-
let argFirst = arg.first,
311-
argFirst == "_",
312-
let expectedPointerBitWidth = Int(arg.dropFirst()) else {
315+
let arg = argExpr.simpleIdentifierExpr,
316+
let argFirst = arg.first,
317+
argFirst == "_",
318+
let expectedPointerBitWidth = Int(arg.dropFirst())
319+
else {
313320
result = nil
314321
break
315322
}
@@ -331,10 +338,11 @@ private func evaluateIfConfig(
331338
// Argument is a single unlabeled argument containing a string
332339
// literal.
333340
guard let argExpr = call.argumentList.singleUnlabeledExpression,
334-
let stringLiteral = argExpr.as(StringLiteralExprSyntax.self),
335-
stringLiteral.segments.count == 1,
336-
let segment = stringLiteral.segments.first,
337-
case .stringSegment(let stringSegment) = segment else {
341+
let stringLiteral = argExpr.as(StringLiteralExprSyntax.self),
342+
stringLiteral.segments.count == 1,
343+
let segment = stringLiteral.segments.first,
344+
case .stringSegment(let stringSegment) = segment
345+
else {
338346
// FIXME: better diagnostic here
339347
throw IfConfigError.unknownExpression(condition)
340348
}
@@ -352,7 +360,8 @@ private func evaluateIfConfig(
352360
// Retrieve the first argument, which must not have a label. This is
353361
// the module import path.
354362
guard let firstArg = call.argumentList.first,
355-
firstArg.label == nil else {
363+
firstArg.label == nil
364+
else {
356365
throw IfConfigError.canImportMissingModule(syntax: ExprSyntax(call))
357366
}
358367

@@ -363,16 +372,16 @@ private func evaluateIfConfig(
363372
// _underlyingVersion.
364373
let version: CanImportVersion
365374
if let secondArg = call.argumentList.dropFirst().first {
366-
if secondArg.label?.text != "_version" &&
367-
secondArg.label?.text != "_underlyingVersion" {
375+
if secondArg.label?.text != "_version" && secondArg.label?.text != "_underlyingVersion" {
368376
throw IfConfigError.canImportLabel(syntax: secondArg.expression)
369377
}
370378

371379
let versionText: String
372380
if let stringLiteral = secondArg.expression.as(StringLiteralExprSyntax.self),
373-
stringLiteral.segments.count == 1,
374-
let firstSegment = stringLiteral.segments.first,
375-
case .stringSegment(let stringSegment) = firstSegment {
381+
stringLiteral.segments.count == 1,
382+
let firstSegment = stringLiteral.segments.first,
383+
case .stringSegment(let stringSegment) = firstSegment
384+
{
376385
versionText = stringSegment.content.text
377386
} else {
378387
versionText = secondArg.expression.trimmedDescription
@@ -386,7 +395,7 @@ private func evaluateIfConfig(
386395

387396
if secondArg.label?.text == "_version" {
388397
version = .version(versionTuple)
389-
} else {
398+
} else {
390399
assert(secondArg.label?.text == "_underlyingVersion")
391400
version = .underlyingVersion(versionTuple)
392401
}
@@ -400,7 +409,8 @@ private func evaluateIfConfig(
400409

401410
result = configuration.canImport(
402411
importPath: importPath.map { String($0) },
403-
version: version, syntax: ExprSyntax(call)
412+
version: version,
413+
syntax: ExprSyntax(call)
404414
)
405415
}
406416

Sources/SwiftIfConfig/IfConfigRewriter.swift

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,13 @@ import SwiftSyntax
5050
///
5151
/// For any other target platforms, the resulting tree will be empty (other
5252
/// than trivia).
53-
class ActiveSyntaxRewriter<Configuration: BuildConfiguration> : SyntaxRewriter {
53+
class ActiveSyntaxRewriter<Configuration: BuildConfiguration>: SyntaxRewriter {
5454
let configuration: Configuration
55-
55+
5656
init(configuration: Configuration) {
5757
self.configuration = configuration
5858
}
59-
59+
6060
private func dropInactive<List: Collection & SyntaxCollection>(
6161
_ node: List,
6262
elementAsIfConfig: (List.Element) -> IfConfigDeclSyntax?
@@ -65,7 +65,7 @@ class ActiveSyntaxRewriter<Configuration: BuildConfiguration> : SyntaxRewriter {
6565
var anyChanged = false
6666
for elementIndex in node.indices {
6767
let element = node[elementIndex]
68-
68+
6969
// Find #ifs within the list.
7070
if let ifConfigDecl = elementAsIfConfig(element) {
7171
// If this is the first element that changed, note that we have
@@ -74,64 +74,64 @@ class ActiveSyntaxRewriter<Configuration: BuildConfiguration> : SyntaxRewriter {
7474
anyChanged = true
7575
newElements.append(contentsOf: node[..<elementIndex])
7676
}
77-
77+
7878
// FIXME: Swallowing errors
7979
guard let activeClause = try? ifConfigDecl.activeClause(in: configuration) else {
8080
continue
8181
}
82-
82+
8383
guard let elements = activeClause.elements else {
8484
continue
8585
}
86-
86+
8787
let innerElements = Syntax(elements).cast(List.self)
8888
let newInnerElements = dropInactive(innerElements, elementAsIfConfig: elementAsIfConfig)
8989
newElements.append(contentsOf: newInnerElements)
90-
90+
9191
continue
9292
}
93-
93+
9494
if anyChanged {
9595
newElements.append(element)
9696
}
9797
}
98-
98+
9999
if !anyChanged {
100100
return node
101101
}
102-
102+
103103
return List(newElements)
104104
}
105-
105+
106106
override func visit(_ node: CodeBlockItemListSyntax) -> CodeBlockItemListSyntax {
107107
let rewrittenNode = dropInactive(node) { element in
108108
guard case .decl(let declElement) = element.item else {
109109
return nil
110110
}
111-
111+
112112
return declElement.as(IfConfigDeclSyntax.self)
113113
}
114-
114+
115115
return super.visit(rewrittenNode)
116116
}
117-
117+
118118
override func visit(_ node: MemberDeclListSyntax) -> MemberDeclListSyntax {
119119
let rewrittenNode = dropInactive(node) { element in
120120
return element.decl.as(IfConfigDeclSyntax.self)
121121
}
122-
122+
123123
return super.visit(rewrittenNode)
124124
}
125-
125+
126126
override func visit(_ node: SwitchCaseListSyntax) -> SwitchCaseListSyntax {
127127
let rewrittenNode = dropInactive(node) { element in
128128
if case .ifConfigDecl(let ifConfigDecl) = element {
129129
return ifConfigDecl
130130
}
131-
131+
132132
return nil
133133
}
134-
134+
135135
return super.visit(rewrittenNode)
136136
}
137137

@@ -149,7 +149,8 @@ class ActiveSyntaxRewriter<Configuration: BuildConfiguration> : SyntaxRewriter {
149149

150150
/// Apply the given base to the postfix expression.
151151
private func applyBaseToPostfixExpression(
152-
base: ExprSyntax, postfix: ExprSyntax
152+
base: ExprSyntax,
153+
postfix: ExprSyntax
153154
) -> ExprSyntax {
154155
/// Try to apply the base to the postfix expression using the given
155156
/// keypath into a specific node type.
@@ -227,7 +228,8 @@ class ActiveSyntaxRewriter<Configuration: BuildConfiguration> : SyntaxRewriter {
227228
// Determine the active clause within this syntax node.
228229
// TODO: Swallows errors
229230
guard let activeClause = try? postfixIfConfig.config.activeClause(in: configuration),
230-
case .`postfixExpression`(let postfixExpr) = activeClause.elements else {
231+
case .`postfixExpression`(let postfixExpr) = activeClause.elements
232+
else {
231233
// If there is no active clause, return the base.
232234

233235
// Prefer the base we have and, if not, use the outer base.
@@ -264,7 +266,6 @@ class ActiveSyntaxRewriter<Configuration: BuildConfiguration> : SyntaxRewriter {
264266
}
265267
}
266268

267-
268269
extension SyntaxProtocol {
269270
/// Produce a copy of this syntax node that removes all syntax regions that
270271
/// are inactive according to the given build configuration, leaving only

Sources/SwiftIfConfig/IfConfigVisitor.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import SwiftSyntax
4040
///
4141
/// TODO: This visitor currently swallows errors uncovered while checking `#if`
4242
/// conditions, which is deeply unfortunate. We need a better answer here.
43-
open class ActiveSyntaxVisitor<Configuration: BuildConfiguration> : SyntaxVisitor {
43+
open class ActiveSyntaxVisitor<Configuration: BuildConfiguration>: SyntaxVisitor {
4444
/// The build configuration, which will be queried for each relevant `#if`.
4545
public let configuration: Configuration
4646

@@ -53,7 +53,8 @@ open class ActiveSyntaxVisitor<Configuration: BuildConfiguration> : SyntaxVisito
5353
// If there is an active clause, visit it's children.
5454
// FIXME: try? suppresses errors here. How shall we report them?
5555
if let activeClause = try? node.activeClause(in: configuration),
56-
let elements = activeClause.elements {
56+
let elements = activeClause.elements
57+
{
5758
walk(Syntax(elements))
5859
}
5960

@@ -90,7 +91,7 @@ open class ActiveSyntaxVisitor<Configuration: BuildConfiguration> : SyntaxVisito
9091
///
9192
/// TODO: This visitor currently swallows errors uncovered while checking `#if`
9293
/// conditions, which is deeply unfortunate. We need a better answer here.
93-
open class ActiveSyntaxAnyVisitor<Configuration: BuildConfiguration> : SyntaxAnyVisitor {
94+
open class ActiveSyntaxAnyVisitor<Configuration: BuildConfiguration>: SyntaxAnyVisitor {
9495
/// The build configuration, which will be queried for each relevant `#if`.
9596
public let configuration: Configuration
9697

@@ -103,7 +104,8 @@ open class ActiveSyntaxAnyVisitor<Configuration: BuildConfiguration> : SyntaxAny
103104
// If there is an active clause, visit it's children.
104105
// FIXME: try? suppresses errors here. How shall we report them?
105106
if let activeClause = try? node.activeClause(in: configuration),
106-
let elements = activeClause.elements {
107+
let elements = activeClause.elements
108+
{
107109
walk(Syntax(elements))
108110
}
109111

Sources/SwiftIfConfig/SyntaxLiteralUtils.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ extension ExprSyntax {
3737
/// Whether this is a simple identifier expression and, if so, what the identifier string is.
3838
var simpleIdentifierExpr: String? {
3939
guard let identExpr = self.as(IdentifierExprSyntax.self),
40-
identExpr.declNameArguments == nil
40+
identExpr.declNameArguments == nil
4141
else {
4242
return nil
4343
}

Sources/SwiftIfConfig/VersionTuple.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ extension VersionTuple {
4949
}
5050
}
5151

52-
extension VersionTuple: Equatable, Hashable { }
52+
extension VersionTuple: Equatable, Hashable {}
5353

5454
extension VersionTuple: Comparable {
55-
public static func <(lhs: VersionTuple, rhs: VersionTuple) -> Bool {
55+
public static func < (lhs: VersionTuple, rhs: VersionTuple) -> Bool {
5656
return lhs.normalized.components.lexicographicallyPrecedes(rhs.normalized.components)
5757
}
5858
}

Tests/SwiftIfConfigTest/TestingBuildConfiguration.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import SwiftIfConfig
1313
import SwiftSyntax
1414

15-
struct TestingBuildConfiguration : BuildConfiguration {
15+
struct TestingBuildConfiguration: BuildConfiguration {
1616
var platformName: String = "Linux"
1717
var customConditions: Set<String> = []
1818
var features: Set<String> = []

Tests/SwiftIfConfigTest/VisitorTests.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ public class VisitorTests: XCTestCase {
177177
.c
178178
.d()
179179
}
180-
""")
180+
"""
181+
)
181182
}
182183
}

0 commit comments

Comments
 (0)