Skip to content

Commit

Permalink
Fixed missing attributes in optional closure type name (#1237)
Browse files Browse the repository at this point in the history
* 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 50ff6e7.
  • Loading branch information
leonspok authored Dec 15, 2023
1 parent 717e0ba commit 1502c0f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
18 changes: 18 additions & 0 deletions SourceryTests/Parsing/FileParser_TypeNameSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)!"))
}
}
}
}
}

0 comments on commit 1502c0f

Please sign in to comment.