Skip to content

Commit f2b3d26

Browse files
Fixes for SwiftSyntax 601 (#42)
* wip * wip * wip * wip * Update ci.yml --------- Co-authored-by: Stephen Celis <[email protected]>
1 parent 972875f commit f2b3d26

File tree

5 files changed

+78
-21
lines changed

5 files changed

+78
-21
lines changed

.github/workflows/ci.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ concurrency:
1616
jobs:
1717
macos:
1818
name: macOS
19-
runs-on: macos-13
19+
runs-on: macos-15
2020
steps:
2121
- uses: actions/checkout@v4
22-
- name: Select Xcode 15
23-
run: sudo xcode-select -s /Applications/Xcode_15.0.app
22+
- name: Select Xcode 16.2
23+
run: sudo xcode-select -s /Applications/Xcode_16.2.app
2424
- name: Run tests
2525
run: swift test
2626

@@ -80,4 +80,4 @@ jobs:
8080
- uses: actions/checkout@v4
8181
- uses: skiptools/swift-android-action@v2
8282
with:
83-
swift-version: ${{ matrix.swift }}
83+
swift-version: ${{ matrix.swift }}

Package.resolved

Lines changed: 8 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Sources/MacroTesting/AssertMacro.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,23 @@ extension FixIt.Change {
470470
range: start..<end,
471471
replacement: newTrivia.description
472472
)
473+
474+
#if canImport(SwiftSyntax601)
475+
case .replaceChild(let replacingChildData):
476+
let range = replacingChildData.replacementRange
477+
let start = expansionContext.position(
478+
of: range.lowerBound,
479+
anchoredAt: replacingChildData.parent
480+
)
481+
let end = expansionContext.position(
482+
of: range.upperBound,
483+
anchoredAt: replacingChildData.parent
484+
)
485+
return SourceEdit(
486+
range: start..<end,
487+
replacement: replacingChildData.newChild.description
488+
)
489+
#endif
473490
}
474491
}
475492
}

Tests/MacroTestingTests/MacroExamples/AddAsyncMacro.swift

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ public struct AddAsyncMacro: PeerMacro {
5353
let completionHandlerParameterAttribute = funcDecl.signature.parameterClause.parameters.last?
5454
.type.as(AttributedTypeSyntax.self),
5555
let completionHandlerParameter = completionHandlerParameterAttribute.baseType.as(
56-
FunctionTypeSyntax.self)
56+
FunctionTypeSyntax.self
57+
)
5758
else {
5859
throw CustomError.message(
5960
"@addAsync requires an function that has a completion handler as last parameter"
@@ -70,12 +71,27 @@ public struct AddAsyncMacro: PeerMacro {
7071
}
7172

7273
let returnType = completionHandlerParameter.parameters.first?.type
73-
7474
let isResultReturn = returnType?.children(viewMode: .all).first?.description == "Result"
75-
let successReturnType =
76-
isResultReturn
77-
? returnType!.as(IdentifierTypeSyntax.self)!.genericArgumentClause?.arguments.first!.argument
78-
: returnType
75+
76+
let successReturnType: TypeSyntax? = {
77+
guard isResultReturn
78+
else { return returnType }
79+
80+
let successType = returnType!.as(IdentifierTypeSyntax.self)!.genericArgumentClause?.arguments.first!
81+
.argument
82+
#if canImport(SwiftSyntax601)
83+
switch successType {
84+
case .none:
85+
return nil
86+
case .type(let syntax):
87+
return syntax
88+
default:
89+
return nil
90+
}
91+
#else
92+
return successType
93+
#endif
94+
}()
7995

8096
// Remove completionHandler and comma from the previous parameter
8197
var newParameterList = funcDecl.signature.parameterClause.parameters
@@ -141,7 +157,9 @@ public struct AddAsyncMacro: PeerMacro {
141157
// add result type
142158
if let successReturnType {
143159
funcDecl.signature.returnClause = ReturnClauseSyntax(
144-
leadingTrivia: .space, type: successReturnType.with(\.leadingTrivia, .space))
160+
leadingTrivia: .space,
161+
type: successReturnType.with(\.leadingTrivia, .space)
162+
)
145163
} else {
146164
funcDecl.signature.returnClause = nil
147165
}

Tests/MacroTestingTests/MacroExamples/OptionSetMacro.swift

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ public struct OptionSetMacro {
9393
else {
9494
context.diagnose(
9595
OptionSetMacroDiagnostic.requiresStringLiteral(optionsEnumNameArgumentLabel).diagnose(
96-
at: optionEnumNameArg.expression))
96+
at: optionEnumNameArg.expression
97+
)
98+
)
9799
return nil
98100
}
99101

@@ -121,7 +123,8 @@ public struct OptionSetMacro {
121123
}).first
122124
else {
123125
context.diagnose(
124-
OptionSetMacroDiagnostic.requiresOptionsEnum(optionsEnumName).diagnose(at: decl))
126+
OptionSetMacroDiagnostic.requiresOptionsEnum(optionsEnumName).diagnose(at: decl)
127+
)
125128
return nil
126129
}
127130

@@ -135,7 +138,25 @@ public struct OptionSetMacro {
135138
return nil
136139
}
137140

138-
return (structDecl, optionsEnum, rawType)
141+
let rawTypeSyntax: TypeSyntax?
142+
#if canImport(SwiftSyntax601)
143+
switch rawType {
144+
case .type(let typeSyntax):
145+
rawTypeSyntax = typeSyntax
146+
default:
147+
rawTypeSyntax = nil
148+
}
149+
#else
150+
rawTypeSyntax = rawType
151+
#endif
152+
153+
guard let rawTypeSyntax
154+
else {
155+
context.diagnose(OptionSetMacroDiagnostic.requiresOptionsEnumRawType.diagnose(at: attribute))
156+
return nil
157+
}
158+
159+
return (structDecl, optionsEnum, rawTypeSyntax)
139160
}
140161
}
141162

0 commit comments

Comments
 (0)