-
Notifications
You must be signed in to change notification settings - Fork 191
@DependencyEntry
#443
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
@DependencyEntry
#443
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
173 changes: 173 additions & 0 deletions
173
Sources/DependenciesMacrosPlugin/DependencyEntryMacro.swift
This file contains hidden or 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,173 @@ | ||
| import SwiftDiagnostics | ||
| public import SwiftSyntax | ||
| import SwiftSyntaxBuilder | ||
| public import SwiftSyntaxMacros | ||
|
|
||
| public enum DependencyEntryMacro {} | ||
|
|
||
| extension DependencyEntryMacro: AccessorMacro { | ||
| public static func expansion( | ||
| of node: AttributeSyntax, | ||
| providingAccessorsOf declaration: some DeclSyntaxProtocol, | ||
| in context: some MacroExpansionContext | ||
| ) throws -> [AccessorDeclSyntax] { | ||
| guard | ||
| isInDependencyValuesExtension(context: context), | ||
| let property = declaration.as(VariableDeclSyntax.self), | ||
| property.bindingSpecifier.tokenKind == .keyword(.var), | ||
| let identifier = property.bindings.first?.pattern | ||
| .as(IdentifierPatternSyntax.self)?.identifier.trimmed | ||
| else { | ||
| return [] | ||
| } | ||
| let keyName: TokenSyntax = "__Key_\(identifier)" | ||
| return [ | ||
| """ | ||
| get { self[\(keyName).self] } | ||
| """, | ||
| """ | ||
| set { self[\(keyName).self] = newValue } | ||
| """, | ||
| ] | ||
| } | ||
| } | ||
|
|
||
| extension DependencyEntryMacro: PeerMacro { | ||
| public static func expansion( | ||
| of node: AttributeSyntax, | ||
| providingPeersOf declaration: some DeclSyntaxProtocol, | ||
| in context: some MacroExpansionContext | ||
| ) throws -> [DeclSyntax] { | ||
| guard | ||
| let property = declaration.as(VariableDeclSyntax.self), | ||
| property.bindingSpecifier.tokenKind == .keyword(.var), | ||
| isInDependencyValuesExtension(context: context) | ||
| else { | ||
| context.diagnose( | ||
| Diagnostic( | ||
| node: node, | ||
| message: MacroExpansionErrorMessage( | ||
| """ | ||
| '@DependencyEntry' macro can only attach to 'var' declarations inside extensions of \ | ||
| 'DependencyValues' | ||
| """ | ||
| ) | ||
| ) | ||
| ) | ||
| return [] | ||
| } | ||
|
|
||
| guard | ||
| let binding = property.bindings.first, | ||
| let identifier = binding.pattern.as(IdentifierPatternSyntax.self)?.identifier.trimmed | ||
| else { | ||
| return [] | ||
| } | ||
|
|
||
| var liveValueExpr: ExprSyntax? | ||
| var previewValueExpr: ExprSyntax? | ||
| if let arguments = node.arguments?.as(LabeledExprListSyntax.self) { | ||
| for argument in arguments { | ||
| switch argument.label?.text { | ||
| case "liveValue": | ||
| liveValueExpr = argument.expression | ||
| case "previewValue": | ||
| previewValueExpr = argument.expression | ||
| default: | ||
| break | ||
| } | ||
| } | ||
| } | ||
|
|
||
| let testValueExpr: ExprSyntax? = binding.initializer?.value | ||
| if testValueExpr == nil, liveValueExpr == nil { | ||
| context.diagnose( | ||
| Diagnostic( | ||
| node: declaration, | ||
| message: MacroExpansionErrorMessage( | ||
| """ | ||
| '@DependencyEntry' requires an initializer to define the property's test value, or a \ | ||
| 'liveValue' argument to fall back on | ||
| """ | ||
| ) | ||
| ) | ||
| ) | ||
| return [] | ||
| } | ||
|
|
||
| let conformance = liveValueExpr != nil ? "DependencyKey" : "TestDependencyKey" | ||
| let keyName: TokenSyntax = "__Key_\(identifier)" | ||
|
|
||
| var members: [String] = [] | ||
| if let typeAnnotation = binding.typeAnnotation?.type.trimmed { | ||
| members.append("typealias Value = \(typeAnnotation)") | ||
| if let liveValueExpr { | ||
| members.append("static var liveValue: Value { \(liveValueExpr) }") | ||
| } | ||
| if let previewValueExpr { | ||
| members.append("static var previewValue: Value { \(previewValueExpr) }") | ||
| } | ||
| if let testValueExpr { | ||
| members.append("static var testValue: Value { \(testValueExpr) }") | ||
| } | ||
| } else { | ||
| let attribute = "@DependenciesMacros._DependencyEntryDefaultValue" | ||
| if let liveValueExpr { | ||
| members.append("\(attribute) static var liveValue = \(liveValueExpr)") | ||
| } | ||
| if let previewValueExpr { | ||
| members.append("\(attribute) static var previewValue = \(previewValueExpr)") | ||
| } | ||
| if let testValueExpr { | ||
| members.append("\(attribute) static var testValue = \(testValueExpr)") | ||
| } | ||
| } | ||
|
|
||
| let body = members.joined(separator: "\n") | ||
| let keyDecl: DeclSyntax = """ | ||
| private enum \(keyName): Dependencies.\(raw: conformance) { | ||
| \(raw: body) | ||
| } | ||
| """ | ||
| return [keyDecl] | ||
| } | ||
| } | ||
|
|
||
| private func isInDependencyValuesExtension( | ||
| context: some MacroExpansionContext | ||
| ) -> Bool { | ||
| guard | ||
| let extensionDecl = context.lexicalContext.first?.as(ExtensionDeclSyntax.self) | ||
| else { | ||
| return false | ||
| } | ||
| let extendedType = extensionDecl.extendedType | ||
| let name: String? | ||
| if let identifier = extendedType.as(IdentifierTypeSyntax.self) { | ||
| name = identifier.name.text | ||
| } else if let member = extendedType.as(MemberTypeSyntax.self) { | ||
| name = member.name.text | ||
| } else { | ||
| name = nil | ||
| } | ||
| return name == "DependencyValues" | ||
| } | ||
|
|
||
| public enum DependencyEntryDefaultValueMacro {} | ||
|
|
||
| extension DependencyEntryDefaultValueMacro: AccessorMacro { | ||
| public static func expansion( | ||
| of node: AttributeSyntax, | ||
| providingAccessorsOf declaration: some DeclSyntaxProtocol, | ||
| in context: some MacroExpansionContext | ||
| ) throws -> [AccessorDeclSyntax] { | ||
| guard | ||
| let property = declaration.as(VariableDeclSyntax.self), | ||
| let binding = property.bindings.first, | ||
| let initializer = binding.initializer?.value | ||
| else { | ||
| return [] | ||
| } | ||
| return ["get { \(initializer) }"] | ||
| } | ||
| } |
This file contains hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Defining this in
DependenciesMacrosfor now, but we could probably flatten these modules soon.