-
Notifications
You must be signed in to change notification settings - Fork 418
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit introduces a new macro, `DefaultFatalErrorImplementationMacro`, to provide default implementations for protocol methods. The macro automatically generates extension code blocks with the default implementations for any protocol it's attached to.
- Loading branch information
Showing
6 changed files
with
214 additions
and
0 deletions.
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
...Sources/MacroExamples/Implementation/Extension/DefaultFatalErrorImplementationMacro.swift
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,72 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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 | ||
import SwiftSyntaxBuilder | ||
import SwiftDiagnostics | ||
|
||
/// Provides default `fatalError` implementations for protocol methods. | ||
/// | ||
/// This macro generates extensions that add default `fatalError` implementations | ||
/// for each method in the protocol it is attached to. | ||
public enum DefaultFatalErrorImplementationMacro: ExtensionMacro { | ||
|
||
/// Unique identifier for messages related to this macro. | ||
private static let messageID = MessageID(domain: "MacroExamples", id: "ProtocolDefaultImplementation") | ||
|
||
/// Generates extension for the protocol to which this macro is attached. | ||
public static func expansion( | ||
of node: AttributeSyntax, | ||
attachedTo declaration: some DeclGroupSyntax, | ||
providingExtensionsOf type: some TypeSyntaxProtocol, | ||
conformingTo protocols: [TypeSyntax], | ||
in context: some MacroExpansionContext | ||
) throws -> [ExtensionDeclSyntax] { | ||
|
||
// Validate that the macro is being applied to a protocol declaration | ||
guard let protocolDecl = declaration.as(ProtocolDeclSyntax.self) else { | ||
throw SimpleDiagnosticMessage( | ||
message: "Macro `defaultFatalErrorImplementation` can only be applied to a protocol", | ||
diagnosticID: messageID, | ||
severity: .error | ||
) | ||
} | ||
|
||
// Extract all the methods from the protocol and assign default implementations | ||
let methods = protocolDecl.memberBlock.members | ||
.map(\.decl) | ||
.compactMap { declaration -> FunctionDeclSyntax? in | ||
guard var function = declaration.as(FunctionDeclSyntax.self) else { | ||
return nil | ||
} | ||
function.body = CodeBlockSyntax { | ||
ExprSyntax(#"fatalError("whoops 😅")"#) | ||
} | ||
return function | ||
} | ||
|
||
// Don't generate an extension if there are no methods | ||
if methods.isEmpty { | ||
return [] | ||
} | ||
|
||
// Generate the extension containing the default implementations | ||
let extensionDecl = ExtensionDeclSyntax(extendedType: type) { | ||
for method in methods { | ||
MemberBlockItemSyntax(decl: method) | ||
} | ||
} | ||
|
||
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
84 changes: 84 additions & 0 deletions
84
...ts/MacroExamples/Implementation/Extension/DefaultFatalErrorImplementationMacroTests.swift
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,84 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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 MacroExamplesImplementation | ||
import SwiftSyntaxMacros | ||
import SwiftSyntaxMacrosTestSupport | ||
import XCTest | ||
|
||
final class DefaultFatalErrorImplementationMacroTests: XCTestCase { | ||
private let macros = ["defaultFatalErrorImplementation": DefaultFatalErrorImplementationMacro.self] | ||
|
||
func testExpansionWhenAttachedToProtocolExpandsCorrectly() { | ||
assertMacroExpansion( | ||
""" | ||
@defaultFatalErrorImplementation | ||
protocol MyProtocol { | ||
func foo() | ||
func bar() -> Int | ||
} | ||
""", | ||
expandedSource: """ | ||
protocol MyProtocol { | ||
func foo() | ||
func bar() -> Int | ||
} | ||
extension MyProtocol { | ||
func foo() { | ||
fatalError("whoops 😅") | ||
} | ||
func bar() -> Int { | ||
fatalError("whoops 😅") | ||
} | ||
} | ||
""", | ||
macros: macros, | ||
indentationWidth: .spaces(2) | ||
) | ||
} | ||
|
||
func testExpansionWhenNotAttachedToProtocolProducesDiagnostic() { | ||
assertMacroExpansion( | ||
""" | ||
@defaultFatalErrorImplementation | ||
class MyClass {} | ||
""", | ||
expandedSource: """ | ||
class MyClass {} | ||
""", | ||
diagnostics: [ | ||
DiagnosticSpec( | ||
message: "Macro `defaultFatalErrorImplementation` can only be applied to a protocol", | ||
line: 1, | ||
column: 1 | ||
) | ||
], | ||
macros: macros, | ||
indentationWidth: .spaces(2) | ||
) | ||
} | ||
|
||
func testExpansionWhenAttachedToEmptyProtocolDoesNotAddExtension() { | ||
assertMacroExpansion( | ||
""" | ||
@defaultFatalErrorImplementation | ||
protocol EmptyProtocol {} | ||
""", | ||
expandedSource: """ | ||
protocol EmptyProtocol {} | ||
""", | ||
macros: macros, | ||
indentationWidth: .spaces(2) | ||
) | ||
} | ||
} |