mirror of
https://github.com/realm/SwiftLint.git
synced 2026-06-06 20:18:40 +00:00
fcf848608e
* Add Example wrapper in order to display test failures inline when running in Xcode. * Stop using Swift 5.1-only features so we can compile on Xcode 10.2. * Wrap strings in Example. * Add Changelog entry. * Wrap all examples in Example struct. * Better and more complete capturing of line numbers. * Fix broken test. * Better test traceability. * Address or disable linting warnings. * Add documentation comments. * Disable linter for a few cases. * Limit mutability and add copy-and-mutate utility functions. * Limit scope of mutability.
51 lines
2.0 KiB
Swift
51 lines
2.0 KiB
Swift
public struct DiscouragedDirectInitRule: ASTRule, 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: [
|
|
Example("let foo = UIDevice.current"),
|
|
Example("let foo = Bundle.main"),
|
|
Example("let foo = Bundle(path: \"bar\")"),
|
|
Example("let foo = Bundle(identifier: \"bar\")"),
|
|
Example("let foo = Bundle.init(path: \"bar\")"),
|
|
Example("let foo = Bundle.init(identifier: \"bar\")")
|
|
],
|
|
triggeringExamples: [
|
|
Example("↓UIDevice()"),
|
|
Example("↓Bundle()"),
|
|
Example("let foo = ↓UIDevice()"),
|
|
Example("let foo = ↓Bundle()"),
|
|
Example("let foo = bar(bundle: ↓Bundle(), device: ↓UIDevice())"),
|
|
Example("↓UIDevice.init()"),
|
|
Example("↓Bundle.init()"),
|
|
Example("let foo = ↓UIDevice.init()"),
|
|
Example("let foo = ↓Bundle.init()"),
|
|
Example("let foo = bar(bundle: ↓Bundle.init(), device: ↓UIDevice.init())")
|
|
]
|
|
)
|
|
|
|
public func validate(file: SwiftLintFile,
|
|
kind: SwiftExpressionKind,
|
|
dictionary: SourceKittenDictionary) -> [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))]
|
|
}
|
|
}
|