Skip to content

Commit

Permalink
Added forced brackets for optional existential and opaque types
Browse files Browse the repository at this point in the history
  • Loading branch information
igor-savelev-bumble committed Dec 14, 2023
1 parent cca37ec commit 50ff6e7
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,12 @@ extension TypeName {
self.init(name: name, isProtocolComposition: true)
} else if let typeIdentifier = node.as(OptionalTypeSyntax.self) {
let type = TypeName(typeIdentifier.wrappedType)
let needsWrapping = type.isClosure || type.isProtocolComposition
let needsWrapping = [
type.isClosure,
type.isProtocolComposition,
type.isExistential,
type.isOpaque,
].contains(true)
self.init(name: needsWrapping ? "(\(type.asSource))" : type.name,
isOptional: true,
isImplicitlyUnwrappedOptional: false,
Expand All @@ -77,7 +82,12 @@ extension TypeName {
)
} else if let typeIdentifier = node.as(ImplicitlyUnwrappedOptionalTypeSyntax.self) {
let type = TypeName(typeIdentifier.wrappedType)
let needsWrapping = type.isClosure || type.isProtocolComposition
let needsWrapping = [
type.isClosure,
type.isProtocolComposition,
type.isExistential,
type.isOpaque,
].contains(true)
self.init(name: needsWrapping ? "(\(type.asSource))" : type.name,
isOptional: false,
isImplicitlyUnwrappedOptional: true,
Expand Down
12 changes: 12 additions & 0 deletions SourceryRuntime/Sources/AST/TypeName/TypeName.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,18 @@ public final class TypeName: NSObject, SourceryModelWithoutDescription, Lossless
return name == "Void" || name == "()" || unwrappedTypeName == "Void"
}

// sourcery: skipEquality
/// Whether type is existential type (`any FooBar`)
public var isExistential: Bool {
return name.hasPrefix("any")
}

// sourcery: skipEquality
/// Whether type is opaque type (`some FooBar`)
public var isOpaque: Bool {
return name.hasPrefix("some")
}

/// Whether type is a tuple
public var isTuple: Bool {
actualTypeName?.tuple != nil || tuple != nil
Expand Down
27 changes: 27 additions & 0 deletions SourceryTests/Parsing/FileParser_TypeNameSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,33 @@ class TypeNameSpec: QuickSpec {
expect(typeName("(@MainActor @Sendable (Int) -> Void)!").name).to(equal("(@MainActor @Sendable (Int) -> Void)!"))
}
}

context("given optional opaque type") {
it("unwrappedTypeName keeps brackets") {
expect(typeName("(some FooBar)?").unwrappedTypeName).to(equal("(some FooBar)"))
}
it("name keeps brackets") {
expect(typeName("(some FooBar)?").name).to(equal("(some FooBar)?"))
}
}

context("given implicitly unwrapped optional opaque type") {
it("unwrappedTypeName keeps brackets") {
expect(typeName("(some FooBar)!").unwrappedTypeName).to(equal("(some FooBar)"))
}
it("name keeps brackets") {
expect(typeName("(some FooBar)!").name).to(equal("(some FooBar)!"))
}
}

context("given implicitly unwrapped optional existential type") {
it("unwrappedTypeName keeps brackets") {
expect(typeName("(any FooBar)!").unwrappedTypeName).to(equal("(any FooBar)"))
}
it("name keeps brackets") {
expect(typeName("(any FooBar)!").name).to(equal("(any FooBar)!"))
}
}
}
}
}

0 comments on commit 50ff6e7

Please sign in to comment.