-
-
Notifications
You must be signed in to change notification settings - Fork 4
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
27 changed files
with
2,111 additions
and
203 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,36 @@ | ||
// The Swift Programming Language | ||
// https://docs.swift.org/swift-book | ||
|
||
// TODO: Define macros here. Sample below. | ||
///// A macro that produces both a value and a string containing the | ||
///// source code that generated the value. For example, | ||
///// | ||
///// #stringify(x + y) | ||
///// | ||
///// produces a tuple `(x + y, "x + y")`. | ||
//@freestanding(expression) | ||
//public macro stringify<T>(_ value: T) -> (T, String) = #externalMacro(module: "SafeDIMacros", type: "StringifyMacro") | ||
// Distributed under the MIT License | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
|
||
// TODO: Document macro. | ||
@attached(member, names: named(`init`), named(build), named(getDependencies), arbitrary) | ||
public macro builder(_ propertyName: StaticString) = #externalMacro(module: "SafeDIMacros", type: "BuilderMacro") | ||
|
||
// TODO: Document macro. | ||
@attached(member, names: named(`init`)) | ||
public macro dependencies() = #externalMacro(module: "SafeDIMacros", type: "DependenciesMacro") | ||
|
||
// TODO: Document macro. | ||
@attached(member) | ||
public macro constructed() = #externalMacro(module: "SafeDIMacros", type: "ConstructedMacro") | ||
|
||
// TODO: Document macro. | ||
@attached(member) | ||
public macro singleton() = #externalMacro(module: "SafeDIMacros", type: "SingletonMacro") | ||
|
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,81 @@ | ||
// Distributed under the MIT License | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
|
||
import SwiftSyntax | ||
import SwiftSyntaxBuilder | ||
|
||
extension Array where Element == Dependency { | ||
|
||
var variantParameterList: FunctionParameterListSyntax { | ||
FunctionParameterListSyntax( | ||
filter { $0.source == .variant } | ||
.map { "\(raw: $0.variableName): \(raw: $0.type)" } | ||
.transformUntilLast { | ||
var functionPamameterSyntax = $0 | ||
functionPamameterSyntax.trailingComma = TokenSyntax(.comma, presence: .present) | ||
functionPamameterSyntax.trailingTrivia = .space | ||
return functionPamameterSyntax | ||
} | ||
) | ||
} | ||
|
||
var variantLabeledExpressionList: String { | ||
filter { $0.isVariant } | ||
.map { "\($0.variableName): \($0.variableName)" } | ||
.joined(separator: ", ") | ||
} | ||
|
||
var invariantParameterList: FunctionParameterListSyntax { | ||
FunctionParameterListSyntax( | ||
filter { $0.isInvariant } | ||
.map { "\(raw: $0.variableName): \(raw: $0.type)" } | ||
.transformUntilLast { | ||
var functionPamameterSyntax = $0 | ||
functionPamameterSyntax.trailingComma = TokenSyntax(.comma, presence: .present) | ||
functionPamameterSyntax.trailingTrivia = .space | ||
return functionPamameterSyntax | ||
} | ||
) | ||
} | ||
|
||
var invariantAssignmentExpressionList: String { | ||
""" | ||
\(filter(\.isInvariant) | ||
.map { "self.\($0.variableName) = \($0.variableName)" } | ||
.joined(separator: "\n")) | ||
""" | ||
} | ||
|
||
} | ||
|
||
extension Array { | ||
|
||
/// Returns an array with all of the items in the array except for the last transformed. | ||
/// - Parameter transform: A transforming closure. `transform` accepts an element of this sequence as its parameter and returns a transformed value of the same type. | ||
/// - Returns: An array containing the transformed elements of this sequence, plus the untransfomred last element. | ||
func transformUntilLast(_ transform: (Element) throws -> Element) rethrows -> [Element] { | ||
var arrayToTransform = self | ||
guard let lastItem = arrayToTransform.popLast() else { | ||
// Array is empty. | ||
return self | ||
} | ||
return try arrayToTransform.map { try transform($0) } + [lastItem] | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
Sources/SafeDIMacros/Extensions/AttributeListSyntaxExtensions.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,58 @@ | ||
// Distributed under the MIT License | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
|
||
import SwiftSyntax | ||
|
||
extension AttributeListSyntax { | ||
|
||
var isDecoratedWithDependenciesMacro: Bool { | ||
contains(where: { element in | ||
switch element { | ||
case let .attribute(attribute): | ||
return attribute.attributeName.as(IdentifierTypeSyntax.self)?.name.text == DependenciesMacro.name | ||
case .ifConfigDecl: | ||
return false | ||
} | ||
}) | ||
} | ||
|
||
var attributedNodes: [(attribute: String, node: AttributeListSyntax.Element)] { | ||
compactMap { element in | ||
switch element { | ||
case let .attribute(attribute): | ||
guard let identifierText = attribute.attributeName.as(IdentifierTypeSyntax.self)?.name.text else { | ||
return nil | ||
} | ||
return (attribute: identifierText, node: element) | ||
case .ifConfigDecl: | ||
return nil | ||
} | ||
} | ||
} | ||
|
||
var dependencySources: [(source: Dependency.Source, node: AttributeListSyntax.Element)] { | ||
attributedNodes.compactMap { | ||
guard let source = Dependency.Source.init($0.attribute) else { | ||
return nil | ||
} | ||
return (source: source, node: $0.node) | ||
} | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
Sources/SafeDIMacros/Extensions/AttributeSyntaxArgumentsExtensions.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,40 @@ | ||
// Distributed under the MIT License | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
|
||
import SwiftSyntax | ||
|
||
extension AttributeSyntax.Arguments { | ||
var string: String? { | ||
switch self { | ||
case let .argumentList(labeledExprListSyntax): | ||
return labeledExprListSyntax | ||
.map(\.expression) | ||
.compactMap(StringLiteralExprSyntax.init) | ||
.map(\.segments) | ||
.flatMap { $0 } | ||
.compactMap(StringSegmentSyntax.init) | ||
.map(\.content) | ||
.map(\.text) | ||
.first | ||
default: | ||
return nil | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
Sources/SafeDIMacros/Extensions/DeclModifierListSyntaxExtensions.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,36 @@ | ||
// Distributed under the MIT License | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
|
||
import SwiftSyntax | ||
|
||
extension DeclModifierListSyntax { | ||
|
||
var containsPublic: Bool { | ||
contains(where: { modifier in | ||
modifier.name.text == "public" | ||
}) | ||
} | ||
|
||
var staticModifier: Element? { | ||
first(where: { modifier in | ||
modifier.name.text == "static" | ||
}) | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
Sources/SafeDIMacros/Extensions/FunctionDeclSyntaxExtensions.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,15 @@ | ||
import SwiftSyntax | ||
import SwiftSyntaxBuilder | ||
|
||
extension FunctionDeclSyntax { | ||
|
||
static var buildTemplate: Self { | ||
try! FunctionDeclSyntax("public func build(<#T##parameter#>: <#T##ParameterType#>) \(returnClauseTemplate)") | ||
} | ||
|
||
static var returnClauseTemplate: ReturnClauseSyntax { | ||
ReturnClauseSyntax( | ||
type: TypeSyntax(" <#T##BuiltProductType#>") | ||
) | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
Sources/SafeDIMacros/Extensions/StructDeclSyntaxExtensions.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,18 @@ | ||
import SwiftSyntax | ||
import SwiftSyntaxBuilder | ||
|
||
extension StructDeclSyntax { | ||
|
||
static var dependenciesTemplate: Self { | ||
try! StructDeclSyntax(""" | ||
@dependencies public struct Dependencies { | ||
\(FunctionDeclSyntax.buildTemplate) { | ||
<#T##ConcreteBuiltProductType#>(<#T##parameter#>: <#T##ParameterType#>) | ||
} | ||
private let <#T##dependency#>: <#T##DependencyType#> | ||
} | ||
""") | ||
} | ||
|
||
} |
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 @@ | ||
// Distributed under the MIT License | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
|
||
import SwiftSyntax | ||
|
||
extension Syntax { | ||
|
||
static var empty: Self { | ||
Syntax(CodeBlockSyntax(statements: CodeBlockItemListSyntax())) | ||
} | ||
} |
Oops, something went wrong.