Skip to content

Commit e5467b0

Browse files
leogdionclaude
andcommitted
fix: resolve type_contents_order violations in ControlFlow files
Move computed properties before initializers to comply with SwiftLint's type_contents_order rule in Guard.swift and While.swift. All tests pass and functionality is preserved. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent d8876fe commit e5467b0

File tree

3 files changed

+98
-99
lines changed

3 files changed

+98
-99
lines changed

Sources/SyntaxKit/ControlFlow/Guard.swift

Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -34,56 +34,6 @@ public struct Guard: CodeBlock, Sendable {
3434
private let conditions: [any CodeBlock]
3535
private let elseBody: [any CodeBlock]
3636

37-
/// Creates a `guard` statement.
38-
/// - Parameters:
39-
/// - condition: A ``CodeBlockBuilder`` that provides the condition expression.
40-
/// - elseBody: A ``CodeBlockBuilder`` that provides the body when the condition is false.
41-
public init(
42-
@CodeBlockBuilderResult _ condition: () throws -> [any CodeBlock],
43-
@CodeBlockBuilderResult else elseBody: () throws -> [any CodeBlock]
44-
) rethrows {
45-
let allConditions = try condition()
46-
if allConditions.isEmpty {
47-
// Use true as default condition when no conditions are provided
48-
self.conditions = [Literal.boolean(true)]
49-
} else {
50-
self.conditions = allConditions
51-
}
52-
self.elseBody = try elseBody()
53-
}
54-
55-
/// Creates a `guard` statement without a condition (uses true as default).
56-
/// - Parameters:
57-
/// - elseBody: A ``CodeBlockBuilder`` that provides the body when the condition is false.
58-
public init(
59-
@CodeBlockBuilderResult else elseBody: () throws -> [any CodeBlock]
60-
) rethrows {
61-
try self.init(
62-
[any CodeBlock].init,
63-
else: elseBody
64-
)
65-
}
66-
67-
/// Creates a `guard` statement with a string condition.
68-
/// - Parameters:
69-
/// - condition: The condition as a string.
70-
/// - elseBody: A ``CodeBlockBuilder`` that provides the body when the condition is false.
71-
public init(
72-
_ condition: String,
73-
@CodeBlockBuilderResult else elseBody: () throws -> [any CodeBlock]
74-
) rethrows {
75-
self.conditions = [VariableExp(condition)]
76-
self.elseBody = try elseBody()
77-
}
78-
79-
/// Convenience initializer that accepts a single condition ``CodeBlock``.
80-
public init(
81-
_ condition: any CodeBlock,
82-
@CodeBlockBuilderResult else elseBody: () throws -> [any CodeBlock]
83-
) rethrows {
84-
try self.init({ condition }, else: elseBody)
85-
}
86-
8737
public var syntax: any SyntaxProtocol {
8838
// MARK: Build conditions list (mirror implementation from `If`)
8939
let condList = ConditionElementListSyntax(
@@ -165,4 +115,54 @@ public struct Guard: CodeBlock, Sendable {
165115
)
166116
)
167117
}
118+
119+
/// Creates a `guard` statement.
120+
/// - Parameters:
121+
/// - condition: A ``CodeBlockBuilder`` that provides the condition expression.
122+
/// - elseBody: A ``CodeBlockBuilder`` that provides the body when the condition is false.
123+
public init(
124+
@CodeBlockBuilderResult _ condition: () throws -> [any CodeBlock],
125+
@CodeBlockBuilderResult else elseBody: () throws -> [any CodeBlock]
126+
) rethrows {
127+
let allConditions = try condition()
128+
if allConditions.isEmpty {
129+
// Use true as default condition when no conditions are provided
130+
self.conditions = [Literal.boolean(true)]
131+
} else {
132+
self.conditions = allConditions
133+
}
134+
self.elseBody = try elseBody()
135+
}
136+
137+
/// Creates a `guard` statement without a condition (uses true as default).
138+
/// - Parameters:
139+
/// - elseBody: A ``CodeBlockBuilder`` that provides the body when the condition is false.
140+
public init(
141+
@CodeBlockBuilderResult else elseBody: () throws -> [any CodeBlock]
142+
) rethrows {
143+
try self.init(
144+
[any CodeBlock].init,
145+
else: elseBody
146+
)
147+
}
148+
149+
/// Creates a `guard` statement with a string condition.
150+
/// - Parameters:
151+
/// - condition: The condition as a string.
152+
/// - elseBody: A ``CodeBlockBuilder`` that provides the body when the condition is false.
153+
public init(
154+
_ condition: String,
155+
@CodeBlockBuilderResult else elseBody: () throws -> [any CodeBlock]
156+
) rethrows {
157+
self.conditions = [VariableExp(condition)]
158+
self.elseBody = try elseBody()
159+
}
160+
161+
/// Convenience initializer that accepts a single condition ``CodeBlock``.
162+
public init(
163+
_ condition: any CodeBlock,
164+
@CodeBlockBuilderResult else elseBody: () throws -> [any CodeBlock]
165+
) rethrows {
166+
try self.init({ condition }, else: elseBody)
167+
}
168168
}

