From 1502c0f26d45e1173794dc424e8583b1d771051d Mon Sep 17 00:00:00 2001 From: Igor Savelev Date: Fri, 15 Dec 2023 14:48:48 +0000 Subject: [PATCH] Fixed missing attributes in optional closure type name (#1237) * Fixed missing attributes in optional closure type name * Reverted changes in unwrappedTypeName * Added fix for implicitly unwrapped closures * Added forced brackets for optional existential and opaque types * Revert "Added forced brackets for optional existential and opaque types" This reverts commit 50ff6e716530b63d3b3de9751a0a05a006c5cf27. --- .../SwiftSyntax/AST/TypeName+SwiftSyntax.swift | 4 ++-- .../Parsing/FileParser_TypeNameSpec.swift | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/SourceryFramework/Sources/Parsing/SwiftSyntax/AST/TypeName+SwiftSyntax.swift b/SourceryFramework/Sources/Parsing/SwiftSyntax/AST/TypeName+SwiftSyntax.swift index 0c360f52a..164cdcec0 100644 --- a/SourceryFramework/Sources/Parsing/SwiftSyntax/AST/TypeName+SwiftSyntax.swift +++ b/SourceryFramework/Sources/Parsing/SwiftSyntax/AST/TypeName+SwiftSyntax.swift @@ -65,7 +65,7 @@ extension TypeName { } else if let typeIdentifier = node.as(OptionalTypeSyntax.self) { let type = TypeName(typeIdentifier.wrappedType) let needsWrapping = type.isClosure || type.isProtocolComposition - self.init(name: needsWrapping ? "(\(type.name))" : type.name, + self.init(name: needsWrapping ? "(\(type.asSource))" : type.name, isOptional: true, isImplicitlyUnwrappedOptional: false, tuple: type.tuple, @@ -78,7 +78,7 @@ extension TypeName { } else if let typeIdentifier = node.as(ImplicitlyUnwrappedOptionalTypeSyntax.self) { let type = TypeName(typeIdentifier.wrappedType) let needsWrapping = type.isClosure || type.isProtocolComposition - self.init(name: needsWrapping ? "(\(type.name))" : type.name, + self.init(name: needsWrapping ? "(\(type.asSource))" : type.name, isOptional: false, isImplicitlyUnwrappedOptional: true, tuple: type.tuple, diff --git a/SourceryTests/Parsing/FileParser_TypeNameSpec.swift b/SourceryTests/Parsing/FileParser_TypeNameSpec.swift index 645e6b8ec..43f2fd8a6 100644 --- a/SourceryTests/Parsing/FileParser_TypeNameSpec.swift +++ b/SourceryTests/Parsing/FileParser_TypeNameSpec.swift @@ -169,6 +169,24 @@ class TypeNameSpec: QuickSpec { expect(typeName("@escaping @autoclosure () -> String").description).to(equal("@autoclosure @escaping () -> String")) } } + + context("given optional closure type with attributes") { + it("keeps attributes in unwrappedTypeName") { + expect(typeName("(@MainActor @Sendable (Int) -> Void)?").unwrappedTypeName).to(equal("(@MainActor @Sendable (Int) -> Void)")) + } + it("keeps attributes in name") { + expect(typeName("(@MainActor @Sendable (Int) -> Void)?").name).to(equal("(@MainActor @Sendable (Int) -> Void)?")) + } + } + + context("given implicitly unwrapped optional closure type with attributes") { + it("keeps attributes in unwrappedTypeName") { + expect(typeName("(@MainActor @Sendable (Int) -> Void)!").unwrappedTypeName).to(equal("(@MainActor @Sendable (Int) -> Void)")) + } + it("keeps attributes in name") { + expect(typeName("(@MainActor @Sendable (Int) -> Void)!").name).to(equal("(@MainActor @Sendable (Int) -> Void)!")) + } + } } } }