diff --git a/Plugins/SafeDIGenerator/SafeDIGenerateDependencyTree.swift b/Plugins/SafeDIGenerator/SafeDIGenerateDependencyTree.swift index 62a9d18..6ab8e53 100644 --- a/Plugins/SafeDIGenerator/SafeDIGenerateDependencyTree.swift +++ b/Plugins/SafeDIGenerator/SafeDIGenerateDependencyTree.swift @@ -108,8 +108,7 @@ extension Target { } // We only care about first-party code. Ignore third-party dependencies. - guard - swiftModule + guard swiftModule .directoryURL .pathComponents // Removing the module name. diff --git a/Sources/SafeDICore/Extensions/AttributeListSyntaxElementExtensions.swift b/Sources/SafeDICore/Extensions/AttributeListSyntaxElementExtensions.swift index e7c0097..a1e2cd0 100644 --- a/Sources/SafeDICore/Extensions/AttributeListSyntaxElementExtensions.swift +++ b/Sources/SafeDICore/Extensions/AttributeListSyntaxElementExtensions.swift @@ -38,10 +38,9 @@ extension AttributeListSyntax.Element { } private func attributeIfNameEquals(_ expectedName: String) -> AttributeSyntax? { - if - case let .attribute(attribute) = self, - let identifier = IdentifierTypeSyntax(attribute.attributeName), - identifier.name.text == expectedName + if case let .attribute(attribute) = self, + let identifier = IdentifierTypeSyntax(attribute.attributeName), + identifier.name.text == expectedName { attribute } else { diff --git a/Sources/SafeDICore/Extensions/AttributeSyntaxExtensions.swift b/Sources/SafeDICore/Extensions/AttributeSyntaxExtensions.swift index 2165ae5..bf9429e 100644 --- a/Sources/SafeDICore/Extensions/AttributeSyntaxExtensions.swift +++ b/Sources/SafeDICore/Extensions/AttributeSyntaxExtensions.swift @@ -22,11 +22,10 @@ import SwiftSyntax extension AttributeSyntax { public var fulfillingAdditionalTypes: ExprSyntax? { - guard - let arguments, - let labeledExpressionList = LabeledExprListSyntax(arguments), - let firstLabeledExpression = labeledExpressionList.first, - firstLabeledExpression.label?.text == "fulfillingAdditionalTypes" + guard let arguments, + let labeledExpressionList = LabeledExprListSyntax(arguments), + let firstLabeledExpression = labeledExpressionList.first, + firstLabeledExpression.label?.text == "fulfillingAdditionalTypes" else { return nil } @@ -35,11 +34,10 @@ extension AttributeSyntax { } public var fulfilledByDependencyNamed: ExprSyntax? { - guard - let arguments, - let labeledExpressionList = LabeledExprListSyntax(arguments), - let firstLabeledExpression = labeledExpressionList.first, - firstLabeledExpression.label?.text == "fulfilledByDependencyNamed" + guard let arguments, + let labeledExpressionList = LabeledExprListSyntax(arguments), + let firstLabeledExpression = labeledExpressionList.first, + firstLabeledExpression.label?.text == "fulfilledByDependencyNamed" else { return nil } @@ -48,9 +46,8 @@ extension AttributeSyntax { } public var fulfillingPropertyName: String? { - guard - let fulfilledByDependencyNamed, - let stringLiteral = StringLiteralExprSyntax(fulfilledByDependencyNamed) + guard let fulfilledByDependencyNamed, + let stringLiteral = StringLiteralExprSyntax(fulfilledByDependencyNamed) else { return nil } @@ -59,11 +56,10 @@ extension AttributeSyntax { } public var fulfilledByType: ExprSyntax? { - guard - let arguments, - let labeledExpressionList = LabeledExprListSyntax(arguments), - let firstLabeledExpression = labeledExpressionList.first, - firstLabeledExpression.label?.text == "fulfilledByType" + guard let arguments, + let labeledExpressionList = LabeledExprListSyntax(arguments), + let firstLabeledExpression = labeledExpressionList.first, + firstLabeledExpression.label?.text == "fulfilledByType" else { return nil } @@ -72,11 +68,10 @@ extension AttributeSyntax { } public var ofType: ExprSyntax? { - guard - let arguments, - let labeledExpressionList = LabeledExprListSyntax(arguments), - let expectedOfTypeLabeledExpression = labeledExpressionList.dropFirst().first, - expectedOfTypeLabeledExpression.label?.text == "ofType" + guard let arguments, + let labeledExpressionList = LabeledExprListSyntax(arguments), + let expectedOfTypeLabeledExpression = labeledExpressionList.dropFirst().first, + expectedOfTypeLabeledExpression.label?.text == "ofType" else { return nil } @@ -85,14 +80,13 @@ extension AttributeSyntax { } public var erasedToConcreteExistential: ExprSyntax? { - guard - let arguments, - let labeledExpressionList = LabeledExprListSyntax(arguments), - let erasedToConcreteExistentialLabeledExpression = labeledExpressionList.dropFirst().first(where: { - // In `@Instantiated`, the `erasedToConcreteExistential` parameter is the second parameter. - // In `@Received`, the `erasedToConcreteExistential` parameter is the third parameter. - $0.label?.text == "erasedToConcreteExistential" - }) + guard let arguments, + let labeledExpressionList = LabeledExprListSyntax(arguments), + let erasedToConcreteExistentialLabeledExpression = labeledExpressionList.dropFirst().first(where: { + // In `@Instantiated`, the `erasedToConcreteExistential` parameter is the second parameter. + // In `@Received`, the `erasedToConcreteExistential` parameter is the third parameter. + $0.label?.text == "erasedToConcreteExistential" + }) else { return nil } @@ -101,10 +95,9 @@ extension AttributeSyntax { } public var fulfillingTypeDescription: TypeDescription? { - if - let expression = fulfilledByType, - let stringLiteral = StringLiteralExprSyntax(expression), - let firstStringSegement = stringLiteral.segments.firstStringSegment + if let expression = fulfilledByType, + let stringLiteral = StringLiteralExprSyntax(expression), + let firstStringSegement = stringLiteral.segments.firstStringSegment { TypeSyntax(stringLiteral: firstStringSegement).typeDescription } else { @@ -113,9 +106,8 @@ extension AttributeSyntax { } public var erasedToConcreteExistentialType: Bool { - guard - let erasedToConcreteExistential, - let erasedToConcreteExistentialType = BooleanLiteralExprSyntax(erasedToConcreteExistential) + guard let erasedToConcreteExistential, + let erasedToConcreteExistentialType = BooleanLiteralExprSyntax(erasedToConcreteExistential) else { // Default value for the `erasedToConcreteExistential` parameter is `false`. return false diff --git a/Sources/SafeDICore/Generators/ScopeGenerator.swift b/Sources/SafeDICore/Generators/ScopeGenerator.swift index 8197ea6..3807569 100644 --- a/Sources/SafeDICore/Generators/ScopeGenerator.swift +++ b/Sources/SafeDICore/Generators/ScopeGenerator.swift @@ -141,18 +141,17 @@ actor ScopeGenerator: CustomStringConvertible, Sendable { let returnLineSansReturn = "\(instantiationDeclaration)(\(argumentList))" let propertyType = property.propertyType - if - propertyType.isErasedInstantiator, - let firstForwardedProperty = forwardedProperties.first, - let forwardedArgument = property.generics?.first, - !( - // The forwarded argument is the same type as our only `@Forwarded` property. - (forwardedProperties.count == 1 && forwardedArgument == firstForwardedProperty.typeDescription) - // The forwarded argument is the same as `InstantiableName.ForwardedProperties`. - || forwardedArgument == .nested(name: "ForwardedProperties", parentType: instantiable.concreteInstantiable) - // The forwarded argument is the same as the tuple we generated for `InstantiableName.ForwardedProperties`. - || forwardedArgument == forwardedProperties.asTupleTypeDescription - ) + if propertyType.isErasedInstantiator, + let firstForwardedProperty = forwardedProperties.first, + let forwardedArgument = property.generics?.first, + !( + // The forwarded argument is the same type as our only `@Forwarded` property. + (forwardedProperties.count == 1 && forwardedArgument == firstForwardedProperty.typeDescription) + // The forwarded argument is the same as `InstantiableName.ForwardedProperties`. + || forwardedArgument == .nested(name: "ForwardedProperties", parentType: instantiable.concreteInstantiable) + // The forwarded argument is the same as the tuple we generated for `InstantiableName.ForwardedProperties`. + || forwardedArgument == forwardedProperties.asTupleTypeDescription + ) { throw GenerationError.erasedInstantiatorGenericDoesNotMatch( property: property, @@ -368,9 +367,8 @@ actor ScopeGenerator: CustomStringConvertible, Sendable { } } func fulfill(_ scope: ScopeGenerator) { - guard - let property = scope.property, - propertyToUnfulfilledScopeMap[property] != nil + guard let property = scope.property, + propertyToUnfulfilledScopeMap[property] != nil else { return } diff --git a/Sources/SafeDICore/Models/Dependency.swift b/Sources/SafeDICore/Models/Dependency.swift index 396c76a..a7fb0eb 100644 --- a/Sources/SafeDICore/Models/Dependency.swift +++ b/Sources/SafeDICore/Models/Dependency.swift @@ -63,9 +63,8 @@ public struct Dependency: Codable, Hashable, Sendable { erasedToConcreteExistential: instantiatedMacro.erasedToConcreteExistentialType ) } else if let receivedMacro = node.receivedMacro { - if - let fulfillingPropertyName = receivedMacro.fulfillingPropertyName, - let fulfillingTypeDescription = receivedMacro.fulfillingTypeDescription + if let fulfillingPropertyName = receivedMacro.fulfillingPropertyName, + let fulfillingTypeDescription = receivedMacro.fulfillingTypeDescription { self = .aliased( fulfillingProperty: Property( diff --git a/Sources/SafeDICore/Visitors/InstantiableVisitor.swift b/Sources/SafeDICore/Visitors/InstantiableVisitor.swift index d76d184..57f25ec 100644 --- a/Sources/SafeDICore/Visitors/InstantiableVisitor.swift +++ b/Sources/SafeDICore/Visitors/InstantiableVisitor.swift @@ -36,9 +36,8 @@ public final class InstantiableVisitor: SyntaxVisitor { // MARK: SyntaxVisitor public override func visit(_ node: VariableDeclSyntax) -> SyntaxVisitorContinueKind { - guard - declarationType.isTypeDefinition, - node.modifiers.staticModifier == nil + guard declarationType.isTypeDefinition, + node.modifiers.staticModifier == nil else { return .skipChildren } @@ -93,9 +92,8 @@ public final class InstantiableVisitor: SyntaxVisitor { )) } - if - let label = IdentifierPatternSyntax(binding.pattern)?.identifier.text, - let typeDescription = binding.typeAnnotation?.type.typeDescription + if let label = IdentifierPatternSyntax(binding.pattern)?.identifier.text, + let typeDescription = binding.typeAnnotation?.type.typeDescription { dependencies.append( Dependency( @@ -380,9 +378,8 @@ public final class InstantiableVisitor: SyntaxVisitor { } private func processAttributes(_: AttributeListSyntax, on macro: AttributeSyntax) { - guard - let fulfillingAdditionalTypesExpression = macro.fulfillingAdditionalTypes, - let fulfillingAdditionalTypesArray = ArrayExprSyntax(fulfillingAdditionalTypesExpression) + guard let fulfillingAdditionalTypesExpression = macro.fulfillingAdditionalTypes, + let fulfillingAdditionalTypesArray = ArrayExprSyntax(fulfillingAdditionalTypesExpression) else { // Nothing to do here. return diff --git a/Sources/SafeDIMacros/Macros/InjectableMacro.swift b/Sources/SafeDIMacros/Macros/InjectableMacro.swift index 1de95a3..5327699 100644 --- a/Sources/SafeDIMacros/Macros/InjectableMacro.swift +++ b/Sources/SafeDIMacros/Macros/InjectableMacro.swift @@ -49,10 +49,9 @@ public struct InjectableMacro: PeerMacro { throw InjectableError.fulfilledByTypeUseOnInstantiator } - if - let stringLiteralExpression = StringLiteralExprSyntax(fulfilledByType), - stringLiteralExpression.segments.count == 1, - let stringLiteral = stringLiteralExpression.segments.firstStringSegment + if let stringLiteralExpression = StringLiteralExprSyntax(fulfilledByType), + stringLiteralExpression.segments.count == 1, + let stringLiteral = stringLiteralExpression.segments.firstStringSegment { switch TypeSyntax(stringLiteral: stringLiteral).typeDescription { case .simple, .nested: @@ -74,9 +73,8 @@ public struct InjectableMacro: PeerMacro { } if let fulfilledByDependencyNamed = macroWithParameters?.fulfilledByDependencyNamed { - guard - let stringLiteralExpression = StringLiteralExprSyntax(fulfilledByDependencyNamed), - stringLiteralExpression.segments.count == 1 + guard let stringLiteralExpression = StringLiteralExprSyntax(fulfilledByDependencyNamed), + stringLiteralExpression.segments.count == 1 else { throw InjectableError.fulfilledByDependencyNamedInvalidType } diff --git a/Sources/SafeDIMacros/Macros/InstantiableMacro.swift b/Sources/SafeDIMacros/Macros/InstantiableMacro.swift index 8eb8756..7553976 100644 --- a/Sources/SafeDIMacros/Macros/InstantiableMacro.swift +++ b/Sources/SafeDIMacros/Macros/InstantiableMacro.swift @@ -30,10 +30,10 @@ public struct InstantiableMacro: MemberMacro { providingMembersOf declaration: some DeclGroupSyntax, in context: some MacroExpansionContext ) throws -> [DeclSyntax] { - if - let fulfillingAdditionalTypesArgument = ( - declaration.attributes.instantiableMacro - )?.fulfillingAdditionalTypes + if let fulfillingAdditionalTypesArgument = declaration + .attributes + .instantiableMacro? + .fulfillingAdditionalTypes { if let arrayExpression = ArrayExprSyntax(fulfillingAdditionalTypesArgument) { if arrayExpression @@ -47,8 +47,7 @@ public struct InstantiableMacro: MemberMacro { } } - if - let concreteDeclaration: ConcreteDeclSyntaxProtocol + if let concreteDeclaration: ConcreteDeclSyntaxProtocol = ActorDeclSyntax(declaration) ?? ClassDeclSyntax(declaration) ?? StructDeclSyntax(declaration)