-
Notifications
You must be signed in to change notification settings - Fork 129
Add Swift macro (requires Swift v5.9) #58
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
74a003f
Add Swift macro (requires Swift v5.9)
tinder-cfuller bef545f
Update Swift workflow
tinder-cfuller 104380d
Fix expandable section in readme
tinder-cfuller b0ce72d
Use initializers
tinder-cfuller 2f82a57
Throw error instead of returning empty array
tinder-cfuller 25fce4d
Do not abbreviate enumeration
tinder-cfuller fd2ee5c
Update file header comments
tinder-cfuller 7f490da
Fix tests
tinder-cfuller 992588b
Utilize transform instead of for loop
tinder-cfuller 80654f3
Remove local variables
tinder-cfuller 3cc4f1a
Improve formatting
tinder-cfuller ada5b9d
Use throwing initializer
tinder-cfuller File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// swift-tools-version:5.9 | ||
|
||
import PackageDescription | ||
import CompilerPluginSupport | ||
|
||
let package = Package( | ||
name: "StateMachine", | ||
platforms: [ | ||
.macOS(.v10_15), | ||
.iOS(.v13), | ||
.tvOS(.v13), | ||
.watchOS(.v5), | ||
], | ||
products: [ | ||
.library( | ||
name: "StateMachine", | ||
targets: ["StateMachine"]), | ||
], | ||
dependencies: [ | ||
.package( | ||
url: "https://github.com/apple/swift-syntax.git", | ||
from: "509.1.0"), | ||
.package( | ||
url: "https://github.com/Quick/Nimble.git", | ||
from: "13.2.0"), | ||
], | ||
targets: [ | ||
.target( | ||
name: "StateMachine", | ||
dependencies: ["StateMachineMacros"], | ||
path: "Swift/Sources/StateMachine"), | ||
.macro( | ||
name: "StateMachineMacros", | ||
dependencies: [ | ||
.product(name: "SwiftSyntaxMacros", package: "swift-syntax"), | ||
.product(name: "SwiftCompilerPlugin", package: "swift-syntax"), | ||
], | ||
path: "Swift/Sources/StateMachineMacros"), | ||
.testTarget( | ||
name: "StateMachineTests", | ||
dependencies: [ | ||
"StateMachine", | ||
"StateMachineMacros", | ||
.product(name: "SwiftSyntaxMacros", package: "swift-syntax"), | ||
.product(name: "SwiftSyntaxMacrosTestSupport", package: "swift-syntax"), | ||
"Nimble", | ||
], | ||
path: "Swift/Tests/StateMachineTests"), | ||
] | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// | ||
// Copyright (c) 2024, Match Group, LLC | ||
// BSD License, see LICENSE file for details | ||
// | ||
|
||
@attached(extension, | ||
conformances: StateMachineHashable, | ||
names: named(hashableIdentifier), named(HashableIdentifier), named(associatedValue)) | ||
public macro StateMachineHashable() = #externalMacro(module: "StateMachineMacros", | ||
type: "StateMachineHashableMacro") |
92 changes: 92 additions & 0 deletions
92
Swift/Sources/StateMachineMacros/Macros/StateMachineHashableMacro.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// | ||
// Copyright (c) 2019, Match Group, LLC | ||
// BSD License, see LICENSE file for details | ||
// | ||
|
||
import SwiftSyntax | ||
import SwiftSyntaxMacros | ||
|
||
public struct StateMachineHashableMacro: ExtensionMacro { | ||
|
||
public static func expansion( | ||
of node: AttributeSyntax, | ||
attachedTo declaration: some DeclGroupSyntax, | ||
providingExtensionsOf type: some TypeSyntaxProtocol, | ||
conformingTo protocols: [TypeSyntax], | ||
in context: some MacroExpansionContext | ||
) throws -> [ExtensionDeclSyntax] { | ||
|
||
guard let enumDecl: EnumDeclSyntax = .init(declaration) | ||
else { throw StateMachineHashableMacroError.typeMustBeEnumeration } | ||
|
||
let elements: [EnumCaseElementSyntax] = enumDecl | ||
.memberBlock | ||
.members | ||
.compactMap(MemberBlockItemSyntax.init) | ||
.map(\.decl) | ||
.compactMap(EnumCaseDeclSyntax.init) | ||
.flatMap(\.elements) | ||
|
||
guard !elements.isEmpty | ||
else { throw StateMachineHashableMacroError.enumerationMustHaveCases } | ||
|
||
let enumCases: [String] = elements | ||
.map(\.name.text) | ||
.map { "case \($0)" } | ||
|
||
let hashableIdentifierCases: [String] = elements | ||
.map(\.name.text) | ||
.map { "case .\($0):\nreturn .\($0)" } | ||
|
||
let associatedValueCases: [String] = elements.map { element in | ||
if let parameters: EnumCaseParameterListSyntax = element.parameterClause?.parameters, !parameters.isEmpty { | ||
if parameters.count > 1 { | ||
let associatedValues: String = (1...parameters.count) | ||
.map { "value\($0)" } | ||
.joined(separator: ", ") | ||
return """ | ||
case let .\(element.name.text)(\(associatedValues)): | ||
return (\(associatedValues)) | ||
""" | ||
} else { | ||
return """ | ||
case let .\(element.name.text)(value): | ||
return (value) | ||
""" | ||
} | ||
} else { | ||
return """ | ||
case .\(element.name.text): | ||
return () | ||
""" | ||
} | ||
} | ||
|
||
let node: DeclSyntax = """ | ||
extension \(type): StateMachineHashable { | ||
|
||
enum HashableIdentifier { | ||
|
||
\(raw: enumCases.joined(separator: "\n")) | ||
} | ||
|
||
var hashableIdentifier: HashableIdentifier { | ||
switch self { | ||
\(raw: hashableIdentifierCases.joined(separator: "\n")) | ||
} | ||
} | ||
|
||
var associatedValue: Any { | ||
switch self { | ||
\(raw: associatedValueCases.joined(separator: "\n")) | ||
} | ||
} | ||
} | ||
""" | ||
|
||
guard let extensionDecl: ExtensionDeclSyntax = .init(node) | ||
else { throw StateMachineHashableMacroError.invalidExtension } | ||
|
||
return [extensionDecl] | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
Swift/Sources/StateMachineMacros/Macros/StateMachineHashableMacroError.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// | ||
// Copyright (c) 2019, Match Group, LLC | ||
// BSD License, see LICENSE file for details | ||
// | ||
|
||
public enum StateMachineHashableMacroError: Error, CustomStringConvertible { | ||
|
||
case typeMustBeEnumeration | ||
case enumerationMustHaveCases | ||
case invalidExtension | ||
|
||
public var description: String { | ||
switch self { | ||
case .typeMustBeEnumeration: | ||
return "Type Must Be Enumeration" | ||
case .enumerationMustHaveCases: | ||
return "Enumeration Must Have Cases" | ||
case .invalidExtension: | ||
return "Invalid Extension" | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// | ||
// Copyright (c) 2019, Match Group, LLC | ||
// BSD License, see LICENSE file for details | ||
// | ||
|
||
#if canImport(SwiftCompilerPlugin) | ||
|
||
import SwiftCompilerPlugin | ||
import SwiftSyntaxMacros | ||
|
||
@main | ||
internal struct StateMachineMacros: CompilerPlugin { | ||
|
||
internal let providingMacros: [Macro.Type] = [StateMachineHashableMacro.self] | ||
} | ||
|
||
#endif |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.