-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
7 changed files
with
200 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import SwiftSyntax | ||
import SwiftSyntaxBuilder | ||
import SwiftSyntaxMacros | ||
import Foundation | ||
|
||
public struct ConformToHashable: ExtensionMacro { | ||
public static func expansion(of node: SwiftSyntax.AttributeSyntax, | ||
attachedTo declaration: some SwiftSyntax.DeclGroupSyntax, | ||
providingExtensionsOf type: some SwiftSyntax.TypeSyntaxProtocol, | ||
conformingTo protocols: [SwiftSyntax.TypeSyntax], | ||
in context: some SwiftSyntaxMacros.MacroExpansionContext) throws -> [SwiftSyntax.ExtensionDeclSyntax] { | ||
guard [SwiftSyntax.SyntaxKind.classDecl].contains(declaration.kind) else { | ||
throw MacroDiagnostics.errorMacroUsage(message: "Can only be applied to a class type") | ||
} | ||
let equatableProtocol = InheritanceClauseSyntax(inheritedTypes: InheritedTypeListSyntax( | ||
arrayLiteral: InheritedTypeSyntax(type: TypeSyntax(stringLiteral: "Hashable"))) | ||
) | ||
|
||
let mambers = declaration.memberBlock.members.compactMap { member in | ||
if let patternBinding = member.decl.as(VariableDeclSyntax.self)?.bindings | ||
.as(PatternBindingListSyntax.self)?.first?.as(PatternBindingSyntax.self), | ||
let identifier = patternBinding.pattern.as(IdentifierPatternSyntax.self)?.identifier, | ||
let type = patternBinding.typeAnnotation?.as(TypeAnnotationSyntax.self)?.type { | ||
if type.is(IdentifierTypeSyntax.self) { | ||
return "hasher.combine(\(identifier))" | ||
} | ||
if let wrappedType = type.as(OptionalTypeSyntax.self)?.wrappedType, | ||
wrappedType.is(IdentifierTypeSyntax.self) { | ||
return "hasher.combine(\(identifier))" | ||
} | ||
} | ||
return nil | ||
}.joined(separator: "\n ") | ||
|
||
let equtableFunction = """ | ||
func hash(into hasher: inout Hasher) { | ||
\(mambers) | ||
} | ||
""" | ||
|
||
let member = MemberBlockSyntax(members: MemberBlockItemListSyntax(stringLiteral: equtableFunction)) | ||
let extensionDecl = ExtensionDeclSyntax(extendedType: type, | ||
inheritanceClause: equatableProtocol, | ||
memberBlock: member) | ||
return [extensionDecl] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import SwiftSyntaxMacros | ||
import SwiftSyntaxMacrosTestSupport | ||
import XCTest | ||
import Macros | ||
|
||
final class ConformToHashableTests: XCTestCase { | ||
let testMacros: [String: Macro.Type] = [ | ||
"ConformToHashable": ConformToHashable.self, | ||
] | ||
|
||
func testConformToEquatableMacro() { | ||
assertMacroExpansion( | ||
""" | ||
@ConformToHashable | ||
class AClass { | ||
let a: Int | ||
let b: Int | ||
init(a: Int, b: Int) { | ||
self.a = a | ||
self.b = b | ||
} | ||
} | ||
""", | ||
expandedSource: | ||
""" | ||
class AClass { | ||
let a: Int | ||
let b: Int | ||
init(a: Int, b: Int) { | ||
self.a = a | ||
self.b = b | ||
} | ||
} | ||
extension AClass: Hashable { | ||
func hash(into hasher: inout Hasher) { | ||
hasher.combine(a) | ||
} | ||
} | ||
""", | ||
macros: testMacros | ||
) | ||
} | ||
|
||
func testConformToEquatableIgnoreClosureTypeMacro() { | ||
assertMacroExpansion( | ||
""" | ||
@ConformToHashable | ||
class AClass { | ||
let a: Int | ||
let b: (Int) -> Void | ||
init(a: Int, b: (Int) -> Void) { | ||
self.a = a | ||
self.b = b | ||
} | ||
} | ||
""", | ||
expandedSource: | ||
""" | ||
class AClass { | ||
let a: Int | ||
let b: (Int) -> Void | ||
init(a: Int, b: (Int) -> Void) { | ||
self.a = a | ||
self.b = b | ||
} | ||
} | ||
extension AClass: Hashable { | ||
func hash(into hasher: inout Hasher) { | ||
hasher.combine(a) | ||
} | ||
} | ||
""", | ||
macros: testMacros | ||
) | ||
} | ||
|
||
func testConformToEquatableIncludeOptionalTypeMacro() { | ||
assertMacroExpansion( | ||
""" | ||
@ConformToHashable | ||
class AClass { | ||
let a: Int? | ||
init(a: Int?) { | ||
self.a = a | ||
} | ||
} | ||
""", | ||
expandedSource: | ||
""" | ||
class AClass { | ||
let a: Int? | ||
init(a: Int?) { | ||
self.a = a | ||
} | ||
} | ||
extension AClass: Hashable { | ||
func hash(into hasher: inout Hasher) { | ||
hasher.combine(a) | ||
} | ||
} | ||
""", | ||
macros: testMacros | ||
) | ||
} | ||
} |