Skip to content

Commit

Permalink
Fix indentation parsing (#830)
Browse files Browse the repository at this point in the history
Inline snapshot testing attempts to figure out the indentation width of
a file by finding the first line with whitespace and grabbing the prefix
of whitespace. While this works fine if you trim whitespace from
whitespace-only lines, it breaks if you do not, because a line of the
form:

```
"    \n"
```

Will currently match in its entirety and incorporate the newline into
the indentation.

This PR fixes this by picking the first indented line that contains
non-whitespace characters, instead.
  • Loading branch information
stephencelis authored Feb 1, 2024
1 parent 8e68404 commit e7b7722
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Sources/InlineSnapshotTesting/AssertInlineSnapshot.swift
Original file line number Diff line number Diff line change
Expand Up @@ -388,8 +388,8 @@ public struct InlineSnapshotSyntaxDescriptor: Hashable {
self.wasRecording = snapshots.first?.wasRecording ?? isRecording
self.indent = String(
sourceLocationConverter.sourceLines
.first(where: { $0.first?.isWhitespace == true && $0 != "\n" })?
.prefix(while: { $0.isWhitespace })
.first { $0.first?.isWhitespace == true && $0.contains { !$0.isWhitespace } }?
.prefix { $0.isWhitespace }
?? " "
)
self.snapshots = snapshots
Expand Down

0 comments on commit e7b7722

Please sign in to comment.