Skip to content

Commit 2ae060c

Browse files
committed
Move EquatableExtensionMacro from ExamplePlugin to MacroExamples target
1 parent cb105c1 commit 2ae060c

File tree

6 files changed

+116
-1
lines changed

6 files changed

+116
-1
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
}

Examples/Sources/MacroExamples/Implementation/Plugin.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ struct MyPlugin: CompilerPlugin {
3838
FuncUniqueMacro.self,
3939
PeerValueWithSuffixNameMacro.self,
4040
MemberDeprecatedMacro.self,
41+
EquatableExtensionMacro.self,
4142
]
4243
}
4344
#endif
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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")
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
}

Examples/Sources/MacroExamples/Playground/main.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ runExpressionMacrosPlayground()
3232

3333
// MARK: - Extension Macros
3434

35-
// TODO: Add example of extension macro
35+
runEquatableExtensionMacroPlayground()
3636

3737
// MARK: - Member Attribute Macros
3838

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
}

0 commit comments

Comments
 (0)