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.
Rule requested by @Noobish1 on issue realm#1306.
- Loading branch information
1 parent
f0f746f
commit 8c6a5da
Showing
6 changed files
with
120 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// | ||
// DiscourageInitRule.swift | ||
// SwiftLint | ||
// | ||
// Created by Ornithologist Coder on 8/1/17. | ||
// Copyright © 2017 Realm. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import SourceKittenFramework | ||
|
||
public struct DiscouragedInitRule: ASTRule, OptInRule, ConfigurationProviderRule { | ||
public var configuration = DiscouragedInitConfiguration() | ||
|
||
public init() {} | ||
|
||
public static let description = RuleDescription( | ||
identifier: "discouraged_init", | ||
name: "Discouraged Init", | ||
description: "Discouraged direct initialization of class.", | ||
kind: .style, | ||
nonTriggeringExamples: [ | ||
"let foo = UIDevice.current", | ||
"let foo = Bundle.main", | ||
"let foo = Bundle(path: \"bar\")", | ||
"let foo = Bundle(identifier: \"bar\")" | ||
], | ||
triggeringExamples: [ | ||
"↓UIDevice()", | ||
"↓Bundle()", | ||
"let foo = ↓UIDevice()", | ||
"let foo = ↓Bundle()", | ||
"let foo = bar(bundle: ↓Bundle(), device: ↓UIDevice())" | ||
] | ||
) | ||
|
||
public func validate(file: File, | ||
kind: SwiftExpressionKind, | ||
dictionary: [String: SourceKitRepresentable]) -> [StyleViolation] { | ||
guard | ||
let offset = dictionary.nameOffset, | ||
let name = dictionary.name, | ||
kind == .call, | ||
dictionary.bodyLength == 0, | ||
configuration.discouragedInits.contains(name) | ||
else { | ||
return [] | ||
} | ||
|
||
return [StyleViolation(ruleDescription: type(of: self).description, | ||
severity: configuration.severity, | ||
location: Location(file: file, byteOffset: offset))] | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
Source/SwiftLintFramework/Rules/RuleConfigurations/DiscouragedInitConfiguration.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,45 @@ | ||
// | ||
// DiscouragedInitConfiguration.swift | ||
// SwiftLint | ||
// | ||
// Created by Ornithologist Coder on 8/1/17. | ||
// Copyright © 2017 Realm. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import SourceKittenFramework | ||
|
||
public struct DiscouragedInitConfiguration: RuleConfiguration, Equatable { | ||
public var severityConfiguration = SeverityConfiguration(.warning) | ||
|
||
public var consoleDescription: String { | ||
return severityConfiguration.consoleDescription | ||
} | ||
|
||
public let discouragedInits = [ | ||
"Bundle", | ||
"UIDevice" | ||
] | ||
|
||
public var severity: ViolationSeverity { | ||
return severityConfiguration.severity | ||
} | ||
|
||
// MARK: - RuleConfiguration | ||
|
||
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) | ||
} | ||
} | ||
|
||
// MARK: - Equatable | ||
|
||
public static func == (lhs: DiscouragedInitConfiguration, rhs: DiscouragedInitConfiguration) -> Bool { | ||
return lhs.severityConfiguration == rhs.severityConfiguration | ||
} | ||
} |
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