SwiftLint/Source/SwiftLintFramework/Rules/Style/ClosingBraceRule.swift

44 lines
1.5 KiB
Swift

import Foundation
import SourceKittenFramework
public struct ClosingBraceRule: SubstitutionCorrectableRule, ConfigurationProviderRule, AutomaticTestableRule {
public var configuration = SeverityConfiguration(.warning)
public init() {}
public static let description = RuleDescription(
identifier: "closing_brace",
name: "Closing Brace Spacing",
description: "Closing brace with closing parenthesis " +
"should not have any whitespaces in the middle.",
kind: .style,
nonTriggeringExamples: [
Example("[].map({ })"),
Example("[].map(\n { }\n)")
],
triggeringExamples: [
Example("[].map({ ↓} )"),
Example("[].map({ ↓}\t)")
],
corrections: [
Example("[].map({ ↓} )\n"): Example("[].map({ })\n")
]
)
public func validate(file: SwiftLintFile) -> [StyleViolation] {
return violationRanges(in: file).map {
StyleViolation(ruleDescription: type(of: self).description,
severity: configuration.severity,
location: Location(file: file, characterOffset: $0.location))
}
}
public func violationRanges(in file: SwiftLintFile) -> [NSRange] {
return file.match(pattern: "(\\}[ \\t]+\\))", excludingSyntaxKinds: SyntaxKind.commentAndStringKinds)
}
public func substitution(for violationRange: NSRange, in file: SwiftLintFile) -> (NSRange, String)? {
return (violationRange, "})")
}
}