-
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 #1731 from ornithocoder/discourage_init
Add discouraged_init opt-in rule that discourages direct initialization of certain types
- Loading branch information
Showing
7 changed files
with
288 additions
and
7 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
61 changes: 61 additions & 0 deletions
61
Source/SwiftLintFramework/Rules/DiscouragedDirectInitRule.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,61 @@ | ||
// | ||
// DiscourageInitRule.swift | ||
// SwiftLint | ||
// | ||
// Created by Ornithologist Coder on 8/1/17. | ||
// Copyright © 2017 Realm. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import SourceKittenFramework | ||
|
||
public struct DiscouragedDirectInitRule: ASTRule, OptInRule, ConfigurationProviderRule { | ||
public var configuration = DiscouragedDirectInitConfiguration() | ||
|
||
public init() {} | ||
|
||
public static let description = RuleDescription( | ||
identifier: "discouraged_direct_init", | ||
name: "Discouraged Direct Initialization", | ||
description: "Discouraged direct initialization of types that can be harmful.", | ||
kind: .lint, | ||
nonTriggeringExamples: [ | ||
"let foo = UIDevice.current", | ||
"let foo = Bundle.main", | ||
"let foo = Bundle(path: \"bar\")", | ||
"let foo = Bundle(identifier: \"bar\")", | ||
"let foo = Bundle.init(path: \"bar\")", | ||
"let foo = Bundle.init(identifier: \"bar\")" | ||
], | ||
triggeringExamples: [ | ||
"↓UIDevice()", | ||
"↓Bundle()", | ||
"let foo = ↓UIDevice()", | ||
"let foo = ↓Bundle()", | ||
"let foo = bar(bundle: ↓Bundle(), device: ↓UIDevice())", | ||
"↓UIDevice.init()", | ||
"↓Bundle.init()", | ||
"let foo = ↓UIDevice.init()", | ||
"let foo = ↓Bundle.init()", | ||
"let foo = bar(bundle: ↓Bundle.init(), device: ↓UIDevice.init())" | ||
] | ||
) | ||
|
||
public func validate(file: File, | ||
kind: SwiftExpressionKind, | ||
dictionary: [String: SourceKitRepresentable]) -> [StyleViolation] { | ||
guard | ||
kind == .call, | ||
let offset = dictionary.nameOffset, | ||
let name = dictionary.name, | ||
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))] | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
Source/SwiftLintFramework/Rules/RuleConfigurations/DiscouragedDirectInitConfiguration.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,59 @@ | ||
// | ||
// DiscouragedInitConfiguration.swift | ||
// SwiftLint | ||
// | ||
// Created by Ornithologist Coder on 8/1/17. | ||
// Copyright © 2017 Realm. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import SourceKittenFramework | ||
|
||
private func toExplicitInitMethod(typeName: String) -> String { | ||
return "\(typeName).init" | ||
} | ||
|
||
public struct DiscouragedDirectInitConfiguration: RuleConfiguration, Equatable { | ||
public var severityConfiguration = SeverityConfiguration(.warning) | ||
|
||
public var consoleDescription: String { | ||
return severityConfiguration.consoleDescription + ", types: \(discouragedInits)" | ||
} | ||
|
||
public var severity: ViolationSeverity { | ||
return severityConfiguration.severity | ||
} | ||
|
||
private(set) public var discouragedInits: Set<String> | ||
|
||
private let defaultDiscouragedInits = [ | ||
"Bundle", | ||
"UIDevice" | ||
] | ||
|
||
init() { | ||
discouragedInits = Set(defaultDiscouragedInits + defaultDiscouragedInits.map(toExplicitInitMethod)) | ||
} | ||
|
||
// 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) | ||
} | ||
|
||
if let types = [String].array(of: configuration["types"]) { | ||
discouragedInits = Set(types + types.map(toExplicitInitMethod)) | ||
} | ||
} | ||
|
||
// MARK: - Equatable | ||
|
||
public static func == (lhs: DiscouragedDirectInitConfiguration, rhs: DiscouragedDirectInitConfiguration) -> Bool { | ||
return lhs.discouragedInits == rhs.discouragedInits && 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
Oops, something went wrong.