diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6c63b82..e92a826 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -98,6 +98,6 @@ jobs: - name: Check Swift Syntax Compatibility uses: davdroman/swift-syntax-compatibility-check@v1 with: - run-tests: false + run-tests: true major-versions-only: true disable-prebuilts: true diff --git a/.swiftformat b/.swiftformat index 3c33396..5f8bbc9 100644 --- a/.swiftformat +++ b/.swiftformat @@ -3,15 +3,25 @@ --header ignore --indent tab +--tabwidth 2 +--maxwidth 120 --ifdef no-indent --ranges preserve --extensionacl on-declarations --trailing-commas always --nil-init insert --import-grouping alpha +--wrap-arguments before-first --wrap-conditions before-first +--wrap-parameters before-first +--wrap-return-type never --guard-else next-line +# Opt-in rules +--enable blockComments +--enable wrapMultilineConditionalAssignment +--enable wrapMultilineFunctionChains + # Disabled rules --disable blankLinesAroundMark --disable blankLinesBetweenScopes diff --git a/Sources/AssociationMacro/AssociatedMacro.swift b/Sources/AssociationMacro/AssociatedMacro.swift index a0e7400..fb153ed 100644 --- a/Sources/AssociationMacro/AssociatedMacro.swift +++ b/Sources/AssociationMacro/AssociatedMacro.swift @@ -221,13 +221,14 @@ extension AssociatedMacro { policy: ExprSyntax, defaultValue: ExprSyntax?, ) -> AccessorDeclSyntax { - let typeWithoutOptional = if let type = type.as(ImplicitlyUnwrappedOptionalTypeSyntax.self) { - type.wrappedType - } else if let type = type.as(OptionalTypeSyntax.self) { - type.wrappedType - } else { - type - } + let typeWithoutOptional = + if let type = type.as(ImplicitlyUnwrappedOptionalTypeSyntax.self) { + type.wrappedType + } else if let type = type.as(OptionalTypeSyntax.self) { + type.wrappedType + } else { + type + } return AccessorDeclSyntax( accessorSpecifier: .keyword(.get), @@ -345,7 +346,8 @@ extension AssociatedMacro { type: type, accessor: didSet, body: body, - ).with(\.leadingTrivia, .newlines(2)) + ) + .with(\.leadingTrivia, .newlines(2)) Self.callDidSet() } @@ -353,6 +355,22 @@ extension AssociatedMacro { ) } + private static var selfCapture: ClosureCaptureSyntax { + let selfReference = DeclReferenceExprSyntax(baseName: .keyword(.`self`)) + #if canImport(SwiftSyntax601) + return ClosureCaptureSyntax( + name: .keyword(.`self`), + initializer: .init(value: selfReference), + ) + #else + return ClosureCaptureSyntax( + name: .keyword(.`self`), + equal: .equalToken(), + expression: selfReference, + ) + #endif + } + /// `willSet` closure /// /// Convert a willSet accessor to a closure variable in the following format. @@ -393,10 +411,7 @@ extension AssociatedMacro { value: ClosureExprSyntax( signature: .init( capture: .init { - ClosureCaptureSyntax( - name: .keyword(.`self`), - expression: DeclReferenceExprSyntax(baseName: .keyword(.`self`)), - ) + Self.selfCapture }, parameterClause: .init(ClosureShorthandParameterListSyntax { ClosureShorthandParameterSyntax(name: newValue) @@ -450,10 +465,7 @@ extension AssociatedMacro { value: ClosureExprSyntax( signature: .init( capture: .init { - ClosureCaptureSyntax( - name: .keyword(.`self`), - expression: DeclReferenceExprSyntax(baseName: .keyword(.`self`)), - ) + Self.selfCapture }, parameterClause: .init(ClosureShorthandParameterListSyntax { ClosureShorthandParameterSyntax(name: oldValue) diff --git a/Sources/Swizzling/AnyHook.swift b/Sources/Swizzling/AnyHook.swift index fd8a919..8237e93 100644 --- a/Sources/Swizzling/AnyHook.swift +++ b/Sources/Swizzling/AnyHook.swift @@ -81,7 +81,9 @@ public class AnyHook { /// Validate that the selector exists on the active class. @discardableResult func validate(expectedState: State = .prepared) throws -> Method { - guard let method = class_getInstanceMethod(`class`, selector) else { throw SwizzlingError.methodNotFound(`class`, selector) } + guard let method = class_getInstanceMethod(`class`, selector) else { + throw SwizzlingError.methodNotFound(`class`, selector) + } guard state == expectedState else { throw SwizzlingError.invalidState(expectedState: expectedState) } return method } diff --git a/Sources/Swizzling/SwizzlingHook.swift b/Sources/Swizzling/SwizzlingHook.swift index 49d5281..fd84aa8 100644 --- a/Sources/Swizzling/SwizzlingHook.swift +++ b/Sources/Swizzling/SwizzlingHook.swift @@ -28,7 +28,8 @@ final class SwizzlingHook: TypedHook) -> HookSignature?, // this must be optional or swift runtime will crash. Or swiftc may segfault. Compiler bug? + // `HookSignature?` must be optional or the Swift runtime will crash. `swiftc` may also segfault. Compiler bug? + implementation: (SwizzlingHook) -> HookSignature?, ) throws { try super.init(class: `class`, selector: selector) replacementIMP = imp_implementationWithBlock(implementation(self) as Any) @@ -45,7 +46,9 @@ final class SwizzlingHook: TypedHook