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.
Adds new opt-in rule, private_action
Implements realm#1931.
- Loading branch information
1 parent
a03f8f8
commit 9d2c113
Showing
7 changed files
with
219 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
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)) | ||
] | ||
} | ||
} |
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