Files
SwiftLint/Source/SwiftLintFramework/Rules/OperatorFunctionWhitespaceRule.swift
T
Norio Nomura 40828dff03 Merge branch 'master' into swift3.0
* master: (41 commits)
  Fix formatting in CHANGELOG.md
  release 0.13.0
  Update CHANGELOG.md
  Fix check for trailing whitespace to return early
  Fix checks for some inline comments
  Replace check for comments to use SyntaxKind
  Add configuration for trailing_whitespace to ignore comments
  Unwanted space removed
  - Lint issues fixed
  Updated HTML Reporter
  PR feedback
  Add check on autocorrect for disabled range
  Use `utf8.count` instead of `utf16.count` to byte range
  Re-write `ExplicitInitRule` to `ASTRule`
  added ExplicitInitRule
  Updated CHANGELOG
  HTML Reporter added
  HTML Reporter added
  Adds information about SwiftLint plugin for AppCode into README.md
  added reasons why a new rule should be opt in
  ...

# Conflicts:
#	Source/SwiftLintFramework/Extensions/File+SwiftLint.swift
#	Source/SwiftLintFramework/Extensions/Structure+SwiftLint.swift
#	Source/SwiftLintFramework/Rules/ColonRule.swift
#	Source/SwiftLintFramework/Rules/CommaRule.swift
#	Source/SwiftLintFramework/Rules/LegacyCGGeometryFunctionsRule.swift
#	Source/SwiftLintFramework/Rules/LegacyConstantRule.swift
#	Source/SwiftLintFramework/Rules/LegacyConstructorRule.swift
#	Source/SwiftLintFramework/Rules/LegacyNSGeometryFunctionsRule.swift
#	Source/SwiftLintFramework/Rules/LineLengthRule.swift
#	Source/SwiftLintFramework/Rules/OperatorFunctionWhitespaceRule.swift
#	Source/SwiftLintFramework/Rules/ReturnArrowWhitespaceRule.swift
#	Source/SwiftLintFramework/Rules/RuleConfigurations/StatementPositionConfiguration.swift
#	Source/SwiftLintFramework/Rules/StatementPositionRule.swift
#	Source/SwiftLintFramework/Rules/TrailingWhitespaceRule.swift
#	Tests/SwiftLintFramework/RuleConfigurationTests.swift
2016-11-04 21:40:56 +09:00

53 lines
2.1 KiB
Swift

//
// OperatorWhitespaceRule.swift
// SwiftLint
//
// Created by Akira Hirakawa on 8/6/15.
// Copyright (c) 2015 Realm. All rights reserved.
//
import SourceKittenFramework
public struct OperatorFunctionWhitespaceRule: ConfigurationProviderRule {
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.",
nonTriggeringExamples: [
"func <| (lhs: Int, rhs: Int) -> Int {}\n",
"func <|< <A>(lhs: A, rhs: A) -> A {}\n",
"func abc(lhs: Int, rhs: Int) -> Int {}\n"
],
triggeringExamples: [
"↓func <|(lhs: Int, rhs: Int) -> Int {}\n", // no spaces after
"↓func <|<<A>(lhs: A, rhs: A) -> A {}\n", // no spaces after
"↓func <| (lhs: Int, rhs: Int) -> Int {}\n", // 2 spaces after
"↓func <|< <A>(lhs: A, rhs: A) -> A {}\n", // 2 spaces after
"↓func <| (lhs: Int, rhs: Int) -> Int {}\n", // 2 spaces before
"↓func <|< <A>(lhs: A, rhs: A) -> A {}\n" // 2 spaces before
]
)
public func validateFile(_ file: File) -> [StyleViolation] {
let operators = ["/", "=", "-", "+", "!", "*", "|", "^", "~", "?", "."].map({ "\\\($0)" }) +
["%", "<", ">", "&"]
let zeroOrManySpaces = "(\\s{0}|\\s{2,})"
let pattern1 = "func\\s+[" + operators.joined() +
"]+\(zeroOrManySpaces)(<[A-Z]+>)?\\("
let pattern2 = "func\(zeroOrManySpaces)[" + operators.joined() +
"]+\\s+(<[A-Z]+>)?\\("
return file.matchPattern("(\(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))
}
}
}