style: format the sources as the current SwiftFormat has them

Repeated guards with the same body are joined and a returned value is written once; nothing else changes, and the whole suite passes.
This commit is contained in:
Mihaela Mihaljevic 2026-11-14 19:07:01 +01:00
parent 938ad85d9e
commit 81035d3f92
10 changed files with 22 additions and 62 deletions

View File

@ -123,9 +123,9 @@ private struct SingleValueContainer: SingleValueDecodingContainer {
func decode(_: Double.Type) throws -> Double {
switch value {
case let .double(value):
return value
value
case let .int(value):
return Double(value)
Double(value)
case .null, .bool, .string, .sequence, .mapping:
throw mismatch("Double")
}

View File

@ -21,10 +21,7 @@ extension PureYAML.Emitting.Dumper {
return false
}
let value = String(line)
guard !containsCommentStart(value), !containsMappingSeparator(value) else {
return false
}
guard let first = line.first else {
guard !containsCommentStart(value), !containsMappingSeparator(value), let first = line.first else {
return false
}
return canRenderLiteralBlockLineStarting(with: first, line: line)
@ -85,20 +82,13 @@ extension PureYAML.Emitting.Dumper {
}
func canRenderPlainString(_ value: String) -> Bool {
guard !value.isEmpty else {
return false
}
guard value.first?.isWhitespace == false, value.last?.isWhitespace == false else {
guard !value.isEmpty, value.first?.isWhitespace == false, value.last?.isWhitespace == false else {
return false
}
// By unicode scalar: CR+LF is one Character in Swift, so `contains("\n")` is false for it and a string with a CRLF break would be written raw.
guard !value.unicodeScalars.contains(where: { $0 == "\n" || $0 == "\r" || $0 == "\t" }) else {
return false
}
guard !isReservedPlainScalar(value), !isNumberLikePlainScalar(value) else {
return false
}
guard let first = value.first, !isPlainScalarIndicator(first) else {
guard !value.unicodeScalars.contains(where: { $0 == "\n" || $0 == "\r" || $0 == "\t" }), !isReservedPlainScalar(value), !isNumberLikePlainScalar(value),
let first = value.first, !isPlainScalarIndicator(first)
else {
return false
}
// A trailing `:` reparses as a mapping key with an empty value, so a

View File

@ -64,10 +64,7 @@ extension PureYAML.Emitting.Dumper {
rendered: String,
indent: Int,
) -> [String]? {
guard options.lineWidth > 0, head.count + rendered.count > options.lineWidth else {
return nil
}
guard case let .string(string) = value, rendered == string else {
guard options.lineWidth > 0, head.count + rendered.count > options.lineWidth, case let .string(string) = value, rendered == string else {
return nil
}
let continuationIndent = indent + options.indentWidth

View File

@ -52,9 +52,9 @@ extension PureYAML.Parsing.EventComposer {
) throws -> [[PureYAML.Model.Pair]] {
switch value {
case let .mapping(mapping):
return [mapping.pairs]
[mapping.pairs]
case let .sequence(values):
return try values.map { value in
try values.map { value in
guard case let .mapping(mapping) = value else {
throw PureYAML.Parsing.MergeResolution.invalidMergeValue(mark: mark)
}

View File

@ -217,10 +217,7 @@ extension PureYAML.Parsing.EventComposer {
_ expected: String,
matching: (PureYAML.Parsing.Event) -> Bool,
) throws -> PureYAML.Parsing.Event {
guard let event = current else {
throw unexpectedEvent(expected: expected)
}
guard matching(event) else {
guard let event = current, matching(event) else {
throw unexpectedEvent(expected: expected)
}
index += 1

View File

@ -112,22 +112,15 @@ extension PureYAML.Tagged.Dumper {
flow: Bool,
renderer: PureYAML.Emitting.Dumper,
) -> Bool {
guard !string.isEmpty else {
return false
}
guard string.first?.isWhitespace == false, string.last?.isWhitespace == false else {
return false
}
guard !string.contains("\n"), !string.contains("\r"), !string.contains("\t") else {
guard !string.isEmpty, string.first?.isWhitespace == false, string.last?.isWhitespace == false, !string.contains("\n"), !string.contains("\r"),
!string.contains("\t")
else {
return false
}
// A trailing `:` reparses as a mapping key with an empty value, so a
// scalar that ends in one is not plain-safe even when no interior `:`
// is followed by a space.
guard string.last != ":" else {
return false
}
guard let first = string.first else {
guard string.last != ":", let first = string.first else {
return false
}
let second = string.dropFirst().first

View File

@ -382,10 +382,7 @@ extension PureYAML.Tagged.Dumper {
}
func isPlainStringKey(_ pair: PureYAML.Tagged.Pair) -> Bool {
guard case let .scalar(scalar) = pair.keyNode else {
return false
}
guard case .string = scalar.value else {
guard case let .scalar(scalar) = pair.keyNode, case .string = scalar.value else {
return false
}
return scalar.anchor == nil

View File

@ -242,10 +242,7 @@ extension PureYAML.Tagged.EventComposer {
_ expected: String,
matching: (PureYAML.Parsing.Event) -> Bool,
) throws -> PureYAML.Parsing.Event {
guard let event = current else {
throw unexpectedEvent(expected: expected)
}
guard matching(event) else {
guard let event = current, matching(event) else {
throw unexpectedEvent(expected: expected)
}
index += 1

View File

@ -53,10 +53,7 @@ public extension PureYAML.Tagged.Scalar {
// group structure must be consistent: 8 leftover bits (two missing
// characters) pairs with two `=`, 4 bits with one, 0 with none.
let expectedPadding = bitCount == 4 ? 2 : bitCount == 2 ? 1 : 0
guard bitCount != 6, padding == expectedPadding || padding == 0 else {
return nil
}
guard buffer & ((1 << bitCount) - 1) == 0 || bitCount == 0 else {
guard bitCount != 6, padding == expectedPadding || padding == 0, buffer & ((1 << bitCount) - 1) == 0 || bitCount == 0 else {
return nil
}
return bytes

View File

@ -109,10 +109,7 @@ private func firstIndentationTabColumn(in line: String) -> Int? {
}
private func missingMappingSpaceColumn(in line: String) -> Int? {
guard let colonIndex = line.firstIndex(of: ":") else {
return nil
}
guard isLikelyBlockMappingKey(before: colonIndex, in: line) else {
guard let colonIndex = line.firstIndex(of: ":"), isLikelyBlockMappingKey(before: colonIndex, in: line) else {
return nil
}
@ -139,10 +136,7 @@ private func isLikelyBlockMappingKey(
in line: String,
) -> Bool {
let prefix = line[..<colonIndex]
guard let first = prefix.firstNonWhitespace else {
return false
}
guard first != "#", first != "{", first != "[", first != "\"", first != "'" else {
guard let first = prefix.firstNonWhitespace, first != "#", first != "{", first != "[", first != "\"", first != "'" else {
return false
}
@ -182,13 +176,11 @@ private func missingSequenceSpaceColumn(in line: String) -> Int? {
let afterDash = trimmed.dropFirst()
guard let firstAfterDash = afterDash.first,
!firstAfterDash.isNumber
!firstAfterDash.isNumber,
let dashIndex = line.firstIndex(of: "-")
else {
return nil
}
guard let dashIndex = line.firstIndex(of: "-") else {
return nil
}
return line.distance(from: line.startIndex, to: line.index(after: dashIndex)) + 1
}