-
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
b42f6ff
commit 1b4563e
Showing
6 changed files
with
85 additions
and
1 deletion.
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
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
71 changes: 71 additions & 0 deletions
71
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,71 @@ | ||
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 }"), | ||
Example("f ↓{ $0.0.a }"), | ||
Example("f(a: ↓{ $0.b })"), | ||
Example("f { 1 } a: ↓{ $0.b }"), | ||
Example("let f: (Int) -> Int = ↓{ $0.bigEndian }") | ||
] | ||
) | ||
} | ||
|
||
private extension PreferKeyPathRule { | ||
final class Visitor: ViolationsSyntaxVisitor<ConfigurationType> { | ||
override func visitPost(_ node: ClosureExprSyntax) { | ||
if case let .expr(expr) = node.statements.onlyElement?.item, | ||
expr.accesses(identifier: node.onlyParameter) { | ||
violations.append(node.positionAfterSkippingLeadingTrivia) | ||
} | ||
} | ||
} | ||
} | ||
|
||
private extension ExprSyntax { | ||
func accesses(identifier: String?) -> Bool { | ||
if let base = `as`(MemberAccessExprSyntax.self)?.base { | ||
if let declRef = base.as(DeclReferenceExprSyntax.self) { | ||
return declRef.baseName.text == identifier ?? "$0" | ||
} | ||
return base.accesses(identifier: identifier) | ||
} | ||
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
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