Files
SwiftLint/Source/SwiftLintFramework/File+SwiftLint.swift
T
2015-05-18 06:17:05 +02:00

75 lines
3.3 KiB
Swift

//
// File+SwiftLint.swift
// SwiftLint
//
// Created by JP Simard on 2015-05-16.
// Copyright (c) 2015 Realm. All rights reserved.
//
import SourceKittenFramework
import SwiftXPC
typealias Line = (index: Int, content: String)
extension File {
public func matchPattern(pattern: String, withSyntaxKinds syntaxKinds: [SyntaxKind] = []) ->
[NSRange] {
return flatMap(NSRegularExpression(pattern: pattern, options: nil, error: nil)) { regex in
let range = NSRange(location: 0, length: count(self.contents.utf16))
let syntax = SyntaxMap(file: self)
let matches = regex.matchesInString(self.contents, options: nil, range: range)
return map(matches as? [NSTextCheckingResult]) { matches in
return compact(matches.map { match in
let tokensInRange = syntax.tokens.filter {
NSLocationInRange($0.offset, match.range)
}
let kindsInRange = compact(map(tokensInRange) {
SyntaxKind(rawValue: $0.type)
})
if kindsInRange.count != syntaxKinds.count {
return nil
}
for (index, kind) in enumerate(syntaxKinds) {
if kind != kindsInRange[index] {
return nil
}
}
return match.range
})
}
} ?? []
}
func astViolationsInDictionary(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(self.astViolationsInDictionary(subDict))
violations.extend(TypeNameRule.validateFile(self, kind: kind, dictionary: subDict))
violations.extend(VariableNameRule.validateFile(self, kind: kind, dictionary: subDict))
violations.extend(TypeBodyLengthRule.validateFile(self, kind: kind, dictionary: subDict))
violations.extend(FunctionBodyLengthRule.validateFile(self, kind: kind, dictionary: subDict))
violations.extend(NestingRule.validateFile(self, kind: kind, dictionary: subDict))
}
return violations
}
}
internal var stringViolations: [StyleViolation] {
let lines = contents.lines()
// FIXME: Using '+' to concatenate these arrays would be nicer,
// but slows the compiler to a crawl.
var violations = LineLengthRule.validateFile(self)
violations.extend(LeadingWhitespaceRule.validateFile(self))
violations.extend(TrailingWhitespaceRule.validateFile(self))
violations.extend(TrailingNewlineRule.validateFile(self))
violations.extend(ForceCastRule.validateFile(self))
violations.extend(FileLengthRule.validateFile(self))
violations.extend(TodoRule.validateFile(self))
violations.extend(ColonRule.validateFile(self))
return violations
}
}