Sources/SyntaxKit/ControlFlow/While.swift

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,54 @@ public struct While: CodeBlock, Sendable {
4040
private let body: [any CodeBlock]
4141
private let kind: Kind
4242

43+
public var syntax: any SyntaxProtocol {
44+
let conditionExpr = condition.exprSyntax
45+
46+
let bodyBlock = CodeBlockSyntax(
47+
leftBrace: .leftBraceToken(leadingTrivia: .space, trailingTrivia: .newline),
48+
statements: CodeBlockItemListSyntax(
49+
body.compactMap {
50+
var item: CodeBlockItemSyntax?
51+
if let decl = $0.syntax.as(DeclSyntax.self) {
52+
item = CodeBlockItemSyntax(item: .decl(decl))
53+
} else if let expr = $0.syntax.as(ExprSyntax.self) {
54+
item = CodeBlockItemSyntax(item: .expr(expr))
55+
} else if let stmt = $0.syntax.as(StmtSyntax.self) {
56+
item = CodeBlockItemSyntax(item: .stmt(stmt))
57+
}
58+
return item?.with(\.trailingTrivia, .newline)
59+
}
60+
),
61+
rightBrace: .rightBraceToken(leadingTrivia: .newline)
62+
)
63+
64+
switch kind {
65+
case .repeatWhile:
66+
return StmtSyntax(
67+
RepeatWhileStmtSyntax(
68+
repeatKeyword: .keyword(.repeat, trailingTrivia: .space),
69+
body: bodyBlock,
70+
whileKeyword: .keyword(.while, trailingTrivia: .space),
71+
condition: conditionExpr
72+
)
73+
)
74+
case .while:
75+
return StmtSyntax(
76+
WhileStmtSyntax(
77+
whileKeyword: .keyword(.while, trailingTrivia: .space),
78+
conditions: ConditionElementListSyntax(
79+
[
80+
ConditionElementSyntax(
81+
condition: .expression(conditionExpr)
82+
)
83+
]
84+
),
85+
body: bodyBlock
86+
)
87+
)
88+
}
89+
}
90+
4391
/// Creates a `while` loop statement with an expression condition.
4492
/// - Parameters:
4593
/// - condition: The condition expression that conforms to ExprCodeBlock.
@@ -109,52 +157,4 @@ public struct While: CodeBlock, Sendable {
109157
self.body = try then()
110158
self.kind = kind
111159
}
112-
113-
public var syntax: any SyntaxProtocol {
114-
let conditionExpr = condition.exprSyntax
115-
116-
let bodyBlock = CodeBlockSyntax(
117-
leftBrace: .leftBraceToken(leadingTrivia: .space, trailingTrivia: .newline),
118-
statements: CodeBlockItemListSyntax(
119-
body.compactMap {
120-
var item: CodeBlockItemSyntax?
121-
if let decl = $0.syntax.as(DeclSyntax.self) {
122-
item = CodeBlockItemSyntax(item: .decl(decl))
123-
} else if let expr = $0.syntax.as(ExprSyntax.self) {
124-
item = CodeBlockItemSyntax(item: .expr(expr))
125-
} else if let stmt = $0.syntax.as(StmtSyntax.self) {
126-
item = CodeBlockItemSyntax(item: .stmt(stmt))
127-
}
128-
return item?.with(\.trailingTrivia, .newline)
129-
}
130-
),
131-
rightBrace: .rightBraceToken(leadingTrivia: .newline)
132-
)
133-
134-
switch kind {
135-
case .repeatWhile:
136-
return StmtSyntax(
137-
RepeatWhileStmtSyntax(
138-
repeatKeyword: .keyword(.repeat, trailingTrivia: .space),
139-
body: bodyBlock,
140-
whileKeyword: .keyword(.while, trailingTrivia: .space),
141-
condition: conditionExpr
142-
)
143-
)
144-
case .while:
145-
return StmtSyntax(
146-
WhileStmtSyntax(
147-
whileKeyword: .keyword(.while, trailingTrivia: .space),
148-
conditions: ConditionElementListSyntax(
149-
[
150-
ConditionElementSyntax(
151-
condition: .expression(conditionExpr)
152-
)
153-
]
154-
),
155-
body: bodyBlock
156-
)
157-
)
158-
}
159-
}
160160
}

Tests/SyntaxDocTests/DocumentationTestHarness.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import SwiftParser
33
import SwiftSyntax
44
import Testing
55

6-
76
/// Harness for extracting and testing documentation code examples
87
internal class DocumentationTestHarness {
98
/// Validates all code examples in all documentation files

0 commit comments

Comments
 (0)