-
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.
Browse files
Browse the repository at this point in the history
feat: @WrapperView 매크로 구현
- Loading branch information
Showing
22 changed files
with
421 additions
and
48 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
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
88 changes: 88 additions & 0 deletions
88
Sources/Macros/Implementation/Complex/WrapperViewMacro.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,88 @@ | ||
// | ||
// File.swift | ||
// | ||
// | ||
// Created by 김건우 on 6/3/24. | ||
// | ||
|
||
import SwiftSyntax | ||
import SwiftSyntaxBuilder | ||
import SwiftSyntaxMacros | ||
|
||
public struct WrapperViewMacro { } | ||
|
||
extension WrapperViewMacro: MemberMacro { | ||
|
||
public static func expansion( | ||
of node: AttributeSyntax, | ||
providingMembersOf declaration: some DeclGroupSyntax, | ||
in context: some MacroExpansionContext | ||
) throws -> [DeclSyntax] { | ||
guard | ||
let _ = declaration.as(ClassDeclSyntax.self) | ||
else { | ||
throw MacroError.message("이 매크로는 Class에만 적용할 수 있습니다.") | ||
} | ||
|
||
guard | ||
let genericArguments = node.attributeName | ||
.as(IdentifierTypeSyntax.self)? | ||
.genericArgumentClause? | ||
.arguments, | ||
let firstGenericType = genericArguments | ||
.first? | ||
.argument, | ||
let lastGenericType = genericArguments | ||
.last? | ||
.argument | ||
else { | ||
throw MacroError.message("매크로 선언으로 전달된 Generic 타입이 유효하지 않습니다.") | ||
} | ||
|
||
let scope = declaration.getAccessControl() | ||
|
||
return [ | ||
""" | ||
\(scope)typealias R = \(firstGenericType) | ||
""", | ||
""" | ||
\(scope)typealias V = \(lastGenericType) | ||
""", | ||
|
||
""" | ||
\(scope)func makeView() -> V { | ||
return \(lastGenericType)(reactor: makeReactor()) | ||
} | ||
""", | ||
|
||
""" | ||
\(scope)var view: V { | ||
return makeView() | ||
} | ||
""", | ||
""" | ||
\(scope)var reactor: R { | ||
return makeReactor() | ||
} | ||
""" | ||
] | ||
} | ||
|
||
} | ||
|
||
extension WrapperViewMacro: 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 baseWrapperExtension = try ExtensionDeclSyntax("extension \(type.trimmed): BaseWrapper { }") | ||
|
||
return [baseWrapperExtension] | ||
} | ||
|
||
} | ||
|
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
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
Oops, something went wrong.