Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Update weak delegate rule to only match delegate suffix #923

Merged
merged 6 commits into from
Dec 3, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@

##### Bug Fixes

* None.
* Fix `weak_delegate` rule reporting a violation for variables containing
but not ending in `delegate`.
[Phil Webster](https://github.com/philwebster)

## 0.13.2: Light Cycle

Expand Down
9 changes: 5 additions & 4 deletions Source/SwiftLintFramework/Rules/WeakDelegateRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ public struct WeakDelegateRule: ASTRule, ConfigurationProviderRule {
// We only consider properties to be a delegate if it has "delegate" in its name
"class Foo {\n var scrollHandler: ScrollDelegate?\n}\n",
// Only trigger on instance variables, not local variables
"func foo() {\n var delegate: SomeDelegate\n}\n"
"func foo() {\n var delegate: SomeDelegate\n}\n",
// Only trigger when variable has the suffix "-delegate" to avoid false positives
"class Foo {\n var delegateNotified: Bool?\n}\n"
],
triggeringExamples: [
"class Foo {\n var delegate: SomeProtocol?\n}\n",
"class Foo {\n var scrollDelegate: ScrollDelegate?\n}\n",
"class Foo {\n var delegateScroll: ScrollDelegate?\n}\n"
"class Foo {\n var scrollDelegate: ScrollDelegate?\n}\n"
]
)

Expand All @@ -43,7 +44,7 @@ public struct WeakDelegateRule: ASTRule, ConfigurationProviderRule {

// Check if name contains "delegate"
guard let name = (dictionary["key.name"] as? String),
name.lowercased().contains("delegate") else {
name.lowercased().hasSuffix("delegate") else {
return []
}

Expand Down