Skip to content

Commit 85ab6c8

Browse files
authored
Making a Code Review (#73)
1 parent ed9de39 commit 85ab6c8

34 files changed

Lines changed: 799 additions & 217 deletions

Sources/SyntaxKit/Attribute.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ import SwiftSyntax
3131

3232
/// Internal representation of a Swift attribute with its arguments.
3333
internal struct AttributeInfo {
34-
let name: String
35-
let arguments: [String]
34+
internal let name: String
35+
internal let arguments: [String]
3636

37-
init(name: String, arguments: [String] = []) {
37+
internal init(name: String, arguments: [String] = []) {
3838
self.name = name
3939
self.arguments = arguments
4040
}

Sources/SyntaxKit/CodeBlock+Generate.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,21 @@ extension CodeBlock {
4545
item = .stmt(stmt)
4646
} else if let expr = self.syntax.as(ExprSyntax.self) {
4747
item = .expr(expr)
48+
} else if let token = self.syntax.as(TokenSyntax.self) {
49+
// Wrap TokenSyntax in DeclReferenceExprSyntax and then in ExprSyntax
50+
let expr = ExprSyntax(DeclReferenceExprSyntax(baseName: .identifier(token.text)))
51+
item = .expr(expr)
52+
} else if let switchCase = self.syntax.as(SwitchCaseSyntax.self) {
53+
// Wrap SwitchCaseSyntax in a SwitchExprSyntax and treat it as an expression
54+
// This is a fallback for when SwitchCase is used standalone
55+
let switchExpr = SwitchExprSyntax(
56+
switchKeyword: .keyword(.switch, trailingTrivia: .space),
57+
subject: ExprSyntax(DeclReferenceExprSyntax(baseName: .identifier("_"))),
58+
leftBrace: .leftBraceToken(leadingTrivia: .space, trailingTrivia: .newline),
59+
cases: SwitchCaseListSyntax([SwitchCaseListSyntax.Element(switchCase)]),
60+
rightBrace: .rightBraceToken(leadingTrivia: .newline)
61+
)
62+
item = .expr(ExprSyntax(switchExpr))
4863
} else {
4964
fatalError(
5065
"Unsupported syntax type at top level: \(type(of: self.syntax)) generating from \(self)")

Sources/SyntaxKit/CommentedCodeBlock.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ import SwiftSyntax
3333
// MARK: - Wrapper `CodeBlock` that injects leading trivia
3434

3535
internal struct CommentedCodeBlock: CodeBlock {
36-
let base: CodeBlock
37-
let lines: [Line]
36+
internal let base: CodeBlock
37+
internal let lines: [Line]
3838

39-
var syntax: SyntaxProtocol {
39+
internal var syntax: SyntaxProtocol {
4040
// Shortcut if there are no comment lines
4141
guard !lines.isEmpty else { return base.syntax }
4242

Sources/SyntaxKit/Enum.swift

Lines changed: 55 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -145,56 +145,84 @@ public struct Enum: CodeBlock {
145145
/// A Swift `case` declaration inside an `enum`.
146146
public struct EnumCase: CodeBlock {
147147
private let name: String
148-
private var value: String?
149-
private var intValue: Int?
148+
private var literalValue: Literal?
150149

151150
/// Creates a `case` declaration.
152151
/// - Parameter name: The name of the case.
153152
public init(_ name: String) {
154153
self.name = name
154+
self.literalValue = nil
155155
}
156156

157-
/// Sets the raw value of the case to a string.
158-
/// - Parameter value: The string value.
157+
/// Sets the raw value of the case to a Literal.
158+
/// - Parameter value: The literal value.
159159
/// - Returns: A copy of the case with the raw value set.
160-
public func equals(_ value: String) -> Self {
160+
public func equals(_ value: Literal) -> Self {
161161
var copy = self
162-
copy.value = value
163-
copy.intValue = nil
162+
copy.literalValue = value
164163
return copy
165164
}
166165

167-
/// Sets the raw value of the case to an integer.
166+
/// Sets the raw value of the case to a string (for backward compatibility).
167+
/// - Parameter value: The string value.
168+
/// - Returns: A copy of the case with the raw value set.
169+
public func equals(_ value: String) -> Self {
170+
self.equals(.string(value))
171+
}
172+
173+
/// Sets the raw value of the case to an integer (for backward compatibility).
168174
/// - Parameter value: The integer value.
169175
/// - Returns: A copy of the case with the raw value set.
170176
public func equals(_ value: Int) -> Self {
171-
var copy = self
172-
copy.value = nil
173-
copy.intValue = value
174-
return copy
177+
self.equals(.integer(value))
178+
}
179+
180+
/// Sets the raw value of the case to a float (for backward compatibility).
181+
/// - Parameter value: The float value.
182+
/// - Returns: A copy of the case with the raw value set.
183+
public func equals(_ value: Double) -> Self {
184+
self.equals(.float(value))
175185
}
176186

177187
public var syntax: SyntaxProtocol {
178188
let caseKeyword = TokenSyntax.keyword(.case, trailingTrivia: .space)
179189
let identifier = TokenSyntax.identifier(name, trailingTrivia: .space)
180190

181191
var initializer: InitializerClauseSyntax?
182-
if let value = value {
183-
initializer = InitializerClauseSyntax(
184-
equal: .equalToken(leadingTrivia: .space, trailingTrivia: .space),
185-
value: StringLiteralExprSyntax(
186-
openingQuote: .stringQuoteToken(),
187-
segments: StringLiteralSegmentListSyntax([
188-
.stringSegment(StringSegmentSyntax(content: .stringSegment(value)))
189-
]),
190-
closingQuote: .stringQuoteToken()
192+
if let literal = literalValue {
193+
switch literal {
194+
case .string(let value):
195+
initializer = InitializerClauseSyntax(
196+
equal: .equalToken(leadingTrivia: .space, trailingTrivia: .space),
197+
value: StringLiteralExprSyntax(
198+
openingQuote: .stringQuoteToken(),
199+
segments: StringLiteralSegmentListSyntax([
200+
.stringSegment(StringSegmentSyntax(content: .stringSegment(value)))
201+
]),
202+
closingQuote: .stringQuoteToken()
203+
)
191204
)
192-
)
193-
} else if let intValue = intValue {
194-
initializer = InitializerClauseSyntax(
195-
equal: .equalToken(leadingTrivia: .space, trailingTrivia: .space),
196-
value: IntegerLiteralExprSyntax(digits: .integerLiteral(String(intValue)))
197-
)
205+
case .float(let value):
206+
initializer = InitializerClauseSyntax(
207+
equal: .equalToken(leadingTrivia: .space, trailingTrivia: .space),
208+
value: FloatLiteralExprSyntax(literal: .floatLiteral(String(value)))
209+
)
210+
case .integer(let value):
211+
initializer = InitializerClauseSyntax(
212+
equal: .equalToken(leadingTrivia: .space, trailingTrivia: .space),
213+
value: IntegerLiteralExprSyntax(digits: .integerLiteral(String(value)))
214+
)
215+
case .nil:
216+
initializer = InitializerClauseSyntax(
217+
equal: .equalToken(leadingTrivia: .space, trailingTrivia: .space),
218+
value: NilLiteralExprSyntax(nilKeyword: .keyword(.nil))
219+
)
220+
case .boolean(let value):
221+
initializer = InitializerClauseSyntax(
222+
equal: .equalToken(leadingTrivia: .space, trailingTrivia: .space),
223+
value: BooleanLiteralExprSyntax(literal: value ? .keyword(.true) : .keyword(.false))
224+
)
225+
}
198226
}
199227

200228
return EnumCaseDeclSyntax(

Sources/SyntaxKit/Group.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import SwiftSyntax
3131

3232
/// A group of code blocks.
3333
public struct Group: CodeBlock {
34-
let members: [CodeBlock]
34+
internal let members: [CodeBlock]
3535

3636
/// Creates a group of code blocks.
3737
/// - Parameter content: A ``CodeBlockBuilder`` that provides the members of the group.

Sources/SyntaxKit/Let.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ import SwiftSyntax
3131

3232
/// A Swift `let` declaration for use in an `if` statement.
3333
public struct Let: CodeBlock {
34-
let name: String
35-
let value: String
34+
internal let name: String
35+
internal let value: String
3636

3737
/// Creates a `let` declaration for an `if` statement.
3838
/// - Parameters:

Sources/SyntaxKit/Parameter.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ import SwiftSyntax
3333

3434
/// A parameter for a function or initializer.
3535
public struct Parameter: CodeBlock {
36-
let name: String
37-
let type: String
38-
let defaultValue: String?
39-
let isUnnamed: Bool
36+
internal let name: String
37+
internal let type: String
38+
internal let defaultValue: String?
39+
internal let isUnnamed: Bool
4040
internal var attributes: [AttributeInfo] = []
4141

4242
/// Creates a parameter for a function or initializer.

Sources/SyntaxKit/ParameterExp.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ import SwiftSyntax
3131

3232
/// A parameter for a function call.
3333
public struct ParameterExp: CodeBlock {
34-
let name: String
35-
let value: String
34+
internal let name: String
35+
internal let value: String
3636

3737
/// Creates a parameter for a function call.
3838
/// - Parameters:
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//
2+
// Parenthesized.swift
3+
// SyntaxKit
4+
//
5+
// Created by Leo Dion.
6+
// Copyright © 2025 BrightDigit.
7+
//
8+
// Permission is hereby granted, free of charge, to any person
9+
// obtaining a copy of this software and associated documentation
10+
// files (the “Software”), to deal in the Software without
11+
// restriction, including without limitation the rights to use,
12+
// copy, modify, merge, publish, distribute, sublicense, and/or
13+
// sell copies of the Software, and to permit persons to whom the
14+
// Software is furnished to do so, subject to the following
15+
// conditions:
16+
//
17+
// The above copyright notice and this permission notice shall be
18+
// included in all copies or substantial portions of the Software.
19+
//
20+
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
21+
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
22+
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23+
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
24+
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
25+
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26+
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
27+
// OTHER DEALINGS IN THE SOFTWARE.
28+
//
29+
30+
import SwiftSyntax
31+
32+
/// A code block that wraps its content in parentheses.
33+
public struct Parenthesized: CodeBlock {
34+
private let content: CodeBlock
35+
36+
/// Creates a parenthesized code block.
37+
/// - Parameter content: The code block to wrap in parentheses.
38+
public init(@CodeBlockBuilderResult _ content: () -> [CodeBlock]) {
39+
let blocks = content()
40+
precondition(blocks.count == 1, "Parenthesized expects exactly one code block.")
41+
self.content = blocks[0]
42+
}
43+
44+
public var syntax: SyntaxProtocol {
45+
ExprSyntax(
46+
TupleExprSyntax(
47+
leftParen: .leftParenToken(),
48+
elements: LabeledExprListSyntax([
49+
LabeledExprSyntax(expression: content.expr)
50+
]),
51+
rightParen: .rightParenToken()
52+
)
53+
)
54+
}
55+
}

Sources/SyntaxKit/VariableExp.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import SwiftSyntax
3131

3232
/// An expression that refers to a variable.
3333
public struct VariableExp: CodeBlock {
34-
let name: String
34+
internal let name: String
3535

3636
/// Creates a variable expression.
3737
/// - Parameter name: The name of the variable.
@@ -71,8 +71,8 @@ public struct VariableExp: CodeBlock {
7171

7272
/// An expression that accesses a property on a base expression.
7373
public struct PropertyAccessExp: CodeBlock {
74-
let baseName: String
75-
let propertyName: String
74+
internal let baseName: String
75+
internal let propertyName: String
7676

7777
/// Creates a property access expression.
7878
/// - Parameters:
@@ -97,9 +97,9 @@ public struct PropertyAccessExp: CodeBlock {
9797

9898
/// An expression that calls a function.
9999
public struct FunctionCallExp: CodeBlock {
100-
let baseName: String
101-
let methodName: String
102-
let parameters: [ParameterExp]
100+
internal let baseName: String
101+
internal let methodName: String
102+
internal let parameters: [ParameterExp]
103103

104104
/// Creates a function call expression.
105105
/// - Parameters:

0 commit comments

Comments
 (0)