File tree Expand file tree Collapse file tree 6 files changed +116
-1
lines changed
Tests/MacroExamples/Implementation/Extension Expand file tree Collapse file tree 6 files changed +116
-1
lines changed Original file line number Diff line number Diff line change
1
+ //===----------------------------------------------------------------------===//
2
+ //
3
+ // This source file is part of the Swift.org open source project
4
+ //
5
+ // Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
6
+ // Licensed under Apache License v2.0 with Runtime Library Exception
7
+ //
8
+ // See https://swift.org/LICENSE.txt for license information
9
+ // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10
+ //
11
+ //===----------------------------------------------------------------------===//
12
+
13
+ import SwiftSyntax
14
+ import SwiftSyntaxMacros
15
+
16
+ public enum EquatableExtensionMacro : ExtensionMacro {
17
+ public static func expansion(
18
+ of node: AttributeSyntax ,
19
+ attachedTo declaration: some DeclGroupSyntax ,
20
+ providingExtensionsOf type: some TypeSyntaxProtocol ,
21
+ conformingTo protocols: [ TypeSyntax ] ,
22
+ in context: some MacroExpansionContext
23
+ ) throws -> [ ExtensionDeclSyntax ] {
24
+ let equatableExtension = try ExtensionDeclSyntax ( " extension \( type. trimmed) : Equatable {} " )
25
+
26
+ return [ equatableExtension]
27
+ }
28
+ }
Original file line number Diff line number Diff line change @@ -38,6 +38,7 @@ struct MyPlugin: CompilerPlugin {
38
38
FuncUniqueMacro . self,
39
39
PeerValueWithSuffixNameMacro . self,
40
40
MemberDeprecatedMacro . self,
41
+ EquatableExtensionMacro . self,
41
42
]
42
43
}
43
44
#endif
Original file line number Diff line number Diff line change
1
+ //===----------------------------------------------------------------------===//
2
+ //
3
+ // This source file is part of the Swift.org open source project
4
+ //
5
+ // Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
6
+ // Licensed under Apache License v2.0 with Runtime Library Exception
7
+ //
8
+ // See https://swift.org/LICENSE.txt for license information
9
+ // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10
+ //
11
+ //===----------------------------------------------------------------------===//
12
+
13
+ // MARK: - Equatable Extension
14
+
15
+ @attached ( extension, conformances: Equatable)
16
+ public macro equatable( ) = #externalMacro( module: " MacroExamplesImplementation " , type: " EquatableExtensionMacro " )
Original file line number Diff line number Diff line change
1
+ //===----------------------------------------------------------------------===//
2
+ //
3
+ // This source file is part of the Swift.org open source project
4
+ //
5
+ // Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
6
+ // Licensed under Apache License v2.0 with Runtime Library Exception
7
+ //
8
+ // See https://swift.org/LICENSE.txt for license information
9
+ // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10
+ //
11
+ //===----------------------------------------------------------------------===//
12
+
13
+ import MacroExamplesInterface
14
+
15
+ // MARK: - Equatable Extension
16
+
17
+ @equatable
18
+ struct Pet {
19
+ let name : String
20
+ }
21
+
22
+ func runEquatableExtensionMacroPlayground( ) {
23
+ let cat = Pet ( name: " Tom " )
24
+ let mouse = Pet ( name: " Jerry " )
25
+
26
+ print ( " Has the cat \( cat) the same name as the mouse \( mouse) ? " , cat == mouse ? " Yes. " : " No. " )
27
+ }
Original file line number Diff line number Diff line change @@ -32,7 +32,7 @@ runExpressionMacrosPlayground()
32
32
33
33
// MARK: - Extension Macros
34
34
35
- // TODO: Add example of extension macro
35
+ runEquatableExtensionMacroPlayground ( )
36
36
37
37
// MARK: - Member Attribute Macros
38
38
Original file line number Diff line number Diff line change
1
+ //===----------------------------------------------------------------------===//
2
+ //
3
+ // This source file is part of the Swift.org open source project
4
+ //
5
+ // Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
6
+ // Licensed under Apache License v2.0 with Runtime Library Exception
7
+ //
8
+ // See https://swift.org/LICENSE.txt for license information
9
+ // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10
+ //
11
+ //===----------------------------------------------------------------------===//
12
+
13
+ import MacroExamplesImplementation
14
+ import SwiftSyntaxMacros
15
+ import SwiftSyntaxMacrosTestSupport
16
+ import XCTest
17
+
18
+ final class EquatableExtensionMacroTests : XCTestCase {
19
+ private let macros = [ " equatable " : EquatableExtensionMacro . self]
20
+
21
+ func testExpansionAddsExtensionWithEquatableConformance( ) {
22
+ assertMacroExpansion (
23
+ """
24
+ @equatable
25
+ final public class Message {
26
+ let text: String
27
+ let sender: String
28
+ }
29
+ """ ,
30
+ expandedSource: """
31
+ final public class Message {
32
+ let text: String
33
+ let sender: String
34
+ }
35
+
36
+ extension Message: Equatable {
37
+ }
38
+ """ ,
39
+ macros: macros,
40
+ indentationWidth: . spaces( 2 )
41
+ )
42
+ }
43
+ }
You can’t perform that action at this time.
0 commit comments