Skip to content

Commit

Permalink
Introduce Property
Browse files Browse the repository at this point in the history
  • Loading branch information
dfed committed Nov 26, 2023
1 parent d586e66 commit c700705
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 15 deletions.
12 changes: 6 additions & 6 deletions Sources/SafeDIMacros/Extensions/ArrayExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ extension Array where Element == Dependency {
var variantUnlabeledParameterList: FunctionParameterListSyntax {
FunctionParameterListSyntax(
filter { $0.source == .variant }
.map { "\(raw: $0.type)" }
.map { "\(raw: $0.property.type)" }
.transformUntilLast {
var functionPamameterSyntax = $0
functionPamameterSyntax.trailingComma = TokenSyntax(.comma, presence: .present)
Expand All @@ -39,7 +39,7 @@ extension Array where Element == Dependency {
var variantParameterList: FunctionParameterListSyntax {
FunctionParameterListSyntax(
filter { $0.source == .variant }
.map { "\(raw: $0.variableName): \(raw: $0.type)" }
.map { "\(raw: $0.property.label): \(raw: $0.property.type)" }
.transformUntilLast {
var functionPamameterSyntax = $0
functionPamameterSyntax.trailingComma = TokenSyntax(.comma, presence: .present)
Expand All @@ -51,20 +51,20 @@ extension Array where Element == Dependency {

var variantUnlabeledExpressionList: String {
filter { $0.isVariant }
.map { "\($0.variableName)" }
.map { "\($0.property.label)" }
.joined(separator: ", ")
}

var variantLabeledExpressionList: String {
filter { $0.isVariant }
.map { "\($0.variableName): \($0.variableName)" }
.map { "\($0.property.label): \($0.property.label)" }
.joined(separator: ", ")
}

var invariantParameterList: FunctionParameterListSyntax {
FunctionParameterListSyntax(
filter { $0.isInvariant }
.map { "\(raw: $0.variableName): \(raw: $0.type)" }
.map { "\(raw: $0.property.label): \(raw: $0.property.type)" }
.transformUntilLast {
var functionPamameterSyntax = $0
functionPamameterSyntax.trailingComma = TokenSyntax(.comma, presence: .present)
Expand All @@ -77,7 +77,7 @@ extension Array where Element == Dependency {
var invariantAssignmentExpressionList: String {
"""
\(filter(\.isInvariant)
.map { "self.\($0.variableName) = \($0.variableName)" }
.map { "self.\($0.property.label) = \($0.property.label)" }
.joined(separator: "\n"))
"""
}
Expand Down
18 changes: 11 additions & 7 deletions Sources/SafeDIMacros/Internal/DependenciesVisitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,15 @@ final class DependenciesVisitor: SyntaxVisitor {
}

if
let variableName = IdentifierPatternSyntax(binding.pattern)?.identifier.text,
let label = IdentifierPatternSyntax(binding.pattern)?.identifier.text,
let type = binding.typeAnnotation?.type
{
addDependency(
Dependency(
variableName: variableName,
type: type.description,
property: Property(
label: label,
type: type.description
),
source: dependencySource
),
derivedFrom: Syntax(node)
Expand Down Expand Up @@ -212,8 +214,10 @@ final class DependenciesVisitor: SyntaxVisitor {
for parameter in node.signature.parameterClause.parameters {
addDependency(
Dependency(
variableName: parameter.secondName?.text ?? parameter.firstName.text,
type: parameter.type.trimmedDescription,
property: Property(
label: parameter.secondName?.text ?? parameter.firstName.text,
type: parameter.type.trimmedDescription
),
source: .variant
),
derivedFrom: Syntax(parameter)
Expand Down Expand Up @@ -304,7 +308,7 @@ final class DependenciesVisitor: SyntaxVisitor {
private var dependencyVariableNames = Set<String>()

private func addDependency(_ dependency: Dependency, derivedFrom node: Syntax) {
guard !dependencyVariableNames.contains(dependency.variableName) else {
guard !dependencyVariableNames.contains(dependency.property.label) else {
if
let typedNode = FunctionParameterSyntax(node),
let parent = node.parent,
Expand Down Expand Up @@ -346,7 +350,7 @@ final class DependenciesVisitor: SyntaxVisitor {
}
return
}
dependencyVariableNames.insert(dependency.variableName)
dependencyVariableNames.insert(dependency.property.label)
dependencies.append(dependency)
}
}
3 changes: 1 addition & 2 deletions Sources/SafeDIMacros/Internal/Dependency.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
// SOFTWARE.

struct Dependency: Codable, Equatable {
let variableName: String
let type: String
let property: Property
let source: Source

var isVariant: Bool {
Expand Down
28 changes: 28 additions & 0 deletions Sources/SafeDIMacros/Internal/Property.swift
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.

/// A representation of a property.
/// e.g. `let myDependency: MyDependency`
struct Property: Codable, Equatable {
/// The label by which the property is referenced.
let label: String
/// The type to which the property conforms.
let type: String
}

0 comments on commit c700705

Please sign in to comment.