diff --git a/Package.resolved b/Package.resolved index c4f9794..6340fa4 100644 --- a/Package.resolved +++ b/Package.resolved @@ -5,8 +5,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/apple/swift-syntax.git", "state" : { - "revision" : "165fc6d22394c1168ff76ab5d951245971ef07e5", - "version" : "509.0.0-swift-DEVELOPMENT-SNAPSHOT-2023-06-05-a" + "revision" : "057bdfb4632cc758973d46efaf98e43d07ee4bdb", + "version" : "509.0.0-swift-DEVELOPMENT-SNAPSHOT-2023-08-28-a" } } ], diff --git a/Package.swift b/Package.swift index 41f07bb..c1cf164 100644 --- a/Package.swift +++ b/Package.swift @@ -13,6 +13,7 @@ let package = Package( name: "ExtractCaseValue", targets: ["ExtractCaseValue"] ), + .library(name: "ExtractCaseValueTypes", targets: ["ExtractCaseValueTypes"]), .executable( name: "ExtractCaseValueClient", targets: ["ExtractCaseValueClient"] @@ -20,7 +21,7 @@ let package = Package( ], dependencies: [ // Depend on the latest Swift 5.9 prerelease of SwiftSyntax - .package(url: "https://github.com/apple/swift-syntax.git", from: "509.0.0-swift-5.9-DEVELOPMENT-SNAPSHOT-2023-04-25-b") + .package(url: "https://github.com/apple/swift-syntax.git", from: "509.0.0-swift-DEVELOPMENT-SNAPSHOT-2023-08-28-a") ], targets: [ // Targets are the basic building blocks of a package, defining a module or a test suite. @@ -30,12 +31,16 @@ let package = Package( name: "ExtractCaseValueMacros", dependencies: [ .product(name: "SwiftSyntaxMacros", package: "swift-syntax"), - .product(name: "SwiftCompilerPlugin", package: "swift-syntax") + .product(name: "SwiftCompilerPlugin", package: "swift-syntax"), + "ExtractCaseValueTypes" ] ), // Library that exposes a macro as part of its API, which is used in client programs. - .target(name: "ExtractCaseValue", dependencies: ["ExtractCaseValueMacros"]), + .target(name: "ExtractCaseValue", dependencies: ["ExtractCaseValueMacros", "ExtractCaseValueTypes"]), + .target(name: "ExtractCaseValueTypes", dependencies: [ + .product(name: "SwiftSyntax", package: "swift-syntax"), + ]), // A client of the library, which is able to use the macro in its own code. .executableTarget(name: "ExtractCaseValueClient", dependencies: ["ExtractCaseValue"]), diff --git a/Sources/ExtractCaseValue/ExtractCaseValue.swift b/Sources/ExtractCaseValue/ExtractCaseValue.swift index f3280f8..54b2793 100644 --- a/Sources/ExtractCaseValue/ExtractCaseValue.swift +++ b/Sources/ExtractCaseValue/ExtractCaseValue.swift @@ -1,4 +1,4 @@ -import ExtractCaseValueMacros +import ExtractCaseValueTypes /// A macro that extracts an associated value from enum cases using a default value if /// extraction is not possible. diff --git a/Sources/ExtractCaseValueMacros/CaseExtractionKind.swift b/Sources/ExtractCaseValueMacros/CaseExtractionKind.swift index 4c90b2d..497d32c 100644 --- a/Sources/ExtractCaseValueMacros/CaseExtractionKind.swift +++ b/Sources/ExtractCaseValueMacros/CaseExtractionKind.swift @@ -1,40 +1,27 @@ import SwiftSyntax - -/// The available kinds of case value extractions. -public enum CaseExtractionKind { - /// Extract a value at a position in the associated values. - case position(Int) - - /// Extract a value with a certain name. - case associatedValueName(String) - - /// Extract the first value with a matching type. - case firstMatchingType - - public static let `default` = Self.firstMatchingType -} +import ExtractCaseValueTypes extension CaseExtractionKind { - init?(expr: ExprSyntax) { - guard - let functionCall = expr.as(FunctionCallExprSyntax.self), - let memberAccessExpr = functionCall.calledExpression.as(MemberAccessExprSyntax.self) - else { return nil } + init?(expr: ExprSyntax) { + guard + let functionCall = expr.as(FunctionCallExprSyntax.self), + let memberAccessExpr = functionCall.calledExpression.as(MemberAccessExprSyntax.self) + else { return nil } - let firstIntArgument = (functionCall.argumentList.first?.expression.as(IntegerLiteralExprSyntax.self)?.digits.text).flatMap(Int.init) - let firstStringArgument = functionCall.argumentList.first?.expression.stringLiteralSegment + let firstIntArgument = (functionCall.argumentList.first?.expression.as(IntegerLiteralExprSyntax.self)?.digits.text).flatMap(Int.init) + let firstStringArgument = functionCall.argumentList.first?.expression.stringLiteralSegment - switch memberAccessExpr.name.text { - case "position" : - guard let position = firstIntArgument else { return nil } - self = .position(position) - case "associatedValueName": - guard let name = firstStringArgument?.content.text else { return nil } - self = .associatedValueName(name) - case "firstMatchingType": - self = .firstMatchingType - default: - return nil + switch memberAccessExpr.name.text { + case "position" : + guard let position = firstIntArgument else { return nil } + self = .position(position) + case "associatedValueName": + guard let name = firstStringArgument?.content.text else { return nil } + self = .associatedValueName(name) + case "firstMatchingType": + self = .firstMatchingType + default: + return nil + } } - } } diff --git a/Sources/ExtractCaseValueMacros/ExtractCaseValueMacro.swift b/Sources/ExtractCaseValueMacros/ExtractCaseValueMacro.swift index 347d2ae..d1e3290 100644 --- a/Sources/ExtractCaseValueMacros/ExtractCaseValueMacro.swift +++ b/Sources/ExtractCaseValueMacros/ExtractCaseValueMacro.swift @@ -2,6 +2,7 @@ import SwiftDiagnostics import SwiftSyntax import SwiftSyntaxBuilder import SwiftSyntaxMacros +import ExtractCaseValueTypes // Argument labels let caseParamExtractionPropertyNameArgumentLabel = "name" @@ -98,7 +99,7 @@ extension ExtractCaseValueMacro: MemberMacro { let elements = caseDecls.flatMap(\.elements) // infer access modifier from enum - let access = enumDecl.modifiers?.first(where: \.isNeededAccessLevelModifier) + let access = enumDecl.modifiers.first(where: \.isNeededAccessLevelModifier) var switchCaseSyntaxes: [SwitchCaseSyntax] = [] diff --git a/Sources/ExtractCaseValueTypes/CaseExtractionKind.swift b/Sources/ExtractCaseValueTypes/CaseExtractionKind.swift new file mode 100644 index 0000000..8ce02ff --- /dev/null +++ b/Sources/ExtractCaseValueTypes/CaseExtractionKind.swift @@ -0,0 +1,23 @@ +// +// CaseExtractionKind.swift +// +// +// Created by Tomas Harkema on 02/09/2023. +// + +import Foundation + +/// The available kinds of case value extractions. +public enum CaseExtractionKind { + /// Extract a value at a position in the associated values. + case position(Int) + + /// Extract a value with a certain name. + case associatedValueName(String) + + /// Extract the first value with a matching type. + case firstMatchingType + + public static let `default` = Self.firstMatchingType +} +