Skip to content

Commit

Permalink
feat: Reactor 매크로 구현 (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
rlarjsdn3 committed Jun 5, 2024
1 parent 4053288 commit 847230b
Show file tree
Hide file tree
Showing 12 changed files with 129 additions and 7 deletions.
5 changes: 3 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ let package = Package(
.macro(
name: "MacrosImplementation",
dependencies: [
"SyntaxHelper",
"SwiftSyntaxHelper",
.product(name: "SwiftSyntaxMacros", package: "swift-syntax"),
.product(name: "SwiftCompilerPlugin", package: "swift-syntax"),
.product(name: "SwiftDiagnostics", package: "swift-syntax"),
Expand All @@ -34,7 +34,7 @@ let package = Package(
),

.target(
name: "SyntaxHelper",
name: "SwiftSyntaxHelper",
dependencies: [
.product(name: "SwiftSyntax", package: "swift-syntax"),
],
Expand Down Expand Up @@ -62,6 +62,7 @@ let package = Package(
.testTarget(
name: "Bibbi-MacroTests",
dependencies: [
"SwiftSyntaxHelper",
"MacrosImplementation",
.product(name: "SwiftSyntaxMacrosTestSupport", package: "swift-syntax"),
],
Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@

- [x] [@Codable]()
- [x] [@CodableKey(_:)]()
- [x] [@Deprecated]()
- [x] [@DependencyValue(for:)]()
- [x] ~~[@Deprecated]()~~ _(Deprecated)_
- [x] ~~[@DependencyValue(for:)]()~~ _(Deprecated)_
- [x] [@DependencyOrganizer]()
- [x] [@Reactor]()
- [x] [@Wrapper]()
- [x] (@WrapperView)

### Libraries

- [x] [DIContainer]()
- [x] [Dependencies]()
- [x] [ReactorKit]()

Expand All @@ -30,4 +32,4 @@

| 버전 | 내용 |
| :----: | :------------------: |
| - | - |
| v0.1.0 | `@Codable`, `@Wrapper` 매크로 구현 등 |
26 changes: 26 additions & 0 deletions Sources/Macros/Implementation/Extension/ReactorMacro.swift
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]
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import SwiftSyntax
import SwiftSyntaxBuilder
import SwiftSyntaxMacros
import SyntaxHelper
import SwiftSyntaxHelper

public struct DependencyOrganizerMacro: MemberAttributeMacro {

Expand Down
1 change: 1 addition & 0 deletions Sources/Macros/Implementation/Plugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ struct Bibbi_MacroPlugin: CompilerPlugin {
WrapperMacro.self,
WrapperViewMacro.self,
URLMacro.self,
ReactorMacro.self,
DependencyOrganizerMacro.self,
DeprecatedMacro.self
]
Expand Down
1 change: 1 addition & 0 deletions Sources/Macros/Interface/AccessorMacrosInterface.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import Dependencies
///
/// Author: - 김소월
///
@available(*, deprecated)
@attached(accessor)
public macro DependencyValue(for: any DependencyKey.Type) = #externalMacro(
module: "MacrosImplementation",
Expand Down
18 changes: 18 additions & 0 deletions Sources/Macros/Interface/ExtensionMacrosInterface.swift
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"
)
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public macro Deprecated() = #externalMacro(
///
/// - Warning: 이 매크로는 Extension에만 적용할 수 있습니다.
///
@available(*, deprecated)
@attached(memberAttribute)
public macro DependencyOrganizer() = #externalMacro(
module: "MacrosImplementation",
Expand Down
2 changes: 1 addition & 1 deletion Sources/Macros/Playground/AccessorMacrosPlayground.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ struct MemberRepoKey: DependencyKey {
static let liveValue: MemberRepositoryProtocol = MemberRepository()
}

// DependencyValues
// DependencyOrganizer
@DependencyOrganizer
extension DependencyValues {
var meRepository: MeRepositoryProtocol
Expand Down
28 changes: 28 additions & 0 deletions Sources/Macros/Playground/ExtensionMacrosPlayground.swift
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)")

}
5 changes: 5 additions & 0 deletions Sources/Macros/Playground/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ runComplexMacorsPlayground()
runExressionMacrosPlayground()


// MARK: - Extension Macros

runExtensionMacrosPlayground()


// MARK: - MemberAttribute Macros

runMemberAttributeMacrosPlayground()
Expand Down
39 changes: 39 additions & 0 deletions Tests/Macros/Extension/ReactorMacroTests.swift
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
)

}

}

0 comments on commit 847230b

Please sign in to comment.