Skip to content

Commit 0b89af7

Browse files
committed
adding more tests
1 parent 5d53521 commit 0b89af7

25 files changed

+1887
-1734
lines changed

Sources/SyntaxKit/EnumCase.swift

Lines changed: 8 additions & 8 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
@@ -130,6 +130,12 @@ public struct EnumCase: CodeBlock {
130130
}
131131
}
132132

133+
/// Returns the expression syntax for this enum case.
134+
/// This is the preferred method when using EnumCase in expression contexts.
135+
public var exprSyntax: ExprSyntax {
136+
asExpressionSyntax
137+
}
138+
133139
/// Returns the appropriate syntax based on context.
134140
/// When used in expressions (throw, return, if bodies), returns expression syntax.
135141
/// When used in declarations (enum cases), returns declaration syntax.
@@ -230,10 +236,4 @@ public struct EnumCase: CodeBlock {
230236
])
231237
)
232238
}
233-
234-
/// Returns the expression syntax for this enum case.
235-
/// This is the preferred method when using EnumCase in expression contexts.
236-
public var exprSyntax: ExprSyntax {
237-
asExpressionSyntax
238-
}
239239
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
//
2+
// Function+EffectSpecifiers.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+
extension Function {
33+
/// Builds the effect specifiers (async / throws) for the function.
34+
internal func buildEffectSpecifiers() -> FunctionEffectSpecifiersSyntax? {
35+
switch effect {
36+
case .none:
37+
return nil
38+
case .throws(let isRethrows, let errorType):
39+
let throwsSpecifier: TokenSyntax
40+
if let errorType = errorType {
41+
throwsSpecifier = .keyword(
42+
isRethrows ? .rethrows : .throws, leadingTrivia: .space)
43+
return FunctionEffectSpecifiersSyntax(
44+
asyncSpecifier: nil,
45+
throwsClause: ThrowsClauseSyntax(
46+
throwsSpecifier: throwsSpecifier,
47+
leftParen: .leftParenToken(),
48+
type: IdentifierTypeSyntax(name: .identifier(errorType)),
49+
rightParen: .rightParenToken()
50+
)
51+
)
52+
} else {
53+
throwsSpecifier = .keyword(
54+
isRethrows ? .rethrows : .throws, leadingTrivia: .space)
55+
return FunctionEffectSpecifiersSyntax(
56+
asyncSpecifier: nil,
57+
throwsSpecifier: throwsSpecifier
58+
)
59+
}
60+
case .async:
61+
return FunctionEffectSpecifiersSyntax(
62+
asyncSpecifier: .keyword(.async, leadingTrivia: .space, trailingTrivia: .space),
63+
throwsSpecifier: nil
64+
)
65+
case .asyncThrows(let isRethrows, let errorType):
66+
let throwsSpecifier: TokenSyntax
67+
if let errorType = errorType {
68+
throwsSpecifier = .keyword(.throws, leadingTrivia: .space)
69+
return FunctionEffectSpecifiersSyntax(
70+
asyncSpecifier: .keyword(.async, leadingTrivia: .space, trailingTrivia: .space),
71+
throwsClause: ThrowsClauseSyntax(
72+
throwsSpecifier: throwsSpecifier,
73+
leftParen: .leftParenToken(),
74+
type: IdentifierTypeSyntax(name: .identifier(errorType)),
75+
rightParen: .rightParenToken()
76+
)
77+
)
78+
} else {
79+
throwsSpecifier = .keyword(
80+
isRethrows ? .rethrows : .throws, leadingTrivia: .space)
81+
return FunctionEffectSpecifiersSyntax(
82+
asyncSpecifier: .keyword(.async, leadingTrivia: .space, trailingTrivia: .space),
83+
throwsSpecifier: throwsSpecifier
84+
)
85+
}
86+
}
87+
}
88+
}

Sources/SyntaxKit/Function+Syntax.swift

Lines changed: 1 addition & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -110,60 +110,7 @@ extension Function {
110110
)
111111

