-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from Ryu0118/feature/dependency-value-macro
add DependencyValue Macro
- Loading branch information
Showing
10 changed files
with
281 additions
and
27 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
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
110 changes: 110 additions & 0 deletions
110
Sources/DependenciesMacroPlugin/DependencyValueMacroDiagnostic.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,110 @@ | ||
import SwiftSyntax | ||
import SwiftSyntaxMacros | ||
import SwiftDiagnostics | ||
|
||
public enum DependencyValuesMacroDiagnostic { | ||
case notExtension | ||
case notDependencyValues | ||
case invalidArgument | ||
} | ||
|
||
extension DependencyValuesMacroDiagnostic: DiagnosticMessage { | ||
func diagnose(at node: some SyntaxProtocol) -> Diagnostic { | ||
Diagnostic(node: Syntax(node), message: self) | ||
} | ||
|
||
public var message: String { | ||
switch self { | ||
case .notExtension: | ||
"DependencyValue Macro can only be applied to extension." | ||
|
||
case .invalidArgument: | ||
"Invalid argument." | ||
|
||
case .notDependencyValues: | ||
"DependencyValue Macro can only be applied to extension of DependencyValues" | ||
} | ||
} | ||
|
||
public var severity: DiagnosticSeverity { .error } | ||
|
||
public var diagnosticID: MessageID { | ||
switch self { | ||
case .notExtension: | ||
MessageID(domain: "DependencyValuesMacroDiagnostic", id: "notExtension") | ||
|
||
case .invalidArgument: | ||
MessageID(domain: "DependencyValuesMacroDiagnostic", id: "invalidArgument") | ||
|
||
case .notDependencyValues: | ||
MessageID(domain: "DependencyValuesMacroDiagnostic", id: "invalidArgument") | ||
} | ||
} | ||
} | ||
|
||
public extension DependencyValuesMacro { | ||
static func decodeExpansion( | ||
of syntax: AttributeSyntax, | ||
attachedTo declaration: some DeclGroupSyntax, | ||
in context: some MacroExpansionContext | ||
) throws -> (decl: ExtensionDeclSyntax, type: String) { | ||
guard let extensionDecl = declaration.as(ExtensionDeclSyntax.self) else { | ||
if let actorDecl = declaration.as(ActorDeclSyntax.self) { | ||
throw DiagnosticsError( | ||
diagnostics: [ | ||
DependencyValuesMacroDiagnostic.notExtension.diagnose(at: actorDecl.actorKeyword) | ||
] | ||
) | ||
} | ||
else if let classDecl = declaration.as(ClassDeclSyntax.self) { | ||
throw DiagnosticsError( | ||
diagnostics: [ | ||
DependencyValuesMacroDiagnostic.notExtension.diagnose(at: classDecl.classKeyword) | ||
] | ||
) | ||
} | ||
else if let enumDecl = declaration.as(EnumDeclSyntax.self) { | ||
throw DiagnosticsError( | ||
diagnostics: [ | ||
DependencyValuesMacroDiagnostic.notExtension.diagnose(at: enumDecl.enumKeyword) | ||
] | ||
) | ||
} | ||
else if let structDecl = declaration.as(StructDeclSyntax.self) { | ||
throw DiagnosticsError( | ||
diagnostics: [ | ||
DependencyValuesMacroDiagnostic.notExtension.diagnose(at: structDecl.structKeyword) | ||
] | ||
) | ||
} | ||
else { | ||
throw DiagnosticsError( | ||
diagnostics: [ | ||
DependencyValuesMacroDiagnostic.notExtension.diagnose(at: declaration) | ||
] | ||
) | ||
} | ||
} | ||
|
||
guard extensionDecl.extendedType.as(IdentifierTypeSyntax.self)?.name.text == "DependencyValues" else { | ||
throw DiagnosticsError( | ||
diagnostics: [ | ||
DependencyValuesMacroDiagnostic.notDependencyValues.diagnose(at: extensionDecl.extendedType) | ||
] | ||
) | ||
} | ||
|
||
guard case .argumentList(let arguments) = syntax.arguments, | ||
let type = arguments.first?.expression.as(MemberAccessExprSyntax.self)?.base?.as(DeclReferenceExprSyntax.self)?.baseName.text, | ||
arguments.count == 1 | ||
else { | ||
throw DiagnosticsError( | ||
diagnostics: [ | ||
DependencyValuesMacroDiagnostic.invalidArgument.diagnose(at: declaration) | ||
] | ||
) | ||
} | ||
|
||
return (extensionDecl, type) | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
Sources/DependenciesMacroPlugin/DependencyValuesMacro.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,38 @@ | ||
import SwiftSyntax | ||
import SwiftSyntaxBuilder | ||
import SwiftSyntaxMacros | ||
import SwiftDiagnostics | ||
import SwiftCompilerPluginMessageHandling | ||
|
||
public struct DependencyValuesMacro: MemberMacro { | ||
public static func expansion( | ||
of node: AttributeSyntax, | ||
providingMembersOf declaration: some DeclGroupSyntax, | ||
in context: some MacroExpansionContext | ||
) throws -> [DeclSyntax] { | ||
let (_, typeName) = try decodeExpansion( | ||
of: node, | ||
attachedTo: declaration, | ||
in: context | ||
) | ||
|
||
let variableName = typeName.initialLowerCased() | ||
|
||
return [ | ||
DeclSyntax( | ||
""" | ||
var \(raw: variableName): \(raw: typeName) { | ||
get { self[\(raw: typeName).self] } | ||
set { self[\(raw: typeName).self] = newValue } | ||
} | ||
""" | ||
) | ||
] | ||
} | ||
} | ||
|
||
extension String { | ||
func initialLowerCased() -> String { | ||
return prefix(1).lowercased() + dropFirst() | ||
} | ||
} |
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
108 changes: 108 additions & 0 deletions
108
Tests/DependenciesMacroTests/DependencyValueMacroTests.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,108 @@ | ||
import DependenciesMacro | ||
import DependenciesMacroPlugin | ||
import MacroTesting | ||
import XCTest | ||
|
||
final class DependencyValueMacroTests: XCTestCase { | ||
override func invokeTest() { | ||
withMacroTesting( | ||
macros: ["DependencyValue": DependencyValuesMacro.self] | ||
) { | ||
super.invokeTest() | ||
} | ||
} | ||
|
||
func testDiagnostic() { | ||
assertMacro { | ||
""" | ||
@DependencyValue(TestClient.self) | ||
public struct DependencyValues {} | ||
""" | ||
} matches: { | ||
""" | ||
@DependencyValue(TestClient.self) | ||
public struct DependencyValues {} | ||
┬───── | ||
╰─ 🛑 DependencyValue Macro can only be applied to extension. | ||
""" | ||
} | ||
|
||
assertMacro { | ||
""" | ||
@DependencyValue(TestClient.self) | ||
public class DependencyValues {} | ||
""" | ||
} matches: { | ||
""" | ||
@DependencyValue(TestClient.self) | ||
public class DependencyValues {} | ||
┬──── | ||
╰─ 🛑 DependencyValue Macro can only be applied to extension. | ||
""" | ||
} | ||
|
||
assertMacro { | ||
""" | ||
@DependencyValue(TestClient.self) | ||
public actor DependencyValues {} | ||
""" | ||
} matches: { | ||
""" | ||
@DependencyValue(TestClient.self) | ||
public actor DependencyValues {} | ||
┬──── | ||
╰─ 🛑 DependencyValue Macro can only be applied to extension. | ||
""" | ||
} | ||
|
||
assertMacro { | ||
""" | ||
@DependencyValue(TestClient.self) | ||
public enum DependencyValues {} | ||
""" | ||
} matches: { | ||
""" | ||
@DependencyValue(TestClient.self) | ||
public enum DependencyValues {} | ||
┬─── | ||
╰─ 🛑 DependencyValue Macro can only be applied to extension. | ||
""" | ||
} | ||
|
||
assertMacro { | ||
""" | ||
@DependencyValue(TestClient.self) | ||
public extension Test {} | ||
""" | ||
} matches: { | ||
""" | ||
@DependencyValue(TestClient.self) | ||
public extension Test {} | ||
┬─── | ||
╰─ 🛑 DependencyValue Macro can only be applied to extension of DependencyValues | ||
""" | ||
} | ||
} | ||
|
||
func testMacro() { | ||
assertMacro { | ||
""" | ||
@DependencyValue(TestClient.self) | ||
public extension DependencyValues {} | ||
""" | ||
} matches: { | ||
""" | ||
public extension DependencyValues { | ||
var testClient: TestClient { | ||
get { | ||
self [TestClient.self] | ||
} | ||
set { | ||
self [TestClient.self] = newValue | ||
} | ||
}} | ||
""" | ||
} | ||
} | ||
} |