Skip to content

Commit

Permalink
fix: Skip multiple binding variables to support Swift 5.10
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-Alirezaa committed Mar 6, 2024
1 parent 514d3ee commit 26e4afd
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 23 deletions.
3 changes: 2 additions & 1 deletion Package.resolved
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"originHash" : "69270a6b1bddfc649cf1685316d802eb52d2b76bec9743b3de4fba94d65d11e4",
"pins" : [
{
"identity" : "swift-macro-testing",
Expand Down Expand Up @@ -28,5 +29,5 @@
}
}
],
"version" : 2
"version" : 3
}
6 changes: 4 additions & 2 deletions Sources/BuildableMacros/Buildable/BuildableMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ public struct BuildableMacro: MemberAttributeMacro {

guard let variableDecl = member.as(VariableDeclSyntax.self),
variableDecl.bindingSpecifier.tokenKind == .keyword(.var),
!variableDecl.modifiers.lazy.map(\.name.tokenKind).contains(anyOf: [.keyword(.static), .keyword(.class)])
!variableDecl.modifiers.lazy.map(\.name.tokenKind).contains(anyOf: [.keyword(.static), .keyword(.class)]),
variableDecl.bindings.count == 1
else { return [] }

if let firstBinding = variableDecl.bindings.first, let accessors = firstBinding.accessorBlock?.accessors {
let firstBinding = variableDecl.bindings.first!
if let accessors = firstBinding.accessorBlock?.accessors {
switch accessors {
case let .accessors(accessorList):
let specifiers = accessorList.lazy.map(\.accessorSpecifier.tokenKind)
Expand Down
2 changes: 1 addition & 1 deletion Sources/BuildableMacros/SwiftSyntax+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ extension TypeSyntaxProtocol {
} else if
let tuple = self.as(TupleTypeSyntax.self),
tuple.elements.count == 1,
let type = tuple.elements.first?.type.as(TypeSyntax.self) {
let type = (tuple.elements.first?.type).flatMap(TypeSyntax.init) {
return type.requiresEscaping()
}
return false
Expand Down
2 changes: 1 addition & 1 deletion Tests/BuildableMacroTests/BuildableIngoredMacroTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import BuildableMacros
final class BuildableIngoredMacroTests: XCTestCase {
override func invokeTest() {
withMacroTesting(
isRecording: false,
// isRecording: true,
macros: ["BuildableIgnored": BuildableIgnoredMacro.self]
) {
super.invokeTest()
Expand Down
35 changes: 17 additions & 18 deletions Tests/BuildableMacroTests/BuildableMacroTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,6 @@ final class BuildableMacroTests: XCTestCase {
}
}

func testAddingSingleBuildableTrackedToSettableOneForMultipleBindings() throws {
assertMacro {
"""
@Buildable
struct Sample {
var p1: String, p2: Int
}
"""
} expansion: {
"""
struct Sample {
@BuildableTracked
var p1: String, p2: Int
}
"""
}
}

func testPublicAccessControl() throws {
assertMacro {
"""
Expand Down Expand Up @@ -274,6 +256,23 @@ final class BuildableMacroTests: XCTestCase {
}
}

func testSkippingMultipleBindingsProperties() {
assertMacro {
"""
@Buildable
struct Sample {
var p1: String, p2: Int
}
"""
} expansion: {
"""
struct Sample {
var p1: String, p2: Int
}
"""
}
}

func testHandlingSettableComputedProperties() {
assertMacro {
"""
Expand Down
28 changes: 28 additions & 0 deletions Tests/BuildableMacroTests/BuildableTrackedMacroTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ final class BuildableTrackedMacroTests: XCTestCase {
}

func testMultipleSettersForMultiBindings() throws {
#if SWIFT_SYNTAX_509
assertMacro {
"""
struct Sample {
Expand All @@ -141,6 +142,33 @@ final class BuildableTrackedMacroTests: XCTestCase {
}
"""
}
#else
throw XCTSkip("\(#function) is only testable with SwiftSyntax 509")
#endif
}

func testErrorOnMultiBindings() throws {
#if SWIFT_SYNTAX_510
assertMacro {
"""
struct Sample {
@BuildableTracked
var p1: String, p2: Int
}
"""
} diagnostics: {
"""
struct Sample {
@BuildableTracked
┬────────────────
╰─ 🛑 peer macro can only be applied to a single variable
var p1: String, p2: Int
}
"""
}
#else
throw XCTSkip("\(#function) is only testable with SwiftSyntax 510")
#endif
}

func testOpenAccessControlSetters() throws {
Expand Down

0 comments on commit 26e4afd

Please sign in to comment.