-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
129 additions
and
7 deletions.
There are no files selected for viewing
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
26 changes: 26 additions & 0 deletions
26
Sources/Macros/Implementation/Extension/ReactorMacro.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,26 @@ | ||
// | ||
// File.swift | ||
// | ||
// | ||
// Created by 김건우 on 6/5/24. | ||
// | ||
|
||
import SwiftSyntax | ||
import SwiftSyntaxBuilder | ||
import SwiftSyntaxMacros | ||
|
||
public struct ReactorMacro: 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 reactorExtension = try ExtensionDeclSyntax("extension \(type.trimmed): Reactor { }") | ||
|
||
return [reactorExtension] | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// | ||
// File.swift | ||
// | ||
// | ||
// Created by 김건우 on 6/5/24. | ||
// | ||
|
||
import ReactorKit | ||
|
||
// MARK: - Reactor Extension | ||
|
||
/// 타입이 Reactor 프로토콜을 준수하게 합니다. | ||
/// - Author: 김소월 | ||
@attached(extension, conformances: Reactor) | ||
public macro Reactor() = #externalMacro( | ||
module: "MacrosImplementation", | ||
type: "ReactorMacro" | ||
) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// | ||
// File.swift | ||
// | ||
// | ||
// Created by 김건우 on 6/5/24. | ||
// | ||
|
||
import ReactorKit | ||
import MacrosInterface | ||
|
||
// MARK: - Reactor Extension | ||
|
||
@Reactor | ||
public class CommentReactor { | ||
public typealias Action = NoAction | ||
public struct State { } | ||
public var initialState = State() | ||
} | ||
|
||
|
||
|
||
|
||
func runExtensionMacrosPlayground() { | ||
|
||
let reactor = CommentReactor() | ||
print("CommentReactor: \(reactor)") | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// | ||
// File.swift | ||
// | ||
// | ||
// Created by 김건우 on 6/5/24. | ||
// | ||
|
||
import SwiftSyntaxMacros | ||
import SwiftSyntaxMacrosTestSupport | ||
import MacrosImplementation | ||
import XCTest | ||
import SyntaxHelper | ||
|
||
fileprivate let testMacros: [String: Macro.Type] = [ | ||
"Reactor": ReactorMacro.self | ||
] | ||
|
||
final class ReactorMacroTests: XCTestCase { | ||
|
||
func testReactorMacro() throws { | ||
|
||
assertMacroExpansion( | ||
""" | ||
@Reactor | ||
class testReactor { } | ||
""", | ||
expandedSource: | ||
""" | ||
class testReactor { } | ||
extension testReactor: Reactor { | ||
} | ||
""", | ||
macros: testMacros | ||
) | ||
|
||
} | ||
|
||
} |