Skip to content

Commit 594cc52

Browse files
authored
Merge pull request #3 from p-x9/feature/improve-diagnose
improve diagnoses
2 parents 46af14d + 9dbcc38 commit 594cc52

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

Sources/AssociatedObjectPlugin/AssociatedObjectMacro.swift

+19-14
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,18 @@ import SwiftSyntaxMacros
1313
public struct AssociatedObjectMacro {}
1414

1515
extension AssociatedObjectMacro: PeerMacro {
16-
public static func expansion<
17-
Context: MacroExpansionContext,
18-
Declaration: DeclSyntaxProtocol
19-
>(
16+
public static func expansion<Context: MacroExpansionContext, Declaration: DeclSyntaxProtocol>(
2017
of node: AttributeSyntax,
2118
providingPeersOf declaration: Declaration,
2219
in context: Context
2320
) throws -> [DeclSyntax] {
21+
2422
guard let varDecl = declaration.as(VariableDeclSyntax.self),
2523
let binding = varDecl.bindings.first,
26-
let identifier = binding.pattern.as(IdentifierPatternSyntax.self)?.identifier else { return [] }
24+
let identifier = binding.pattern.as(IdentifierPatternSyntax.self)?.identifier else {
25+
context.diagnose(AssociatedObjectMacroDiagnostic.requiresVariableDeclaration.diagnose(at: declaration))
26+
return []
27+
}
2728

2829
let keyDecl = VariableDeclSyntax(
2930
bindingKeyword: .identifier("static var"),
@@ -49,23 +50,28 @@ extension AssociatedObjectMacro: AccessorMacro {
4950
in context: Context
5051
) throws -> [AccessorDeclSyntax] {
5152

52-
guard let varDecl = declaration.as(VariableDeclSyntax.self) else {
53+
guard let varDecl = declaration.as(VariableDeclSyntax.self),
54+
let binding = varDecl.bindings.first,
55+
let identifier = binding.pattern.as(IdentifierPatternSyntax.self)?.identifier
56+
else {
57+
// Probably can't add a diagnose here, since this is an Accessor macro
5358
context.diagnose(AssociatedObjectMacroDiagnostic.requiresVariableDeclaration.diagnose(at: declaration))
5459
return []
5560
}
5661

57-
guard let binding = varDecl.bindings.first,
58-
let identifier = binding.pattern.as(IdentifierPatternSyntax.self)?.identifier,
59-
let type = binding.typeAnnotation?.type
60-
else {
62+
// Explicit specification of type is required
63+
guard let type = binding.typeAnnotation?.type else {
64+
context.diagnose(AssociatedObjectMacroDiagnostic.specifyTypeExplicitly.diagnose(at: identifier))
6165
return []
6266
}
6367

64-
guard binding.accessor == nil else {
65-
context.diagnose(AssociatedObjectMacroDiagnostic.accessorShouldBeNil.diagnose(at: declaration))
68+
// Error if accessor already exists
69+
if let accessor = binding.accessor {
70+
context.diagnose(AssociatedObjectMacroDiagnostic.accessorShouldBeNil.diagnose(at: accessor))
6671
return []
6772
}
6873

74+
// Initial value required
6975
guard let defaultValue = binding.initializer?.value else {
7076
context.diagnose(AssociatedObjectMacroDiagnostic.requiresInitialValue.diagnose(at: declaration))
7177
return []
@@ -79,9 +85,8 @@ extension AssociatedObjectMacro: AccessorMacro {
7985

8086
return [
8187
"""
82-
8388
get {
84-
return objc_getAssociatedObject(
89+
objc_getAssociatedObject(
8590
self,
8691
&Self.__associated_\(identifier)Key
8792
) as? \(type)

Sources/AssociatedObjectPlugin/AssociatedObjectMacroDiagnostic.swift

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public enum AssociatedObjectMacroDiagnostic {
1313
case requiresVariableDeclaration
1414
case accessorShouldBeNil
1515
case requiresInitialValue
16+
case specifyTypeExplicitly
1617
}
1718

1819
extension AssociatedObjectMacroDiagnostic: DiagnosticMessage {
@@ -28,6 +29,8 @@ extension AssociatedObjectMacroDiagnostic: DiagnosticMessage {
2829
return "`accessor should not be specified."
2930
case .requiresInitialValue:
3031
return "Initial values must be specified."
32+
case .specifyTypeExplicitly:
33+
return "Specify a type explicitly"
3134
}
3235
}
3336

0 commit comments

Comments
 (0)