Files
SwiftLint/Source/SwiftLintFramework/Rules/Style/OperatorFunctionWhitespaceRule.swift
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

44 lines
2.1 KiB
Swift

import SourceKittenFramework
public struct OperatorFunctionWhitespaceRule: ConfigurationProviderRule, AutomaticTestableRule {
public var configuration = SeverityConfiguration(.warning)
public init() {}
public static let description = RuleDescription(
identifier: "operator_whitespace",
name: "Operator Function Whitespace",
description: "Operators should be surrounded by a single whitespace when defining them.",
kind: .style,
nonTriggeringExamples: [
Example("func <| (lhs: Int, rhs: Int) -> Int {}\n"),
Example("func <|< <A>(lhs: A, rhs: A) -> A {}\n"),
Example("func abc(lhs: Int, rhs: Int) -> Int {}\n")
],
triggeringExamples: [
Example("↓func <|(lhs: Int, rhs: Int) -> Int {}\n"), // no spaces after
Example("↓func <|<<A>(lhs: A, rhs: A) -> A {}\n"), // no spaces after
Example("↓func <| (lhs: Int, rhs: Int) -> Int {}\n"), // 2 spaces after
Example("↓func <|< <A>(lhs: A, rhs: A) -> A {}\n"), // 2 spaces after
Example("↓func <| (lhs: Int, rhs: Int) -> Int {}\n"), // 2 spaces before
Example("↓func <|< <A>(lhs: A, rhs: A) -> A {}\n") // 2 spaces before
]
)
public func validate(file: SwiftLintFile) -> [StyleViolation] {
let escapedOperators = ["/", "=", "-", "+", "!", "*", "|", "^", "~", "?", "."]
.map({ "\\\($0)" }).joined()
let operators = "\(escapedOperators)%<>&"
let zeroOrManySpaces = "(\\s{0}|\\s{2,})"
let pattern1 = "func\\s+[\(operators)]+\(zeroOrManySpaces)(<[A-Z]+>)?\\("
let pattern2 = "func\(zeroOrManySpaces)[\(operators)]+\\s+(<[A-Z]+>)?\\("
return file.match(pattern: "(\(pattern1)|\(pattern2))").filter { _, syntaxKinds in
return syntaxKinds.first == .keyword
}.map { range, _ in
return StyleViolation(ruleDescription: type(of: self).description,
severity: configuration.severity,
location: Location(file: file, characterOffset: range.location))
}
}
}