-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
108b1fc
commit 247038c
Showing
4 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
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
68 changes: 68 additions & 0 deletions
68
Source/SwiftLintBuiltInRules/Rules/Idiomatic/PreferKeyPathRule.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,68 @@ | ||
import SwiftLintCore | ||
import SwiftSyntax | ||
|
||
@SwiftSyntaxRule | ||
struct PreferKeyPathRule: OptInRule { | ||
var configuration = SeverityConfiguration<Self>(.warning) | ||
|
||
static var description = RuleDescription( | ||
identifier: "prefer_key_path", | ||
name: "Prefer Key Path", | ||
description: "Use a key path argument instead of a closure with property access", | ||
kind: .idiomatic, | ||
minSwiftVersion: .fiveDotTwo, | ||
nonTriggeringExamples: [ | ||
Example("f {}"), | ||
Example("f { $0 }"), | ||
Example("f() { g() }"), | ||
Example("f { a.b.c }"), | ||
Example("f { a in a }"), | ||
Example("f { a, b in a.b }"), | ||
Example("f { (a, b) in a.b }") | ||
], | ||
triggeringExamples: [ | ||
Example("f ↓{ $0.a }"), | ||
Example("f ↓{ a in a.b }"), | ||
Example("f ↓{ a in a.b.c }"), | ||
Example("f ↓{ (a: A) in a.b }"), | ||
Example("f ↓{ (a b: A) in b.c }") | ||
] | ||
) | ||
} | ||
|
||
private extension PreferKeyPathRule { | ||
final class Visitor: ViolationsSyntaxVisitor<ConfigurationType> { | ||
override func visitPost(_ node: FunctionCallExprSyntax) { | ||
if let closure = node.trailingClosure, | ||
case let .expr(expr) = closure.statements.onlyElement?.item, | ||
expr.accesses(identifier: closure.onlyParameter) { | ||
violations.append(closure.positionAfterSkippingLeadingTrivia) | ||
} | ||
} | ||
} | ||
} | ||
|
||
private extension ExprSyntax { | ||
func accesses(identifier: String?) -> Bool { | ||
if let access = `as`(MemberAccessExprSyntax.self) { | ||
if let declRef = access.base?.as(DeclReferenceExprSyntax.self) { | ||
return declRef.baseName.text == identifier ?? "$0" | ||
} | ||
return false | ||
} | ||
return false | ||
} | ||
} | ||
|
||
private extension ClosureExprSyntax { | ||
var onlyParameter: String? { | ||
switch signature?.parameterClause { | ||
case let .simpleInput(params): | ||
return params.onlyElement?.name.text | ||
case let .parameterClause(params): | ||
let param = params.parameters.onlyElement | ||
return param?.secondName?.text ?? param?.firstName.text | ||
case nil: return nil | ||
} | ||
} | ||
} |
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