-
-
Notifications
You must be signed in to change notification settings - Fork 4
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
Refactor property validation and simplify generation #26
Changes from all commits
197f950
6c341c5
450833b
4839eeb
c85d4e6
41f8875
a88955b
346e1da
4e8dd1a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,7 +35,7 @@ public enum FixableInstantiableError: DiagnosticError { | |
public var description: String { | ||
switch self { | ||
case .dependencyHasTooManyAttributes: | ||
"Dependency can have at most one of @\(Dependency.Source.instantiated), @\(Dependency.Source.received), or @\(Dependency.Source.forwarded) attached macro" | ||
"Dependency can have at most one of @\(Dependency.Source.instantiatedRawValue), @\(Dependency.Source.receivedRawValue), or @\(Dependency.Source.forwardedRawValue) attached macro" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
case .dependencyHasInitializer: | ||
"Dependency must not have hand-written initializer" | ||
case .missingPublicOrOpenAttribute: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Distributed under the MIT License | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
|
||
import SwiftSyntax | ||
|
||
extension AttributeListSyntax.Element { | ||
|
||
var instantiableMacro: AttributeSyntax? { | ||
attributeIfNameEquals(InstantiableVisitor.macroName) | ||
} | ||
|
||
var instantiatedMacro: AttributeSyntax? { | ||
attributeIfNameEquals(Dependency.Source.instantiatedRawValue) | ||
} | ||
|
||
var receivedMacro: AttributeSyntax? { | ||
attributeIfNameEquals(Dependency.Source.receivedRawValue) | ||
} | ||
|
||
var forwardedMacro: AttributeSyntax? { | ||
attributeIfNameEquals(Dependency.Source.forwardedRawValue) | ||
} | ||
|
||
private func attributeIfNameEquals(_ expectedName: String) -> AttributeSyntax? { | ||
switch self { | ||
case let .attribute(attribute): | ||
IdentifierTypeSyntax(attribute.attributeName)?.name.text == expectedName ? attribute : nil | ||
case .ifConfigDecl: | ||
nil | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,12 +24,7 @@ extension AttributeListSyntax { | |
|
||
public var instantiableMacro: AttributeSyntax? { | ||
guard let attribute = first(where: { element in | ||
switch element { | ||
case let .attribute(attribute): | ||
return IdentifierTypeSyntax(attribute.attributeName)?.name.text == InstantiableVisitor.macroName | ||
case .ifConfigDecl: | ||
return false | ||
} | ||
element.instantiableMacro != nil | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I simplified our macro access while I was adding associated types to the |
||
}) else { | ||
return nil | ||
} | ||
|
@@ -38,12 +33,7 @@ extension AttributeListSyntax { | |
|
||
public var instantiatedMacro: AttributeSyntax? { | ||
guard let attribute = first(where: { element in | ||
switch element { | ||
case let .attribute(attribute): | ||
return IdentifierTypeSyntax(attribute.attributeName)?.name.text == Dependency.Source.instantiated.rawValue | ||
case .ifConfigDecl: | ||
return false | ||
} | ||
element.instantiatedMacro != nil | ||
}) else { | ||
return nil | ||
} | ||
|
@@ -52,38 +42,24 @@ extension AttributeListSyntax { | |
|
||
public var receivedMacro: AttributeSyntax? { | ||
guard let attribute = first(where: { element in | ||
switch element { | ||
case let .attribute(attribute): | ||
return IdentifierTypeSyntax(attribute.attributeName)?.name.text == Dependency.Source.received.rawValue | ||
case .ifConfigDecl: | ||
return false | ||
} | ||
element.receivedMacro != nil | ||
}) else { | ||
return nil | ||
} | ||
return AttributeSyntax(attribute) | ||
} | ||
|
||
public var attributedNodes: [(attribute: String, node: AttributeListSyntax.Element)] { | ||
compactMap { element in | ||
switch element { | ||
case let .attribute(attribute): | ||
guard let identifierText = IdentifierTypeSyntax(attribute.attributeName)?.name.text else { | ||
public var dependencySources: [(source: Dependency.Source, node: AttributeListSyntax.Element)] { | ||
compactMap { | ||
switch $0 { | ||
case .attribute: | ||
guard let source = Dependency.Source(node: $0) else { | ||
return nil | ||
} | ||
return (attribute: identifierText, node: element) | ||
return (source: source, node: $0) | ||
case .ifConfigDecl: | ||
return nil | ||
} | ||
} | ||
} | ||
|
||
public var dependencySources: [(source: Dependency.Source, node: AttributeListSyntax.Element)] { | ||
attributedNodes.compactMap { | ||
guard let source = Dependency.Source.init(rawValue: $0.attribute) else { | ||
return nil | ||
} | ||
return (source: source, node: $0.node) | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
following the advice here to avoid this kind of CI failure