Files
Zev Eisenberg fcf848608e Add Inline test failure messages (#3040)
* 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.
2020-02-02 10:35:37 +02:00

32 lines
1.1 KiB
Swift

import SourceKittenFramework
public struct EmptyStringRule: ConfigurationProviderRule, OptInRule, AutomaticTestableRule {
public var configuration = SeverityConfiguration(.warning)
public init() {}
public static let description = RuleDescription(
identifier: "empty_string",
name: "Empty String",
description: "Prefer checking `isEmpty` over comparing `string` to an empty string literal.",
kind: .performance,
nonTriggeringExamples: [
Example("myString.isEmpty"),
Example("!myString.isEmpty")
],
triggeringExamples: [
Example("myString↓ == \"\""),
Example("myString↓ != \"\"")
]
)
public func validate(file: SwiftLintFile) -> [StyleViolation] {
let pattern = "\\b\\s*(==|!=)\\s*\"\""
return file.match(pattern: pattern, with: [.string]).map {
StyleViolation(ruleDescription: type(of: self).description,
severity: configuration.severity,
location: Location(file: file, characterOffset: $0.location))
}
}
}