-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use whitespace around operator definitions. #82
Changes from 8 commits
56e2ffb
32edb24
edcfe85
ff51c8a
f799880
10b9ce7
4d0b340
a2e0f2a
752d970
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// | ||
// OperatorWhitespaceRule.swift | ||
// SwiftLint | ||
// | ||
// Created by Akira Hirakawa on 8/6/15. | ||
// Copyright (c) 2015 Realm. All rights reserved. | ||
// | ||
|
||
import SourceKittenFramework | ||
import SwiftXPC | ||
|
||
public struct OperatorFunctionWhitespaceRule: ASTRule { | ||
public init() {} | ||
|
||
public let identifier = "operator_whitespace" | ||
|
||
public func validateFile(file: File) -> [StyleViolation] { | ||
return validateFile(file, dictionary: file.structure.dictionary) | ||
} | ||
|
||
public func validateFile(file: File, dictionary: XPCDictionary) -> [StyleViolation] { | ||
return (dictionary["key.substructure"] as? XPCArray ?? []).flatMap { subItem in | ||
var violations = [StyleViolation]() | ||
if let subDict = subItem as? XPCDictionary, | ||
let kindString = subDict["key.kind"] as? String, | ||
let kind = flatMap(kindString, { SwiftDeclarationKind(rawValue: $0) }) { | ||
violations.extend(validateFile(file, dictionary: subDict)) | ||
violations.extend(validateFile(file, kind: kind, dictionary: subDict)) | ||
} | ||
return violations | ||
} | ||
} | ||
|
||
public func validateFile(file: File, | ||
kind: SwiftDeclarationKind, | ||
dictionary: XPCDictionary) -> [StyleViolation] { | ||
let functionKinds: [SwiftDeclarationKind] = [ | ||
.FunctionFree, | ||
] | ||
if !contains(functionKinds, kind) { | ||
return [] | ||
} | ||
var violations = [StyleViolation]() | ||
if let nameOffset = flatMap(dictionary["key.nameoffset"] as? Int64, { Int($0) }), | ||
let nameLength = flatMap(dictionary["key.namelength"] as? Int64, { Int($0) }), | ||
let offset = flatMap(dictionary["key.offset"] as? Int64, { Int($0) }) { | ||
|
||
let location = Location(file: file, offset: offset) | ||
let startAdvance = advance(file.contents.startIndex, nameOffset) | ||
let endAdvance = advance(startAdvance, nameLength) | ||
let range = Range<String.Index>(start: startAdvance, end: endAdvance) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah yes, this is inferred:) I will remove it |
||
let definition = file.contents.substringWithRange(range) | ||
|
||
let ope1 = ["/", "=", "-", "+", "!", "*", "|", "^", "~", "?", "."].map({ "\\\($0)" }) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice use of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it means .map({"($0)"}), doesn't it? |
||
let ope2 = ["%", "<", ">", "&"] | ||
let ope = "".join(ope1 + ope2) | ||
let pattern = "^[\(ope)]+(<[A-Z]+>)?\\(" | ||
|
||
if let regex = NSRegularExpression(pattern: pattern, options: nil, error: nil) { | ||
let matchRange = NSRange(location: 0, length: count(definition.utf16)) | ||
let matches = regex.matchesInString(definition, options: nil, range: matchRange) | ||
|
||
if matches.count > 0 { | ||
violations.append(StyleViolation(type: .OperatorFunctionWhitespace, | ||
location: location, | ||
severity: .Medium, | ||
reason: "Use whitespace around operators when defining them")) | ||
} | ||
} | ||
} | ||
return violations | ||
} | ||
|
||
public let example = RuleExample( | ||
ruleName: "Operator Function Whitespace Rule", | ||
ruleDescription: "Use whitespace around operators 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", | ||
"func <|<<A>(lhs: A, rhs: A) -> A {}\n" | ||
] | ||
) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😄