Skip to content

Commit d462ce7

Browse files
committed
fixing tests and linting
1 parent 294e5ec commit d462ce7

File tree

7 files changed

+64
-46
lines changed

7 files changed

+64
-46
lines changed

Sources/SyntaxKit/Catch.swift

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//
88
// Permission is hereby granted, free of charge, to any person
99
// obtaining a copy of this software and associated documentation
10-
// files (the "Software"), to deal in the Software without
10+
// files (the Software), to deal in the Software without
1111
// restriction, including without limitation the rights to use,
1212
// copy, modify, merge, publish, distribute, sublicense, and/or
1313
// sell copies of the Software, and to permit persons to whom the
@@ -17,7 +17,7 @@
1717
// The above copyright notice and this permission notice shall be
1818
// included in all copies or substantial portions of the Software.
1919
//
20-
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20+
// THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND,
2121
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
2222
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
2323
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
@@ -69,17 +69,19 @@ public struct Catch: CodeBlock {
6969
var catchItems: CatchItemListSyntax?
7070
if let pattern = pattern {
7171
let patternSyntax: PatternSyntax
72-
72+
7373
if let enumCase = pattern as? EnumCase {
7474
if let associated = enumCase.caseAssociatedValue {
7575
// Handle EnumCase with associated value
7676
// Split the case name into type and case if needed
7777
let baseName = enumCase.caseName
7878
let baseParts = baseName.split(separator: ".")
79-
let (typeName, caseName) = baseParts.count == 2 ? (String(baseParts[0]), String(baseParts[1])) : ("", baseName)
79+
let (typeName, caseName) =
80+
baseParts.count == 2 ? (String(baseParts[0]), String(baseParts[1])) : ("", baseName)
8081
// Build the pattern: Type.caseName(let associatedName)
8182
let memberAccess = MemberAccessExprSyntax(
82-
base: typeName.isEmpty ? nil : ExprSyntax(DeclReferenceExprSyntax(baseName: .identifier(typeName))),
83+
base: typeName.isEmpty
84+
? nil : ExprSyntax(DeclReferenceExprSyntax(baseName: .identifier(typeName))),
8385
dot: .periodToken(),
8486
name: .identifier(caseName)
8587
)
@@ -91,7 +93,8 @@ public struct Catch: CodeBlock {
9193
pattern: PatternSyntax(
9294
ValueBindingPatternSyntax(
9395
bindingSpecifier: .keyword(.let, trailingTrivia: .space),
94-
pattern: PatternSyntax(IdentifierPatternSyntax(identifier: .identifier(associated.name)))
96+
pattern: PatternSyntax(
97+
IdentifierPatternSyntax(identifier: .identifier(associated.name)))
9598
)
9699
),
97100
trailingComma: nil
@@ -103,7 +106,8 @@ public struct Catch: CodeBlock {
103106
let enumPattern = ExpressionPatternSyntax(
104107
expression: ExprSyntax(
105108
MemberAccessExprSyntax(
106-
base: typeName.isEmpty ? nil : ExprSyntax(DeclReferenceExprSyntax(baseName: .identifier(typeName))),
109+
base: typeName.isEmpty
110+
? nil : ExprSyntax(DeclReferenceExprSyntax(baseName: .identifier(typeName))),
107111
dot: .periodToken(),
108112
name: .identifier(caseName)
109113
)
@@ -118,7 +122,8 @@ public struct Catch: CodeBlock {
118122
pattern: PatternSyntax(
119123
ValueBindingPatternSyntax(
120124
bindingSpecifier: .keyword(.let, trailingTrivia: .space),
121-
pattern: PatternSyntax(IdentifierPatternSyntax(identifier: .identifier(associated.name)))
125+
pattern: PatternSyntax(
126+
IdentifierPatternSyntax(identifier: .identifier(associated.name)))
122127
)
123128
),
124129
trailingComma: nil
@@ -143,7 +148,8 @@ public struct Catch: CodeBlock {
143148
pattern: PatternSyntax(
144149
ValueBindingPatternSyntax(
145150
bindingSpecifier: .keyword(.let, trailingTrivia: .space),
146-
pattern: PatternSyntax(IdentifierPatternSyntax(identifier: .identifier(associated.name)))
151+
pattern: PatternSyntax(
152+
IdentifierPatternSyntax(identifier: .identifier(associated.name)))
147153
)
148154
)
149155
)
@@ -174,7 +180,7 @@ public struct Catch: CodeBlock {
174180
)
175181
)
176182
}
177-
183+
178184
catchItems = CatchItemListSyntax([
179185
CatchItemSyntax(pattern: patternSyntax)
180186
])
@@ -208,4 +214,4 @@ public struct Catch: CodeBlock {
208214
public var syntax: SyntaxProtocol {
209215
catchClauseSyntax
210216
}
211-
}
217+
}

Sources/SyntaxKit/CatchBuilder.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//
88
// Permission is hereby granted, free of charge, to any person
99
// obtaining a copy of this software and associated documentation
10-
// files (the "Software"), to deal in the Software without
10+
// files (the Software), to deal in the Software without
1111
// restriction, including without limitation the rights to use,
1212
// copy, modify, merge, publish, distribute, sublicense, and/or
1313
// sell copies of the Software, and to permit persons to whom the
@@ -17,7 +17,7 @@
1717
// The above copyright notice and this permission notice shall be
1818
// included in all copies or substantial portions of the Software.
1919
//
20-
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20+
// THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND,
2121
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
2222
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
2323
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
@@ -59,4 +59,4 @@ public struct CatchBuilder {
5959
public static func buildArray(_ components: [CatchClauseSyntax]) -> CatchClauseListSyntax {
6060
CatchClauseListSyntax(components)
6161
}
62-
}
62+
}

Sources/SyntaxKit/Do.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//
88
// Permission is hereby granted, free of charge, to any person
99
// obtaining a copy of this software and associated documentation
10-
// files (the "Software"), to deal in the Software without
10+
// files (the Software), to deal in the Software without
1111
// restriction, including without limitation the rights to use,
1212
// copy, modify, merge, publish, distribute, sublicense, and/or
1313
// sell copies of the Software, and to permit persons to whom the
@@ -17,7 +17,7 @@
1717
// The above copyright notice and this permission notice shall be
1818
// included in all copies or substantial portions of the Software.
1919
//
20-
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20+
// THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND,
2121
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
2222
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
2323
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
@@ -73,4 +73,4 @@ public struct Do: CodeBlock {
7373
)
7474
)
7575
}
76-
}
76+
}

Sources/SyntaxKit/Init.swift

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ public struct Init: CodeBlock, ExprCodeBlock, LiteralValue {
3434
private let type: String
3535
private let parameters: [ParameterExp]
3636

37+
/// Creates an initializer expression with no parameters.
38+
/// - Parameter type: The type to initialize.
39+
public init(_ type: String) {
40+
self.type = type
41+
self.parameters = []
42+
}
43+
3744
/// Creates an initializer expression.
3845
/// - Parameters:
3946
/// - type: The type to initialize.
@@ -66,13 +73,13 @@ public struct Init: CodeBlock, ExprCodeBlock, LiteralValue {
6673
public var syntax: SyntaxProtocol {
6774
exprSyntax
6875
}
69-
76+
7077
// MARK: - LiteralValue Conformance
71-
78+
7279
public var typeName: String {
7380
type
7481
}
75-
82+
7683
public var literalString: String {
7784
"\(type)()"
7885
}

Sources/SyntaxKit/Variable+TypedInitializers.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ extension Variable {
4545
self.init(
4646
kind: kind,
4747
name: name,
48-
type: nil, // Will be inferred from Init
48+
type: nil, // Will be inferred from Init
4949
defaultValue: defaultValue,
5050
explicitType: explicitType ?? false
5151
)

Sources/SyntaxKit/Variable.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public struct Variable: CodeBlock {
5656
) {
5757
self.kind = kind
5858
self.name = name
59-
59+
6060
// If type is provided, use it; otherwise try to infer from defaultValue
6161
if let providedType = type {
6262
self.type = providedType
@@ -65,7 +65,7 @@ public struct Variable: CodeBlock {
6565
} else {
6666
self.type = ""
6767
}
68-
68+
6969
self.defaultValue = defaultValue
7070
self.explicitType = explicitType
7171
}

Tests/SyntaxKitTests/Unit/ErrorHandlingTests.swift

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import Testing
66
@Test("Error handling DSL generates expected Swift code")
77
internal func testErrorHandlingExample() throws {
88
let errorHandlingExample = Group {
9-
Variable(.var, name: "vendingMachine", equals: Init("VendingMachine") {})
9+
Variable(.var, name: "vendingMachine", equals: Init("VendingMachine"))
1010
Assignment("vendingMachine.coinsDeposited", Literal.integer(8))
11-
Do{
12-
Call("buyFavoriteSnack"){
11+
Do {
12+
Call("buyFavoriteSnack") {
1313
ParameterExp(name: "person", value: Literal.string("Alice"))
1414
ParameterExp(name: "vendingMachine", value: Literal.ref("vendingMachine"))
1515
}.throwing()
@@ -27,9 +27,14 @@ import Testing
2727
ParameterExp(unlabeled: Literal.string("Out of Stock."))
2828
}
2929
}
30-
Catch(EnumCase("VendingMachineError.insufficientFunds").associatedValue("coinsNeeded", type: "Int")) {
30+
Catch(
31+
EnumCase("VendingMachineError.insufficientFunds").associatedValue(
32+
"coinsNeeded", type: "Int")
33+
) {
3134
Call("print") {
32-
ParameterExp(unlabeled: Literal.string("Insufficient funds. Please insert an additional \\(coinsNeeded) coins."))
35+
ParameterExp(
36+
unlabeled: Literal.string(
37+
"Insufficient funds. Please insert an additional \\(coinsNeeded) coins."))
3338
}
3439
}
3540
Catch {
@@ -42,25 +47,25 @@ import Testing
4247

4348
let generated = errorHandlingExample.generateCode()
4449
let expected = """
45-
var vendingMachine = VendingMachine()
46-
vendingMachine.coinsDeposited = 8
47-
do {
48-
try buyFavoriteSnack(person: "Alice", vendingMachine: vendingMachine)
49-
print("Success! Yum.")
50-
} catch VendingMachineError.invalidSelection {
51-
print("Invalid Selection.")
52-
} catch VendingMachineError.outOfStock {
53-
print("Out of Stock.")
54-
} catch VendingMachineError.insufficientFunds(let coinsNeeded) {
55-
print("Insufficient funds. Please insert an additional \\(coinsNeeded) coins.")
56-
} catch {
57-
print("Unexpected error: \\(error).")
58-
}
59-
"""
60-
50+
var vendingMachine = VendingMachine()
51+
vendingMachine.coinsDeposited = 8
52+
do {
53+
try buyFavoriteSnack(person: "Alice", vendingMachine: vendingMachine)
54+
print("Success! Yum.")
55+
} catch VendingMachineError.invalidSelection {
56+
print("Invalid Selection.")
57+
} catch VendingMachineError.outOfStock {
58+
print("Out of Stock.")
59+
} catch VendingMachineError.insufficientFunds(let coinsNeeded) {
60+
print("Insufficient funds. Please insert an additional \\(coinsNeeded) coins.")
61+
} catch {
62+
print("Unexpected error: \\(error).")
63+
}
64+
"""
65+
6166
#expect(generated.normalize() == expected.normalize())
62-
67+
6368
print("Generated code:")
6469
print(generated)
6570
}
66-
}
71+
}

0 commit comments

Comments
 (0)