Skip to content

Commit

Permalink
Merge pull request #15 from Mr-Alirezaa/feature/swift-5-10-support
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-Alirezaa authored Mar 12, 2024
2 parents 8191170 + d1104c3 commit cb0925e
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 29 deletions.
12 changes: 6 additions & 6 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,26 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/swift-macro-testing.git",
"state" : {
"revision" : "10dcef36314ddfea6f60442169b0b320204cbd35",
"version" : "0.2.2"
"revision" : "90e38eec4bf661ec0da1bbfd3ec507d0f0c05310",
"version" : "0.3.0"
}
},
{
"identity" : "swift-snapshot-testing",
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/swift-snapshot-testing",
"state" : {
"revision" : "8e68404f641300bfd0e37d478683bb275926760c",
"version" : "1.15.2"
"revision" : "5b0c434778f2c1a4c9b5ebdb8682b28e84dd69bd",
"version" : "1.15.4"
}
},
{
"identity" : "swift-syntax",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-syntax.git",
"state" : {
"revision" : "64889f0c732f210a935a0ad7cda38f77f876262d",
"version" : "509.1.1"
"revision" : "08a2f0a9a30e0f705f79c9cfaca1f68b71bdc775",
"version" : "510.0.0"
}
}
],
Expand Down
3 changes: 3 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ let package = Package(
dependencies: [
"BuildableMacros",
.product(name: "MacroTesting", package: "swift-macro-testing"),
],
swiftSettings: [
.define("SWIFT_SYNTAX_509")
]
),
]
Expand Down
55 changes: 55 additions & 0 deletions Package@swift-5.10.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// swift-tools-version: 5.10
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription
import CompilerPluginSupport

let package = Package(
name: "BuildableMacro",
platforms: [
.macOS("10.15"),
.iOS("13.0"),
.tvOS("13.0"),
.watchOS("6.0"),
.macCatalyst("13.0"),
.visionOS("1.0")
],
products: [
.library(
name: "BuildableMacro",
targets: ["BuildableMacro"]
),
],
dependencies: [
.package(url: "https://github.com/apple/swift-syntax.git", from: "510.0.0"),
.package(url: "https://github.com/pointfreeco/swift-macro-testing.git", from: "0.1.0"),
],
targets: [
// Macro implementation that performs the source transformation of a macro.
.macro(
name: "BuildableMacros",
dependencies: [
.product(name: "SwiftSyntaxMacros", package: "swift-syntax"),
.product(name: "SwiftCompilerPlugin", package: "swift-syntax")
]
),

// Library that exposes a macro as part of its API, which is used in client programs.
.target(name: "BuildableMacro", dependencies: ["BuildableMacros"]),

// A client of the library, which is able to use the macro in its own code.
.executableTarget(name: "BuildableMacroClient", dependencies: ["BuildableMacro"]),

// A test target used to develop the macro implementation.
.testTarget(
name: "BuildableMacroTests",
dependencies: [
"BuildableMacros",
.product(name: "MacroTesting", package: "swift-macro-testing"),
],
swiftSettings: [
.define("SWIFT_SYNTAX_510")
]
),
]
)
1 change: 0 additions & 1 deletion Sources/BuildableMacroClient/main.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
import Foundation
import BuildableMacro

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 cb0925e

Please sign in to comment.