Skip to content

Commit

Permalink
Prevent adding initializer syntax to a binding
Browse files Browse the repository at this point in the history
  • Loading branch information
Supereg committed Sep 10, 2024
1 parent e7b4762 commit 259dbfe
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
4 changes: 4 additions & 0 deletions Sources/SpeziAccountMacros/AccountKeyMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ extension AccountKeyMacro: PeerMacro {
throw DiagnosticsError(syntax: binding, message: "Variable binding is missing a type annotation", id: .invalidSyntax)
}

if let initializer = binding.initializer {
throw DiagnosticsError(syntax: initializer, message: "Variable binding cannot have a initializer", id: .invalidSyntax)
}

guard case let .argumentList(argumentList) = node.arguments else {
throw DiagnosticsError(syntax: node, message: "Unexpected arguments passed to '@AccountKey'", id: .invalidSyntax)
}
Expand Down
36 changes: 33 additions & 3 deletions Tests/SpeziAccountMacrosTests/AccountKeyMacroTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -398,16 +398,46 @@ final class AccountKeyMacroTests: XCTestCase { // swiftlint:disable:this type_bo
extension NotAccountDetails {
var genderIdentity: GenderIdentity? {
get {
self[__Key_genderIdentity.self]
self [__Key_genderIdentity.self]
}
set {
self[__Key_genderIdentity.self] = newValue
self [__Key_genderIdentity.self] = newValue
}
}
}
""",
diagnostics: [
DiagnosticSpec(
message: "'@AccountKey' can only be applied to 'var' declarations inside of an extension to 'AccountDetails'",
line: 2,
column: 5
)
],
macros: testMacros
)

assertMacroExpansion(

Check failure on line 419 in Tests/SpeziAccountMacrosTests/AccountKeyMacroTests.swift

View workflow job for this annotation

GitHub Actions / Build and Test Swift Package macOS / Test using xcodebuild or run fastlane

testGeneralDiagnostics, failed - Macro expansion did not produce the expected expanded source
"""
extension AccountDetails {
@AccountKey(name: "Gender Identity", category: .personalDetails, as: GenderIdentity.self, initial: .default(.preferNotToState))
var genderIdentity: GenderIdentity? = .male
}
""",
expandedSource:
"""
extension AccountDetails {
var genderIdentity: GenderIdentity? {
get {
self [__Key_genderIdentity.self]
}
set {
self [__Key_genderIdentity.self] = newValue
}
}
}
""",
diagnostics: [
DiagnosticSpec(message: "'@AccountKey' can only be applied to 'var' declarations inside of 'AccountDetails'", line: 2, column: 5)
DiagnosticSpec(message: "Variable binding cannot have a initializer", line: 3, column: 41)
],
macros: testMacros
)
Expand Down

0 comments on commit 259dbfe

Please sign in to comment.