-
-
Notifications
You must be signed in to change notification settings - Fork 41
fix: @Codable fails in structs with static members. #37
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 all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
046d2c6
Fixed failing Codable in structs with static members
Midbin 209a4b4
Fixed spacing and variable name
Midbin 22be4d9
Add support for static diagnostic checking for Default and CodedBy
Midbin 620490c
Fixed formatting
Midbin acf371b
Run swift-format
Midbin 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
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
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
72 changes: 72 additions & 0 deletions
72
Sources/CodableMacroPlugin/Diagnostics/StaticVariableDeclaration.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,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 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
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
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.