diff --git a/Package.resolved b/Package.resolved index 6fc5ad6..0dd993f 100644 --- a/Package.resolved +++ b/Package.resolved @@ -5,8 +5,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/apple/swift-collections", "state" : { - "revision" : "d029d9d39c87bed85b1c50adee7c41795261a192", - "version" : "1.0.6" + "revision" : "94cf62b3ba8d4bed62680a282d4c25f9c63c2efb", + "version" : "1.1.0" } }, { @@ -14,8 +14,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/pointfreeco/swift-macro-testing", "state" : { - "revision" : "35acd9468d40ae87e75991a18af6271e8124c261", - "version" : "0.2.1" + "revision" : "90e38eec4bf661ec0da1bbfd3ec507d0f0c05310", + "version" : "0.3.0" } }, { @@ -23,17 +23,17 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/pointfreeco/swift-snapshot-testing", "state" : { - "revision" : "bb0ea08db8e73324fe6c3727f755ca41a23ff2f4", - "version" : "1.14.2" + "revision" : "5b0c434778f2c1a4c9b5ebdb8682b28e84dd69bd", + "version" : "1.15.4" } }, { "identity" : "swift-syntax", "kind" : "remoteSourceControl", - "location" : "https://github.com/apple/swift-syntax.git", + "location" : "https://github.com/apple/swift-syntax", "state" : { - "revision" : "74203046135342e4a4a627476dd6caf8b28fe11b", - "version" : "509.0.0" + "revision" : "08a2f0a9a30e0f705f79c9cfaca1f68b71bdc775", + "version" : "510.0.0" } }, { @@ -41,8 +41,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/pointfreeco/xctest-dynamic-overlay", "state" : { - "revision" : "23cbf2294e350076ea4dbd7d5d047c1e76b03631", - "version" : "1.0.2" + "revision" : "b13b1d1a8e787a5ffc71ac19dcaf52183ab27ba2", + "version" : "1.1.1" } } ], diff --git a/Package.swift b/Package.swift index eae49f6..63ef8c3 100644 --- a/Package.swift +++ b/Package.swift @@ -15,7 +15,7 @@ let package = Package( .library(name: "Perception", targets: ["Perception"]) ], dependencies: [ - .package(url: "https://github.com/apple/swift-syntax", from: "509.0.0"), + .package(url: "https://github.com/apple/swift-syntax", "509.0.0"..<"511.0.0"), .package(url: "https://github.com/apple/swift-collections", from: "1.0.0"), .package(url: "https://github.com/pointfreeco/swift-macro-testing", from: "0.1.0"), .package(url: "https://github.com/pointfreeco/xctest-dynamic-overlay", from: "1.0.0"), diff --git a/Sources/PerceptionMacros/Extensions.swift b/Sources/PerceptionMacros/Extensions.swift index 996288f..6e8e72d 100644 --- a/Sources/PerceptionMacros/Extensions.swift +++ b/Sources/PerceptionMacros/Extensions.swift @@ -41,7 +41,7 @@ extension VariableDeclSyntax { func accessorsMatching(_ predicate: (TokenKind) -> Bool) -> [AccessorDeclSyntax] { let patternBindings = bindings.compactMap { binding in - binding.as(PatternBindingSyntax.self) + binding } let accessors: [AccessorDeclListSyntax.Element] = patternBindings.compactMap { patternBinding in switch patternBinding.accessorBlock?.accessors { @@ -52,14 +52,7 @@ extension VariableDeclSyntax { } }.flatMap { $0 } return accessors.compactMap { accessor in - guard let decl = accessor.as(AccessorDeclSyntax.self) else { - return nil - } - if predicate(decl.accessorSpecifier.tokenKind) { - return decl - } else { - return nil - } + predicate(accessor.accessorSpecifier.tokenKind) ? accessor : nil } } @@ -215,7 +208,7 @@ extension DeclGroupSyntax { var memberFunctionStandins: [FunctionDeclSyntax.SignatureStandin] { var standins = [FunctionDeclSyntax.SignatureStandin]() for member in memberBlock.members { - if let function = member.as(MemberBlockItemSyntax.self)?.decl.as(FunctionDeclSyntax.self) { + if let function = member.decl.as(FunctionDeclSyntax.self) { standins.append(function.signatureStandin) } } @@ -224,7 +217,7 @@ extension DeclGroupSyntax { func hasMemberFunction(equivalentTo other: FunctionDeclSyntax) -> Bool { for member in memberBlock.members { - if let function = member.as(MemberBlockItemSyntax.self)?.decl.as(FunctionDeclSyntax.self) { + if let function = member.decl.as(FunctionDeclSyntax.self) { if function.isEquivalent(to: other) { return true } @@ -235,7 +228,7 @@ extension DeclGroupSyntax { func hasMemberProperty(equivalentTo other: VariableDeclSyntax) -> Bool { for member in memberBlock.members { - if let variable = member.as(MemberBlockItemSyntax.self)?.decl.as(VariableDeclSyntax.self) { + if let variable = member.decl.as(VariableDeclSyntax.self) { if variable.isEquivalent(to: other) { return true } @@ -246,7 +239,7 @@ extension DeclGroupSyntax { var definedVariables: [VariableDeclSyntax] { memberBlock.members.compactMap { member in - if let variableDecl = member.as(MemberBlockItemSyntax.self)?.decl.as(VariableDeclSyntax.self) + if let variableDecl = member.decl.as(VariableDeclSyntax.self) { return variableDecl } diff --git a/Sources/PerceptionMacros/PerceptibleMacro.swift b/Sources/PerceptionMacros/PerceptibleMacro.swift index 4f15c00..75f6b27 100644 --- a/Sources/PerceptionMacros/PerceptibleMacro.swift +++ b/Sources/PerceptionMacros/PerceptibleMacro.swift @@ -48,11 +48,11 @@ public struct PerceptibleMacro { return """ internal nonisolated func access( - keyPath: KeyPath<\(perceptibleType), Member>, - file: StaticString = #file, - line: UInt = #line + keyPath: KeyPath<\(perceptibleType), Member>, + file: StaticString = #file, + line: UInt = #line ) { - \(raw: registrarVariableName).access(self, keyPath: keyPath, file: file, line: line) + \(raw: registrarVariableName).access(self, keyPath: keyPath, file: file, line: line) } """ } @@ -61,10 +61,10 @@ public struct PerceptibleMacro { return """ internal nonisolated func withMutation( - keyPath: KeyPath<\(perceptibleType), Member>, - _ mutation: () throws -> MutationResult + keyPath: KeyPath<\(perceptibleType), Member>, + _ mutation: () throws -> MutationResult ) rethrows -> MutationResult { - try \(raw: registrarVariableName).withMutation(of: self, keyPath: keyPath, mutation) + try \(raw: registrarVariableName).withMutation(of: self, keyPath: keyPath, mutation) } """ } @@ -218,7 +218,7 @@ extension PerceptibleMacro: MemberMacro { return [] } - let perceptibleType = identified.name + let perceptibleType = identified.name.trimmed if declaration.isEnum { // enumerations cannot store properties @@ -333,7 +333,7 @@ public struct PerceptionTrackedMacro: AccessorMacro { ) throws -> [AccessorDeclSyntax] { guard let property = declaration.as(VariableDeclSyntax.self), property.isValidForPerception, - let identifier = property.identifier + let identifier = property.identifier?.trimmed else { return [] } @@ -346,24 +346,24 @@ public struct PerceptionTrackedMacro: AccessorMacro { """ @storageRestrictions(initializes: _\(identifier)) init(initialValue) { - _\(identifier) = initialValue + _\(identifier) = initialValue } """ let getAccessor: AccessorDeclSyntax = """ get { - access(keyPath: \\.\(identifier)) - return _\(identifier) + access(keyPath: \\.\(identifier)) + return _\(identifier) } """ let setAccessor: AccessorDeclSyntax = """ set { - withMutation(keyPath: \\.\(identifier)) { - _\(identifier) = newValue - } + withMutation(keyPath: \\.\(identifier)) { + _\(identifier) = newValue + } } """ diff --git a/Tests/PerceptionMacrosTests/PerceptionMacrosTests.swift b/Tests/PerceptionMacrosTests/PerceptionMacrosTests.swift index afb8b59..8e6a483 100644 --- a/Tests/PerceptionMacrosTests/PerceptionMacrosTests.swift +++ b/Tests/PerceptionMacrosTests/PerceptionMacrosTests.swift @@ -6,7 +6,7 @@ class PerceptionMacroTests: XCTestCase { override func invokeTest() { withMacroTesting( - //isRecording: true, + // isRecording: true, macros: [ PerceptibleMacro.self, PerceptionTrackedMacro.self, @@ -28,18 +28,18 @@ } expansion: { #""" class Feature { - var count = 0 { - @storageRestrictions(initializes: _count ) + var count { + @storageRestrictions(initializes: _count) init(initialValue) { - _count = initialValue + _count = initialValue } get { - access(keyPath: \.count ) + access(keyPath: \.count) return _count } set { - withMutation(keyPath: \.count ) { - _count = newValue + withMutation(keyPath: \.count) { + _count = newValue } } } @@ -47,15 +47,15 @@ private let _$perceptionRegistrar = Perception.PerceptionRegistrar() internal nonisolated func access( - keyPath: KeyPath, - file: StaticString = #file, - line: UInt = #line + keyPath: KeyPath, + file: StaticString = #file, + line: UInt = #line ) { _$perceptionRegistrar.access(self, keyPath: keyPath, file: file, line: line) } internal nonisolated func withMutation( - keyPath: KeyPath, + keyPath: KeyPath, _ mutation: () throws -> MutationResult ) rethrows -> MutationResult { try _$perceptionRegistrar.withMutation(of: self, keyPath: keyPath, mutation)