-
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.
Merge pull request #1192 from marcelofabri/discarded_notification_cen…
…ter_observer Add discarded_notification_center_observer rule
- Loading branch information
Showing
5 changed files
with
79 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
62 changes: 62 additions & 0 deletions
62
Source/SwiftLintFramework/Rules/DiscardedNotificationCenterObserverRule.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,62 @@ | ||
// | ||
// DiscardedNotificationCenterObserverRule.swift | ||
// SwiftLint | ||
// | ||
// Created by Marcelo Fabri on 01/13/17. | ||
// Copyright © 2017 Realm. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import SourceKittenFramework | ||
|
||
public struct DiscardedNotificationCenterObserverRule: ASTRule, ConfigurationProviderRule { | ||
public var configuration = SeverityConfiguration(.warning) | ||
|
||
public init() {} | ||
|
||
public static let description = RuleDescription( | ||
identifier: "discarded_notification_center_observer", | ||
name: "Discarded Notification Center Observer", | ||
description: "When registing for a notification using a block, the opaque observer that is " + | ||
"returned should be stored so it can be removed later.", | ||
nonTriggeringExamples: [ | ||
"let foo = nc.addObserver(forName: .NSSystemTimeZoneDidChange, object: nil, queue: nil) { }\n", | ||
"let foo = nc.addObserver(forName: .NSSystemTimeZoneDidChange, object: nil, queue: nil, using: { })\n" | ||
], | ||
triggeringExamples: [ | ||
"↓nc.addObserver(forName: .NSSystemTimeZoneDidChange, object: nil, queue: nil) { }\n", | ||
"↓nc.addObserver(forName: .NSSystemTimeZoneDidChange, object: nil, queue: nil, using: { })\n" | ||
] | ||
) | ||
|
||
public func validate(file: File, kind: SwiftExpressionKind, | ||
dictionary: [String: SourceKitRepresentable]) -> [StyleViolation] { | ||
return violationOffsets(in: file, dictionary: dictionary, kind: kind).map { location in | ||
StyleViolation(ruleDescription: type(of: self).description, | ||
severity: configuration.severity, | ||
location: Location(file: file, byteOffset: location)) | ||
} | ||
} | ||
|
||
private func violationOffsets(in file: File, dictionary: [String: SourceKitRepresentable], | ||
kind: SwiftExpressionKind) -> [Int] { | ||
guard kind == .call, | ||
let name = dictionary.name, | ||
name.hasSuffix(".addObserver"), | ||
case let arguments = dictionary.enclosedArguments, | ||
case let argumentsNames = arguments.flatMap({ $0.name }), | ||
argumentsNames == ["forName", "object", "queue"] || | ||
argumentsNames == ["forName", "object", "queue", "using"], | ||
let offset = dictionary.offset, | ||
let range = file.contents.bridge().byteRangeToNSRange(start: 0, length: offset) else { | ||
return [] | ||
} | ||
|
||
if let lastMatch = regex("\\s?=\\s*").matches(in: file.contents, options: [], range: range).last?.range, | ||
lastMatch.location == range.length - lastMatch.length { | ||
return [] | ||
} | ||
|
||
return [offset] | ||
} | ||
} |
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