Skip to content

Commit

Permalink
Add Swift 6 presentation of map(_:) type (realm#5804)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimplyDanny authored Sep 21, 2024
1 parent e9386ae commit 1767dab
Showing 1 changed file with 13 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,16 @@ struct TypesafeArrayInitRule: AnalyzerRule {
)

private static let parentRule = ArrayInitRule()
private static let mapTypePattern = regex("""
\\Q<Self, T where Self : \\E(?:Sequence|Collection)> \
\\Q(Self) -> ((Self.Element) throws -> T) throws -> [T]\\E
""")
private static let mapTypePatterns = [
regex("""
\\Q<Self, T where Self : \\E(?:Sequence|Collection)> \
\\Q(Self) -> ((Self.Element) throws -> T) throws -> [T]\\E
"""),
regex("""
\\Q<Self, T, E where Self : \\E(?:Sequence|Collection), \
\\QE : Error> (Self) -> ((Self.Element) throws(E) -> T) throws(E) -> [T]\\E
"""),
]

func validate(file: SwiftLintFile, compilerArguments: [String]) -> [StyleViolation] {
guard let filePath = file.path else {
Expand Down Expand Up @@ -83,7 +89,9 @@ struct TypesafeArrayInitRule: AnalyzerRule {
if let isSystem = pointee["key.is_system"], isSystem.isEqualTo(true),
let name = pointee["key.name"], name.isEqualTo("map(_:)"),
let typeName = pointee["key.typename"] as? String {
return Self.mapTypePattern.numberOfMatches(in: typeName, range: typeName.fullNSRange) == 1
return Self.mapTypePatterns.contains {
$0.numberOfMatches(in: typeName, range: typeName.fullNSRange) == 1
}
}
return false
}
Expand Down

0 comments on commit 1767dab

Please sign in to comment.