@@ -138,6 +138,11 @@ public struct InlineSnapshotSyntaxDescriptor: Hashable {
138
138
/// The default label describing an inline snapshot.
139
139
public static let defaultTrailingClosureLabel = " matches "
140
140
141
+ /// A list of trailing closure labels from deprecated interfaces.
142
+ ///
143
+ /// Useful for providing migration paths for custom snapshot functions.
144
+ public var deprecatedTrailingClosureLabels : [ String ]
145
+
141
146
/// The label of the trailing closure that returns the inline snapshot.
142
147
public var trailingClosureLabel : String
143
148
@@ -172,9 +177,11 @@ public struct InlineSnapshotSyntaxDescriptor: Hashable {
172
177
/// - trailingClosureOffset: The offset of the trailing closure that returns the inline
173
178
/// snapshot, relative to the first trailing closure.
174
179
public init (
180
+ deprecatedTrailingClosureLabels: [ String ] = [ ] ,
175
181
trailingClosureLabel: String = Self . defaultTrailingClosureLabel,
176
182
trailingClosureOffset: Int = 0
177
183
) {
184
+ self . deprecatedTrailingClosureLabels = deprecatedTrailingClosureLabels
178
185
self . trailingClosureLabel = trailingClosureLabel
179
186
self . trailingClosureOffset = trailingClosureOffset
180
187
}
@@ -216,6 +223,10 @@ public struct InlineSnapshotSyntaxDescriptor: Hashable {
216
223
line: trailingClosureLine. map ( UInt . init) ?? line
217
224
)
218
225
}
226
+
227
+ fileprivate func contains( _ label: String ) -> Bool {
228
+ self . trailingClosureLabel == label || self . deprecatedTrailingClosureLabels. contains ( label)
229
+ }
219
230
}
220
231
221
232
// MARK: - Private
@@ -377,6 +388,9 @@ private final class SnapshotRewriter: SyntaxRewriter {
377
388
repeating: " # " , count: snapshot. actual. hashCount ( isMultiline: true )
378
389
)
379
390
let leadingIndent = leadingTrivia + self . indent
391
+ let snapshotLabel = TokenSyntax (
392
+ stringLiteral: snapshot. syntaxDescriptor. trailingClosureLabel
393
+ )
380
394
let snapshotClosure = ClosureExprSyntax (
381
395
leftBrace: . leftBraceToken( trailingTrivia: . newline) ,
382
396
statements: CodeBlockItemListSyntax {
@@ -425,6 +439,7 @@ private final class SnapshotRewriter: SyntaxRewriter {
425
439
switch centeredTrailingClosureOffset {
426
440
case ..< 0 :
427
441
let index = arguments. index ( arguments. startIndex, offsetBy: trailingClosureOffset)
442
+ functionCallExpr. arguments [ index] . label = snapshotLabel
428
443
functionCallExpr. arguments [ index] . expression = ExprSyntax ( snapshotClosure)
429
444
430
445
case 0 :
@@ -438,7 +453,7 @@ private final class SnapshotRewriter: SyntaxRewriter {
438
453
case 1 ... :
439
454
var newElement : MultipleTrailingClosureElementSyntax {
440
455
MultipleTrailingClosureElementSyntax (
441
- label: TokenSyntax ( stringLiteral : snapshot . syntaxDescriptor . trailingClosureLabel ) ,
456
+ label: snapshotLabel ,
442
457
closure: snapshotClosure. with ( \. leadingTrivia, snapshotClosure. leadingTrivia + . space)
443
458
)
444
459
}
@@ -455,10 +470,11 @@ private final class SnapshotRewriter: SyntaxRewriter {
455
470
limitedBy: endIndex
456
471
)
457
472
{
458
- if functionCallExpr . additionalTrailingClosures [ index ] . label . text
459
- == snapshot . syntaxDescriptor . trailingClosureLabel
460
- {
473
+ if snapshot . syntaxDescriptor . contains (
474
+ functionCallExpr . additionalTrailingClosures [ index ] . label . text
475
+ ) {
461
476
if snapshot. wasRecording {
477
+ functionCallExpr. additionalTrailingClosures [ index] . label = snapshotLabel
462
478
functionCallExpr. additionalTrailingClosures [ index] . closure = snapshotClosure
463
479
}
464
480
} else {
0 commit comments