From 9d4225d5aab06945a0c04c53d7b585ce703b901f Mon Sep 17 00:00:00 2001 From: Dan Federman Date: Wed, 10 Jan 2024 12:40:59 -0700 Subject: [PATCH] Do not use NSString's 'trimmingCharacters' API --- .../Generators/DependencyTreeGenerator.swift | 2 +- Sources/SafeDICore/Models/TypeDescription.swift | 14 +++++++------- Tests/SafeDICoreTests/TypeDescriptionTests.swift | 4 ++-- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/Sources/SafeDICore/Generators/DependencyTreeGenerator.swift b/Sources/SafeDICore/Generators/DependencyTreeGenerator.swift index 7ebdbb06..5f6aabfd 100644 --- a/Sources/SafeDICore/Generators/DependencyTreeGenerator.swift +++ b/Sources/SafeDICore/Generators/DependencyTreeGenerator.swift @@ -54,7 +54,7 @@ public final class DependencyTreeGenerator { for try await generatedRoot in taskGroup { generatedRoots.append(generatedRoot) } - return generatedRoots.sorted().joined(separator: "\n\n").trimmingCharacters(in: .whitespacesAndNewlines) + return generatedRoots.sorted().joined(separator: "\n\n") } let importsWhitespace = imports.isEmpty ? "" : "\n" diff --git a/Sources/SafeDICore/Models/TypeDescription.swift b/Sources/SafeDICore/Models/TypeDescription.swift index 95f908e1..e46e2218 100644 --- a/Sources/SafeDICore/Models/TypeDescription.swift +++ b/Sources/SafeDICore/Models/TypeDescription.swift @@ -121,7 +121,7 @@ public enum TypeDescription: Codable, Hashable, Comparable, Sendable { case let .closure(arguments, isAsync, doesThrow, returnType): return "(\(arguments.map { $0.asSource }.joined(separator: ", ")))\([isAsync ? " async" : "", doesThrow ? " throws" : ""].filter { !$0.isEmpty }.joined()) -> \(returnType.asSource)" case let .unknown(text): - return text.trimmingCharacters(in: .whitespacesAndNewlines) + return text } } @@ -478,7 +478,7 @@ extension TypeSyntax { } else { assertionFailure("TypeSyntax of unknown type. Defaulting to `description`.") // The description is a source-accurate description of this node, so it is a reasonable fallback. - return .unknown(text: description) + return .unknown(text: trimmedDescription) } } } @@ -500,7 +500,7 @@ extension ExprSyntax { if let base = memberAccessExpr.base { return base.typeDescription } else { - return .unknown(text: memberAccessExpr.description) + return .unknown(text: memberAccessExpr.trimmedDescription) } } else { if let base = memberAccessExpr.base { @@ -517,7 +517,7 @@ extension ExprSyntax { ) } } else { - return .unknown(text: memberAccessExpr.description) + return .unknown(text: memberAccessExpr.trimmedDescription) } } } else if let genericExpr = GenericSpecializationExprSyntax(self) { @@ -535,7 +535,7 @@ extension ExprSyntax { parentType: parentType, generics: genericTypeVisitor.genericArguments ) case .any, .array, .attributed, .closure, .composition, .dictionary, .implicitlyUnwrappedOptional, .metatype, .optional, .some, .tuple, .unknown: - return .unknown(text: description) + return .unknown(text: trimmedDescription) } } else if let tupleExpr = TupleExprSyntax(self) { let tupleTypes = tupleExpr.elements.map(\.expression.typeDescription) @@ -573,7 +573,7 @@ extension ExprSyntax { returnType: returnType.typeDescription ) } else { - return .unknown(text: description) + return .unknown(text: trimmedDescription) } } else if let optionalChainingExpr = OptionalChainingExprSyntax(self) { return .optional(optionalChainingExpr.expression.typeDescription) @@ -594,7 +594,7 @@ extension ExprSyntax { value: onlyElement.value.typeDescription ) } else { - return .unknown(text: description) + return .unknown(text: trimmedDescription) } } } diff --git a/Tests/SafeDICoreTests/TypeDescriptionTests.swift b/Tests/SafeDICoreTests/TypeDescriptionTests.swift index 09e068eb..c06bd740 100644 --- a/Tests/SafeDICoreTests/TypeDescriptionTests.swift +++ b/Tests/SafeDICoreTests/TypeDescriptionTests.swift @@ -882,8 +882,8 @@ final class TypeDescriptionTests: XCTestCase { } func test_asSource_whenDescribingAnUnknownCase_returnsTheProvidedStringWithWhitespaceStripped() { - let sut = TypeDescription.unknown(text: " SomeTypeThatIsFormattedOddly ") - XCTAssertEqual(sut.asSource, "SomeTypeThatIsFormattedOddly") + let typeDescription = TypeSyntax(stringLiteral: " SomeTypeThatIsFormattedOddly ").typeDescription + XCTAssertEqual(typeDescription.asSource, "SomeTypeThatIsFormattedOddly") } // MARK: - Visitors