-
Notifications
You must be signed in to change notification settings - Fork 164
Support @autoclosure within arguments at @DependencyEndpoint
#160
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
Changes from all commits
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 |
|---|---|---|
|
|
@@ -162,14 +162,14 @@ public enum DependencyEndpointMacro: AccessorMacro, PeerMacro { | |
| } | ||
| let appliedParameters = | ||
| parameters | ||
| .enumerated() | ||
| .map { | ||
| guard let typed = $0.type.as(AttributedTypeSyntax.self), | ||
| typed.specifier?.tokenKind == .keyword(.inout) | ||
| else { return false } | ||
| return true | ||
| $1.isInout | ||
| ? "&p\($0)" | ||
| : $1.isAutoclosure | ||
| ? "p\($0)()" | ||
| : "p\($0)" | ||
|
Comment on lines
+167
to
+171
Contributor
Author
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. #159 style seemed so cool that I had to try it myself. Originally, I was writing code like this 😅 let appliedParameters =
parameters
.enumerated()
.map {
if let typed = $1.type.as(AttributedTypeSyntax.self),
typed.specifier?.tokenKind == .keyword(.inout) {
return "&p\($0)"
}
if let typed = $1.type.as(AttributedTypeSyntax.self),
typed.attributes.contains(where: {
$0.as(AttributeSyntax.self)?.attributeName.as(IdentifierTypeSyntax.self)?.name.tokenKind == .identifier("autoclosure")
}) {
return "p\($0)()"
}
return "p\($0)"
}
.joined(separator: ", ") |
||
| } | ||
| .enumerated() | ||
| .map { $1 ? "&p\($0)" : "p\($0)" } | ||
| .joined(separator: ", ") | ||
| decls.append( | ||
| """ | ||
|
|
@@ -256,3 +256,26 @@ extension String { | |
| return String(result) | ||
| } | ||
| } | ||
|
|
||
| extension TupleTypeElementSyntax { | ||
| fileprivate var isAutoclosure: Bool { | ||
|
Contributor
Author
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. |
||
| self.type | ||
| .as(AttributedTypeSyntax.self)? | ||
| .attributes | ||
| .contains { | ||
| $0 | ||
| .as(AttributeSyntax.self)? | ||
| .attributeName | ||
| .as(IdentifierTypeSyntax.self)? | ||
| .name | ||
| .tokenKind == .identifier("autoclosure") | ||
|
Contributor
Author
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. #159 uses |
||
| } ?? false | ||
| } | ||
|
|
||
| fileprivate var isInout: Bool { | ||
| self.type | ||
| .as(AttributedTypeSyntax.self)? | ||
| .specifier? | ||
| .tokenKind == .keyword(.inout) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -796,4 +796,40 @@ final class DependencyEndpointMacroTests: BaseTestCase { | |
| """ | ||
| } | ||
| } | ||
|
|
||
| func testAutoclosure() { | ||
| assertMacro { | ||
| """ | ||
| struct Foo { | ||
| @DependencyEndpoint | ||
| var bar: (_ a: @autoclosure () -> Int, _ b: () -> Int, _ c: @autoclosure () -> Int) -> Void | ||
|
Contributor
Author
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. |
||
| } | ||
| """ | ||
| } expansion: { | ||
| """ | ||
| struct Foo { | ||
| var bar: (_ a: @autoclosure () -> Int, _ b: () -> Int, _ c: @autoclosure () -> Int) -> Void { | ||
| @storageRestrictions(initializes: _bar) | ||
| init(initialValue) { | ||
| _bar = initialValue | ||
| } | ||
| get { | ||
| _bar | ||
| } | ||
| set { | ||
| _bar = newValue | ||
| } | ||
| } | ||
|
|
||
| func bar(a p0: @autoclosure () -> Int, b p1: () -> Int, c p2: @autoclosure () -> Int) -> Void { | ||
| self.bar(p0(), p1, p2()) | ||
| } | ||
|
|
||
| private var _bar: (_ a: @autoclosure () -> Int, _ b: () -> Int, _ c: @autoclosure () -> Int) -> Void = { _, _, _ in | ||
| XCTestDynamicOverlay.XCTFail("Unimplemented: 'bar'") | ||
| } | ||
| } | ||
| """ | ||
| } | ||
| } | ||
| } | ||

Uh oh!
There was an error while loading. Please reload this page.
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.
Only
()is OK because@autoclosurecan be used only when there are no parameters.Also
inoutexpression cannot be used on@autoclosureso we don't need to consider the situation when$1.isInout && $1.isAutoclosure.