Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge macro examples from ExamplePlugin target into MacroExamples #2222

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions Examples/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ let package = Package(
products: [
.executable(name: "AddOneToIntegerLiterals", targets: ["AddOneToIntegerLiterals"]),
.executable(name: "CodeGenerationUsingSwiftSyntaxBuilder", targets: ["CodeGenerationUsingSwiftSyntaxBuilder"]),
.executable(name: "ExamplePlugin", targets: ["ExamplePlugin"]),
],
dependencies: [
.package(path: "../")
Expand All @@ -30,15 +29,6 @@ let package = Package(
.product(name: "SwiftSyntaxBuilder", package: "swift-syntax")
]
),
.executableTarget(
name: "ExamplePlugin",
dependencies: [
.product(name: "SwiftCompilerPlugin", package: "swift-syntax"),
.product(name: "SwiftSyntax", package: "swift-syntax"),
.product(name: "SwiftSyntaxMacros", package: "swift-syntax"),
.product(name: "SwiftDiagnostics", package: "swift-syntax"),
]
),
.macro(
name: "MacroExamplesImplementation",
dependencies: [
Expand Down
17 changes: 0 additions & 17 deletions Examples/Sources/ExamplePlugin/ExamplePlugin.swift

This file was deleted.

145 changes: 0 additions & 145 deletions Examples/Sources/ExamplePlugin/Macros.swift

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import SwiftSyntax
import SwiftSyntaxBuilder
import SwiftSyntaxMacros

/// Func With unique name.
public enum FuncUniqueMacro: DeclarationMacro {
public static func expansion(
of node: some FreestandingMacroExpansionSyntax,
in context: some MacroExpansionContext
) throws -> [DeclSyntax] {
let name = context.makeUniqueName("unique")
return [
"""
class MyClass {
func \(name)() {}
}
"""
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import SwiftSyntax
import SwiftSyntaxMacros

public enum EquatableExtensionMacro: ExtensionMacro {
public static func expansion(
of node: AttributeSyntax,
attachedTo declaration: some DeclGroupSyntax,
providingExtensionsOf type: some TypeSyntaxProtocol,
conformingTo protocols: [TypeSyntax],
in context: some MacroExpansionContext
) throws -> [ExtensionDeclSyntax] {
let equatableExtension = try ExtensionDeclSyntax("extension \(type.trimmed): Equatable {}")

return [equatableExtension]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import SwiftSyntax
import SwiftSyntaxMacros

/// Add '@available(*, deprecated)' to members.
public enum MemberDeprecatedMacro: MemberAttributeMacro {
public static func expansion(
of node: AttributeSyntax,
attachedTo declaration: some DeclGroupSyntax,
providingAttributesFor member: some DeclSyntaxProtocol,
in context: some MacroExpansionContext
) throws -> [AttributeSyntax] {
return ["@available(*, deprecated)"]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import SwiftSyntax
import SwiftSyntaxMacros

/// Peer 'var' with the name suffixed with '_peer'.
public enum PeerValueWithSuffixNameMacro: PeerMacro {
public static func expansion(
of node: AttributeSyntax,
providingPeersOf declaration: some DeclSyntaxProtocol,
in context: some MacroExpansionContext
) throws -> [DeclSyntax] {
guard let identified = declaration.asProtocol(NamedDeclSyntax.self) else {
return []
}
return ["var \(raw: identified.name.text)_peer: Int { 1 }"]
}
}
26 changes: 15 additions & 11 deletions Examples/Sources/MacroExamples/Implementation/Plugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,28 @@ import SwiftSyntaxMacros
@main
struct MyPlugin: CompilerPlugin {
let providingMacros: [Macro.Type] = [
StringifyMacro.self,
AddAsyncMacro.self,
AddBlocker.self,
WarningMacro.self,
FontLiteralMacro.self,
WrapStoredPropertiesMacro.self,
DictionaryStorageMacro.self,
DictionaryStoragePropertyMacro.self,
ObservableMacro.self,
ObservablePropertyMacro.self,
AddCompletionHandlerMacro.self,
AddAsyncMacro.self,
CaseDetectionMacro.self,
MetaEnumMacro.self,
CodableKey.self,
CustomCodable.self,
OptionSetMacro.self,
DictionaryStorageMacro.self,
DictionaryStoragePropertyMacro.self,
EquatableExtensionMacro.self,
FontLiteralMacro.self,
FuncUniqueMacro.self,
MemberDeprecatedMacro.self,
MetaEnumMacro.self,
NewTypeMacro.self,
ObservableMacro.self,
ObservablePropertyMacro.self,
OptionSetMacro.self,
PeerValueWithSuffixNameMacro.self,
StringifyMacro.self,
URLMacro.self,
WarningMacro.self,
WrapStoredPropertiesMacro.self,
]
}
#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

// MARK: - Func Unique

@freestanding(declaration, names: named(MyClass))
public macro FuncUnique() = #externalMacro(module: "MacroExamplesImplementation", type: "FuncUniqueMacro")
16 changes: 16 additions & 0 deletions Examples/Sources/MacroExamples/Interface/ExtensionMacros.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

// MARK: - Equatable Extension

@attached(extension, conformances: Equatable)
public macro equatable() = #externalMacro(module: "MacroExamplesImplementation", type: "EquatableExtensionMacro")
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
//
//===----------------------------------------------------------------------===//

// MARK: - Member Deprecated

@attached(memberAttribute)
public macro memberDeprecated() = #externalMacro(module: "MacroExamplesImplementation", type: "MemberDeprecatedMacro")

// MARK: - Wrap Stored Properties

/// Apply the specified attribute to each of the stored properties within the
Expand Down
Loading