Skip to content

Commit

Permalink
General fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
dfed committed Nov 26, 2023
1 parent 6256267 commit 04cc4d1
Show file tree
Hide file tree
Showing 11 changed files with 260 additions and 152 deletions.
4 changes: 2 additions & 2 deletions Sources/SafeDI/SafeDI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
#else

// TODO: Document macro.
@attached(member, names: named(`init`), named(build), named(getDependencies), arbitrary)
@attached(member, names: arbitrary)
public macro builder(_ propertyName: StaticString) = #externalMacro(module: "SafeDIMacros", type: "BuilderMacro")

// TODO: Document macro.
@attached(member, names: named(`init`))
@attached(member, names: arbitrary)
public macro dependencies() = #externalMacro(module: "SafeDIMacros", type: "DependenciesMacro")

// TODO: Document macro.
Expand Down
19 changes: 19 additions & 0 deletions Sources/SafeDIMacros/Extensions/ArrayExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@ import SwiftSyntaxBuilder

extension Array where Element == Dependency {

var variantUnlabeledParameterList: FunctionParameterListSyntax {
FunctionParameterListSyntax(
filter { $0.source == .variant }
.map { "\(raw: $0.type)" }
.transformUntilLast {
var functionPamameterSyntax = $0
functionPamameterSyntax.trailingComma = TokenSyntax(.comma, presence: .present)
functionPamameterSyntax.trailingTrivia = .space
return functionPamameterSyntax
}
)
}

var variantParameterList: FunctionParameterListSyntax {
FunctionParameterListSyntax(
filter { $0.source == .variant }
Expand All @@ -36,6 +49,12 @@ extension Array where Element == Dependency {
)
}

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

var variantLabeledExpressionList: String {
filter { $0.isVariant }
.map { "\($0.variableName): \($0.variableName)" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import SwiftSyntaxBuilder
extension FunctionDeclSyntax {

static var buildTemplate: Self {
try! FunctionDeclSyntax("public func build(<#T##parameter#>: <#T##ParameterType#>) \(returnClauseTemplate)")
try! FunctionDeclSyntax("""
func build(<#T##parameter#>: <#T##ParameterType#>) \(returnClauseTemplate) {
<#T##ConcreteBuiltProductType#>(<#T##parameter#>: <#T##ParameterType#>)
}
""")
}

static var returnClauseTemplate: ReturnClauseSyntax {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@ extension StructDeclSyntax {

static var dependenciesTemplate: Self {
try! StructDeclSyntax("""
@dependencies public struct Dependencies {
\(FunctionDeclSyntax.buildTemplate) {
<#T##ConcreteBuiltProductType#>(<#T##parameter#>: <#T##ParameterType#>)
}
\(FunctionDeclSyntax.buildTemplate)
private let <#T##dependency#>: <#T##DependencyType#>
}
""")
"""
)
}

}
28 changes: 0 additions & 28 deletions Sources/SafeDIMacros/Extensions/SyntaxExtensions.swift

This file was deleted.

54 changes: 45 additions & 9 deletions Sources/SafeDIMacros/Internal/BuilderVisitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,41 +32,77 @@ final class BuilderVisitor: SyntaxVisitor {
// MARK: SyntaxVisitor

override func visit(_ node: VariableDeclSyntax) -> SyntaxVisitorContinueKind {
guard
let parent = node.parent,
let typedParent = MemberBlockItemSyntax(parent),
let greatGrandparent = parent.parent?.parent,
var modifiedGreatGrandparent = MemberBlockSyntax(greatGrandparent),
let index = modifiedGreatGrandparent.members.index(of: typedParent)
else {
return .skipChildren
}

modifiedGreatGrandparent.members.remove(at: index)

diagnostics.append(Diagnostic(
node: node.modifiers,
node: node,
error: FixableBuilderError.unexpectedVariableDeclaration,
changes: [
.replace(
oldNode: Syntax(node),
newNode: .empty
oldNode: greatGrandparent,
newNode: Syntax(modifiedGreatGrandparent)
)
]
))
return .skipChildren
}

override func visit(_ node: InitializerDeclSyntax) -> SyntaxVisitorContinueKind {
guard
let parent = node.parent,
let typedParent = MemberBlockItemSyntax(parent),
let greatGrandparent = parent.parent?.parent,
var modifiedGreatGrandparent = MemberBlockSyntax(greatGrandparent),
let index = modifiedGreatGrandparent.members.index(of: typedParent)
else {
return .skipChildren
}

modifiedGreatGrandparent.members.remove(at: index)

diagnostics.append(Diagnostic(
node: node.modifiers,
node: node,
error: FixableBuilderError.unexpectedInitializer,
changes: [
.replace(
oldNode: Syntax(node),
newNode: .empty
oldNode: greatGrandparent,
newNode: Syntax(modifiedGreatGrandparent)
)
]
))
return .skipChildren
}

override func visit(_ node: FunctionDeclSyntax) -> SyntaxVisitorContinueKind {
guard
let parent = node.parent,
let typedParent = MemberBlockItemSyntax(parent),
let greatGrandparent = parent.parent?.parent,
var modifiedGreatGrandparent = MemberBlockSyntax(greatGrandparent),
let index = modifiedGreatGrandparent.members.index(of: typedParent)
else {
return .skipChildren
}

modifiedGreatGrandparent.members.remove(at: index)

diagnostics.append(Diagnostic(
node: node.modifiers,
node: node,
error: FixableBuilderError.unexpectedFuncationDeclaration,
changes: [
.replace(
oldNode: Syntax(node),
newNode: .empty
oldNode: greatGrandparent,
newNode: Syntax(modifiedGreatGrandparent)
)
]
))
Expand Down
Loading

0 comments on commit 04cc4d1

Please sign in to comment.