forked from realm/SwiftLint
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduction of Yoda condition checking.
This PR aims to implement [realm#1924][1]. [1]: realm#1924
- Loading branch information
Daniel Metzing
committed
Dec 18, 2017
1 parent
a02d4e1
commit e5d5a22
Showing
9 changed files
with
448 additions
and
2 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// | ||
// PrivateActionRule.swift | ||
// SwiftLint | ||
// | ||
// Created by Ornithologist Coder on 11/7/17. | ||
// Copyright © 2016 Realm. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import SourceKittenFramework | ||
|
||
public struct PrivateActionRule: ASTRule, OptInRule, ConfigurationProviderRule { | ||
public var configuration = SeverityConfiguration(.warning) | ||
|
||
public init() {} | ||
|
||
public static let description = RuleDescription( | ||
identifier: "private_action", | ||
name: "Private Actions", | ||
description: "IBActions should be private.", | ||
kind: .lint, | ||
nonTriggeringExamples: [ | ||
"class Foo {\n\t@IBAction private func barButtonTapped(_ sender: UIButton) {}\n}\n", | ||
"struct Foo {\n\t@IBAction private func barButtonTapped(_ sender: UIButton) {}\n}\n", | ||
"class Foo {\n\t@IBAction fileprivate func barButtonTapped(_ sender: UIButton) {}\n}\n", | ||
"struct Foo {\n\t@IBAction fileprivate func barButtonTapped(_ sender: UIButton) {}\n}\n", | ||
"private extension Foo {\n\t@IBAction func barButtonTapped(_ sender: UIButton) {}\n}\n", | ||
"fileprivate extension Foo {\n\t@IBAction func barButtonTapped(_ sender: UIButton) {}\n}\n" | ||
], | ||
triggeringExamples: [ | ||
"class Foo {\n\t@IBAction ↓func barButtonTapped(_ sender: UIButton) {}\n}\n", | ||
"struct Foo {\n\t@IBAction ↓func barButtonTapped(_ sender: UIButton) {}\n}\n", | ||
"class Foo {\n\t@IBAction public ↓func barButtonTapped(_ sender: UIButton) {}\n}\n", | ||
"struct Foo {\n\t@IBAction public ↓func barButtonTapped(_ sender: UIButton) {}\n}\n", | ||
"class Foo {\n\t@IBAction internal ↓func barButtonTapped(_ sender: UIButton) {}\n}\n", | ||
"struct Foo {\n\t@IBAction internal ↓func barButtonTapped(_ sender: UIButton) {}\n}\n", | ||
"extension Foo {\n\t@IBAction ↓func barButtonTapped(_ sender: UIButton) {}\n}\n", | ||
"extension Foo {\n\t@IBAction public ↓func barButtonTapped(_ sender: UIButton) {}\n}\n", | ||
"extension Foo {\n\t@IBAction internal ↓func barButtonTapped(_ sender: UIButton) {}\n}\n", | ||
"public extension Foo {\n\t@IBAction ↓func barButtonTapped(_ sender: UIButton) {}\n}\n", | ||
"internal extension Foo {\n\t@IBAction ↓func barButtonTapped(_ sender: UIButton) {}\n}\n" | ||
] | ||
) | ||
|
||
public func validate(file: File, | ||
kind: SwiftDeclarationKind, | ||
dictionary: [String: SourceKitRepresentable]) -> [StyleViolation] { | ||
guard | ||
let offset = dictionary.offset, | ||
kind == .functionMethodInstance, | ||
dictionary.enclosedSwiftAttributes.contains("source.decl.attribute.ibaction"), | ||
let controlLevel = dictionary.accessibility.flatMap(AccessControlLevel.init(identifier:)), | ||
controlLevel.isPrivate == false | ||
else { | ||
return [] | ||
} | ||
|
||
return [ | ||
StyleViolation(ruleDescription: type(of: self).description, | ||
severity: configuration.severity, | ||
location: Location(file: file, byteOffset: offset)) | ||
] | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
Source/SwiftLintFramework/Rules/RuleConfigurations/YodaConditionConfiguration.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,31 @@ | ||
// | ||
// YodaConditionConfiguration.swift | ||
// SwiftLint | ||
// | ||
// Created by Daniel.Metzing on 02/12/17. | ||
// Copyright © 2017 Realm. All rights reserved. | ||
// | ||
|
||
public struct YodaConditionConfiguration: RuleConfiguration, Equatable { | ||
|
||
private(set) var severityConfiguration = SeverityConfiguration(.warning) | ||
|
||
public var consoleDescription: String { | ||
return severityConfiguration.consoleDescription | ||
} | ||
|
||
public mutating func apply(configuration: Any) throws { | ||
guard let configuration = configuration as? [String: Any] else { | ||
throw ConfigurationError.unknownConfiguration | ||
} | ||
|
||
if let severityString = configuration["severity"] as? String { | ||
try severityConfiguration.apply(configuration: severityString) | ||
} | ||
} | ||
|
||
public static func == (lhs: YodaConditionConfiguration, | ||
rhs: YodaConditionConfiguration) -> Bool { | ||
return lhs.severityConfiguration == rhs.severityConfiguration | ||
} | ||
} |
Oops, something went wrong.