Skip to content

Commit fee7838

Browse files
authored
Merge pull request #2222 from Matejkob/merge-example-plung-target-into-macro-examples
Merge macro examples from `ExamplePlugin` target into `MacroExamples`
2 parents 6edf022 + 0c044f7 commit fee7838

21 files changed

+458
-185
lines changed

Examples/Package.swift

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ let package = Package(
1111
products: [
1212
.executable(name: "AddOneToIntegerLiterals", targets: ["AddOneToIntegerLiterals"]),
1313
.executable(name: "CodeGenerationUsingSwiftSyntaxBuilder", targets: ["CodeGenerationUsingSwiftSyntaxBuilder"]),
14-
.executable(name: "ExamplePlugin", targets: ["ExamplePlugin"]),
1514
],
1615
dependencies: [
1716
.package(path: "../")
@@ -30,15 +29,6 @@ let package = Package(
3029
.product(name: "SwiftSyntaxBuilder", package: "swift-syntax")
3130
]
3231
),
33-
.executableTarget(
34-
name: "ExamplePlugin",
35-
dependencies: [
36-
.product(name: "SwiftCompilerPlugin", package: "swift-syntax"),
37-
.product(name: "SwiftSyntax", package: "swift-syntax"),
38-
.product(name: "SwiftSyntaxMacros", package: "swift-syntax"),
39-
.product(name: "SwiftDiagnostics", package: "swift-syntax"),
40-
]
41-
),
4232
.macro(
4333
name: "MacroExamplesImplementation",
4434
dependencies: [

Examples/Sources/ExamplePlugin/ExamplePlugin.swift

Lines changed: 0 additions & 17 deletions
This file was deleted.

Examples/Sources/ExamplePlugin/Macros.swift

Lines changed: 0 additions & 145 deletions
This file was deleted.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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 SwiftSyntaxBuilder
15+
import SwiftSyntaxMacros
16+
17+
/// Func With unique name.
18+
public enum FuncUniqueMacro: DeclarationMacro {
19+
public static func expansion(
20+
of node: some FreestandingMacroExpansionSyntax,
21+
in context: some MacroExpansionContext
22+
) throws -> [DeclSyntax] {
23+
let name = context.makeUniqueName("unique")
24+
return [
25+
"""
26+
class MyClass {
27+
func \(name)() {}
28+
}
29+
"""
30+
]
31+
}
32+
}
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+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
/// Add '@available(*, deprecated)' to members.
17+
public enum MemberDeprecatedMacro: MemberAttributeMacro {
18+
public static func expansion(
19+
of node: AttributeSyntax,
20+
attachedTo declaration: some DeclGroupSyntax,
21+
providingAttributesFor member: some DeclSyntaxProtocol,
22+
in context: some MacroExpansionContext
23+
) throws -> [AttributeSyntax] {
24+
return ["@available(*, deprecated)"]
25+
}
26+
}
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+
/// Peer 'var' with the name suffixed with '_peer'.
17+
public enum PeerValueWithSuffixNameMacro: PeerMacro {
18+
public static func expansion(
19+
of node: AttributeSyntax,
20+
providingPeersOf declaration: some DeclSyntaxProtocol,
21+
in context: some MacroExpansionContext
22+
) throws -> [DeclSyntax] {
23+
guard let identified = declaration.asProtocol(NamedDeclSyntax.self) else {
24+
return []
25+
}
26+
return ["var \(raw: identified.name.text)_peer: Int { 1 }"]
27+
}
28+
}

Examples/Sources/MacroExamples/Implementation/Plugin.swift

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,28 @@ import SwiftSyntaxMacros
1717
@main
1818
struct MyPlugin: CompilerPlugin {
1919
let providingMacros: [Macro.Type] = [
20-
StringifyMacro.self,
20+
AddAsyncMacro.self,
2121
AddBlocker.self,
22-
WarningMacro.self,
23-
FontLiteralMacro.self,
24-
WrapStoredPropertiesMacro.self,
25-
DictionaryStorageMacro.self,
26-
DictionaryStoragePropertyMacro.self,
27-
ObservableMacro.self,
28-
ObservablePropertyMacro.self,
2922
AddCompletionHandlerMacro.self,
30-
AddAsyncMacro.self,
3123
CaseDetectionMacro.self,
32-
MetaEnumMacro.self,
3324
CodableKey.self,
3425
CustomCodable.self,
35-
OptionSetMacro.self,
26+
DictionaryStorageMacro.self,
27+
DictionaryStoragePropertyMacro.self,
28+
EquatableExtensionMacro.self,
29+
FontLiteralMacro.self,
30+
FuncUniqueMacro.self,
31+
MemberDeprecatedMacro.self,
32+
MetaEnumMacro.self,
3633
NewTypeMacro.self,
34+
ObservableMacro.self,
35+
ObservablePropertyMacro.self,
36+
OptionSetMacro.self,
37+
PeerValueWithSuffixNameMacro.self,
38+
StringifyMacro.self,
3739
URLMacro.self,
40+
WarningMacro.self,
41+
WrapStoredPropertiesMacro.self,
3842
]
3943
}
4044
#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: - Func Unique
14+
15+
@freestanding(declaration, names: named(MyClass))
16+
public macro FuncUnique() = #externalMacro(module: "MacroExamplesImplementation", type: "FuncUniqueMacro")
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")

Examples/Sources/MacroExamples/Interface/MemberAttributeMacros.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
// MARK: - Member Deprecated
14+
15+
@attached(memberAttribute)
16+
public macro memberDeprecated() = #externalMacro(module: "MacroExamplesImplementation", type: "MemberDeprecatedMacro")
17+
1318
// MARK: - Wrap Stored Properties
1419

1520
/// Apply the specified attribute to each of the stored properties within the

0 commit comments

Comments
 (0)