Skip to content

Commit

Permalink
Reduce whitespace usage (#124)
Browse files Browse the repository at this point in the history
  • Loading branch information
dfed authored Dec 27, 2024
1 parent b92530f commit 4805a13
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 84 deletions.
3 changes: 1 addition & 2 deletions Plugins/SafeDIGenerator/SafeDIGenerateDependencyTree.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
68 changes: 30 additions & 38 deletions Sources/SafeDICore/Extensions/AttributeSyntaxExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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 {
Expand All @@ -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
Expand Down
28 changes: 13 additions & 15 deletions Sources/SafeDICore/Generators/ScopeGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
}
Expand Down
5 changes: 2 additions & 3 deletions Sources/SafeDICore/Models/Dependency.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
15 changes: 6 additions & 9 deletions Sources/SafeDICore/Visitors/InstantiableVisitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down
12 changes: 5 additions & 7 deletions Sources/SafeDIMacros/Macros/InjectableMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
}
Expand Down
11 changes: 5 additions & 6 deletions Sources/SafeDIMacros/Macros/InstantiableMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -47,8 +47,7 @@ public struct InstantiableMacro: MemberMacro {
}
}

if
let concreteDeclaration: ConcreteDeclSyntaxProtocol
if let concreteDeclaration: ConcreteDeclSyntaxProtocol
= ActorDeclSyntax(declaration)
?? ClassDeclSyntax(declaration)
?? StructDeclSyntax(declaration)
Expand Down

0 comments on commit 4805a13

Please sign in to comment.