112112
// Build effect specifiers (async / throws)
113-
let effectSpecifiers: FunctionEffectSpecifiersSyntax? = {
114-
switch effect {
115-
case .none:
116-
return nil
117-
case .throws(let isRethrows, let errorType):
118-
let throwsSpecifier: TokenSyntax
119-
if let errorType = errorType {
120-
throwsSpecifier = .keyword(
121-
isRethrows ? .rethrows : .throws, leadingTrivia: .space)
122-
return FunctionEffectSpecifiersSyntax(
123-
asyncSpecifier: nil,
124-
throwsClause: ThrowsClauseSyntax(
125-
throwsSpecifier: throwsSpecifier,
126-
leftParen: .leftParenToken(),
127-
type: IdentifierTypeSyntax(name: .identifier(errorType)),
128-
rightParen: .rightParenToken()
129-
)
130-
)
131-
} else {
132-
throwsSpecifier = .keyword(
133-
isRethrows ? .rethrows : .throws, leadingTrivia: .space)
134-
return FunctionEffectSpecifiersSyntax(
135-
asyncSpecifier: nil,
136-
throwsSpecifier: throwsSpecifier
137-
)
138-
}
139-
case .async:
140-
return FunctionEffectSpecifiersSyntax(
141-
asyncSpecifier: .keyword(.async, leadingTrivia: .space, trailingTrivia: .space),
142-
throwsSpecifier: nil
143-
)
144-
case .asyncThrows(let isRethrows, let errorType):
145-
let throwsSpecifier: TokenSyntax
146-
if let errorType = errorType {
147-
throwsSpecifier = .keyword(.throws, leadingTrivia: .space)
148-
return FunctionEffectSpecifiersSyntax(
149-
asyncSpecifier: .keyword(.async, leadingTrivia: .space, trailingTrivia: .space),
150-
throwsClause: ThrowsClauseSyntax(
151-
throwsSpecifier: throwsSpecifier,
152-
leftParen: .leftParenToken(),
153-
type: IdentifierTypeSyntax(name: .identifier(errorType)),
154-
rightParen: .rightParenToken()
155-
)
156-
)
157-
} else {
158-
throwsSpecifier = .keyword(
159-
isRethrows ? .rethrows : .throws, leadingTrivia: .space)
160-
return FunctionEffectSpecifiersSyntax(
161-
asyncSpecifier: .keyword(.async, leadingTrivia: .space, trailingTrivia: .space),
162-
throwsSpecifier: throwsSpecifier
163-
)
164-
}
165-
}
166-
}()
113+
let effectSpecifiers = buildEffectSpecifiers()
167114

168115
// Build modifiers
169116
var modifiers: DeclModifierListSyntax = []
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
//
2+
// TupleAssignment+AsyncSet.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+
extension TupleAssignment {
33+
internal enum AsyncSet {
34+
static func tuplePattern(elements: [String]) -> PatternSyntax {
35+
let patternElements = TuplePatternElementListSyntax(
36+
elements.enumerated().map { index, element in
37+
TuplePatternElementSyntax(
38+
label: nil,
39+
colon: nil,
40+
pattern: PatternSyntax(IdentifierPatternSyntax(identifier: .identifier(element))),
41+
trailingComma: index < elements.count - 1 ? .commaToken(trailingTrivia: .space) : nil
42+
)
43+
}
44+
)
45+
return PatternSyntax(
46+
TuplePatternSyntax(
47+
leftParen: .leftParenToken(),
48+
elements: patternElements,
49+
rightParen: .rightParenToken()
50+
)
51+
)
52+
}
53+
54+
static func tupleExpr(tuple: Tuple) -> ExprSyntax {
55+
ExprSyntax(
56+
TupleExprSyntax(
57+
leftParen: .leftParenToken(),
58+
elements: LabeledExprListSyntax(
59+
tuple.elements.enumerated().map { index, block in
60+
LabeledExprSyntax(
61+
label: nil,
62+
colon: nil,
63+
expression: block.expr,
64+
trailingComma: index < tuple.elements.count - 1
65+
? .commaToken(trailingTrivia: .space) : nil
66+
)
67+
}
68+
),
69+
rightParen: .rightParenToken()
70+
)
71+
)
72+
}
73+
74+
static func valueExpr(tupleExpr: ExprSyntax, isThrowing: Bool) -> ExprSyntax {
75+
isThrowing
76+
? ExprSyntax(
77+
TryExprSyntax(
78+
tryKeyword: .keyword(.try, trailingTrivia: .space),
79+
expression: ExprSyntax(
80+
AwaitExprSyntax(
81+
awaitKeyword: .keyword(.await, trailingTrivia: .space),
82+
expression: tupleExpr
83+
)
84+
)
85+
)
86+
)
87+
: ExprSyntax(
88+
AwaitExprSyntax(
89+
awaitKeyword: .keyword(.await, trailingTrivia: .space),
90+
expression: tupleExpr
91+
)
92+
)
93+
}
94+
}
95+
}

0 commit comments

Comments
 (0)