-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
9 changed files
with
237 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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
72 changes: 72 additions & 0 deletions
72
Sources/CodableMacroPlugin/Diagnostics/StaticVariableDeclaration.swift
This file contains 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,72 @@ | ||
@_implementationOnly import SwiftDiagnostics | ||
@_implementationOnly import SwiftSyntax | ||
@_implementationOnly import SwiftSyntaxMacros | ||
|
||
/// A diagnostic producer type that can validate passed syntax is not a static | ||
/// variable declaration. | ||
/// | ||
/// This producer can be used for macro-attributes that must be attached to | ||
/// non static variable declarations. | ||
struct StaticVariableDeclaration<Attr: PropertyAttribute>: DiagnosticProducer { | ||
/// The attribute for which | ||
/// validation performed. | ||
/// | ||
/// Uses this attribute name | ||
/// in generated diagnostic | ||
/// messages. | ||
let attr: Attr | ||
|
||
/// Creates a static variable declaration validation instance | ||
/// with provided attribute. | ||
/// | ||
/// - Parameter attr: The attribute for which | ||
/// validation performed. | ||
/// - Returns: Newly created diagnostic producer. | ||
init(_ attr: Attr) { | ||
self.attr = attr | ||
} | ||
|
||
/// Validates and produces diagnostics for the passed syntax | ||
/// in the macro expansion context provided. | ||
/// | ||
/// Checks whether provided syntax is a non static variable declaration, | ||
/// for static variable declarations error diagnostics | ||
/// are generated. | ||
/// | ||
/// - Parameters: | ||
/// - syntax: The syntax to validate and produce diagnostics for. | ||
/// - context: The macro expansion context diagnostics produced in. | ||
/// | ||
/// - Returns: True if syntax fails validation, false otherwise. | ||
@discardableResult | ||
func produce( | ||
for syntax: some SyntaxProtocol, | ||
in context: some MacroExpansionContext | ||
) -> Bool { | ||
// The Macro fails to compile if the .modifiers.contains | ||
// is directly used in the guard statement. | ||
let isStatic = syntax.as(VariableDeclSyntax.self)? | ||
.modifiers.contains { $0.name.tokenKind == .keyword(.static) } | ||
guard isStatic ?? false else { return false } | ||
let message = attr.node.diagnostic( | ||
message: | ||
"@\(attr.name) can't be used with static variables declarations", | ||
id: attr.misuseMessageID, | ||
severity: .error | ||
) | ||
context.diagnose(attr: attr, message: message) | ||
return true | ||
} | ||
} | ||
|
||
extension PropertyAttribute { | ||
/// Indicates attribute must be attached to non static variable declaration. | ||
/// | ||
/// The created diagnostic producer produces error diagnostic, | ||
/// if attribute is attached to static variable declarations. | ||
/// | ||
/// - Returns: Static variable declaration validation diagnostic producer. | ||
func attachedToNonStaticVariable() -> StaticVariableDeclaration<Self> { | ||
return .init(self) | ||
} | ||
} |
This file contains 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 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 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