-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #69 from dafurman/supportNonEscapingClosureParams
Fix #35 - Support nonescaping closure parameters
- Loading branch information
Showing
9 changed files
with
227 additions
and
8 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
Sources/SpyableMacro/Extensions/FunctionParameters+Extensions.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,36 @@ | ||
import SwiftSyntax | ||
|
||
extension FunctionParameterListSyntax { | ||
/// - Returns: Whether or not the function parameter list supports generating and using properties to track received arguments and received invocations. | ||
var supportsParameterTracking: Bool { | ||
!isEmpty && !contains { $0.containsNonEscapingClosure } | ||
} | ||
} | ||
|
||
extension FunctionParameterSyntax { | ||
fileprivate var containsNonEscapingClosure: Bool { | ||
if type.is(FunctionTypeSyntax.self) { | ||
return true | ||
} | ||
guard let attributedType = type.as(AttributedTypeSyntax.self), | ||
attributedType.baseType.is(FunctionTypeSyntax.self) else { | ||
return false | ||
} | ||
|
||
return !attributedType.attributes.contains { | ||
$0.attributeNameTextMatches(TokenSyntax.keyword(.escaping).text) | ||
} | ||
} | ||
|
||
var usesAutoclosure: Bool { | ||
type.as(AttributedTypeSyntax.self)?.attributes.contains { | ||
$0.attributeNameTextMatches(TokenSyntax.keyword(.autoclosure).text) | ||
} == true | ||
} | ||
} | ||
|
||
private extension AttributeListSyntax.Element { | ||
func attributeNameTextMatches(_ name: String) -> Bool { | ||
self.as(AttributeSyntax.self)?.attributeName.as(IdentifierTypeSyntax.self)?.name.text == name | ||
} | ||
} |
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
22 changes: 22 additions & 0 deletions
22
Tests/SpyableMacroTests/Extensions/UT_FunctionParameterListSyntax+Extensions.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,22 @@ | ||
import SwiftSyntax | ||
import XCTest | ||
|
||
@testable import SpyableMacro | ||
|
||
final class UT_FunctionParameterListSyntaxExtensions: XCTestCase { | ||
func testSupportsParameterTracking() { | ||
XCTAssertTrue( | ||
FunctionParameterListSyntax { | ||
"param: Int" | ||
"param: inout Int" | ||
"param: @autoclosure @escaping (Int) async throws -> Void" | ||
}.supportsParameterTracking | ||
) | ||
|
||
XCTAssertFalse( | ||
FunctionParameterListSyntax { | ||
"param: (Int) -> Void" | ||
}.supportsParameterTracking | ||
) | ||
} | ||
} |
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