Skip to content

Commit e7fe33e

Browse files
committed
Support inline snapshot API migrations
1 parent d1082c4 commit e7fe33e

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

Sources/InlineSnapshotTesting/AssertInlineSnapshot.swift

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,11 @@ public struct InlineSnapshotSyntaxDescriptor: Hashable {
138138
/// The default label describing an inline snapshot.
139139
public static let defaultTrailingClosureLabel = "matches"
140140

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+
141146
/// The label of the trailing closure that returns the inline snapshot.
142147
public var trailingClosureLabel: String
143148

@@ -172,9 +177,11 @@ public struct InlineSnapshotSyntaxDescriptor: Hashable {
172177
/// - trailingClosureOffset: The offset of the trailing closure that returns the inline
173178
/// snapshot, relative to the first trailing closure.
174179
public init(
180+
deprecatedTrailingClosureLabels: [String] = [],
175181
trailingClosureLabel: String = Self.defaultTrailingClosureLabel,
176182
trailingClosureOffset: Int = 0
177183
) {
184+
self.deprecatedTrailingClosureLabels = deprecatedTrailingClosureLabels
178185
self.trailingClosureLabel = trailingClosureLabel
179186
self.trailingClosureOffset = trailingClosureOffset
180187
}
@@ -216,6 +223,10 @@ public struct InlineSnapshotSyntaxDescriptor: Hashable {
216223
line: trailingClosureLine.map(UInt.init) ?? line
217224
)
218225
}
226+
227+
fileprivate func contains(_ label: String) -> Bool {
228+
self.trailingClosureLabel == label || self.deprecatedTrailingClosureLabels.contains(label)
229+
}
219230
}
220231

221232
// MARK: - Private
@@ -377,6 +388,9 @@ private final class SnapshotRewriter: SyntaxRewriter {
377388
repeating: "#", count: snapshot.actual.hashCount(isMultiline: true)
378389
)
379390
let leadingIndent = leadingTrivia + self.indent
391+
let snapshotLabel = TokenSyntax(
392+
stringLiteral: snapshot.syntaxDescriptor.trailingClosureLabel
393+
)
380394
let snapshotClosure = ClosureExprSyntax(
381395
leftBrace: .leftBraceToken(trailingTrivia: .newline),
382396
statements: CodeBlockItemListSyntax {
@@ -425,6 +439,7 @@ private final class SnapshotRewriter: SyntaxRewriter {
425439
switch centeredTrailingClosureOffset {
426440
case ..<0:
427441
let index = arguments.index(arguments.startIndex, offsetBy: trailingClosureOffset)
442+
functionCallExpr.arguments[index].label = snapshotLabel
428443
functionCallExpr.arguments[index].expression = ExprSyntax(snapshotClosure)
429444

430445
case 0:
@@ -438,7 +453,7 @@ private final class SnapshotRewriter: SyntaxRewriter {
438453
case 1...:
439454
var newElement: MultipleTrailingClosureElementSyntax {
440455
MultipleTrailingClosureElementSyntax(
441-
label: TokenSyntax(stringLiteral: snapshot.syntaxDescriptor.trailingClosureLabel),
456+
label: snapshotLabel,
442457
closure: snapshotClosure.with(\.leadingTrivia, snapshotClosure.leadingTrivia + .space)
443458
)
444459
}
@@ -455,10 +470,11 @@ private final class SnapshotRewriter: SyntaxRewriter {
455470
limitedBy: endIndex
456471
)
457472
{
458-
if functionCallExpr.additionalTrailingClosures[index].label.text
459-
== snapshot.syntaxDescriptor.trailingClosureLabel
460-
{
473+
if snapshot.syntaxDescriptor.contains(
474+
functionCallExpr.additionalTrailingClosures[index].label.text
475+
) {
461476
if snapshot.wasRecording {
477+
functionCallExpr.additionalTrailingClosures[index].label = snapshotLabel
462478
functionCallExpr.additionalTrailingClosures[index].closure = snapshotClosure
463479
}
464480
} else {

0 commit comments

Comments
 (0)