diff --git a/Source/SwiftLintFramework/Linter.swift b/Source/SwiftLintFramework/Linter.swift index 5f9a2d021d..31c1bac98a 100644 --- a/Source/SwiftLintFramework/Linter.swift +++ b/Source/SwiftLintFramework/Linter.swift @@ -11,13 +11,12 @@ import SwiftXPC import SourceKittenFramework public struct Linter { - private let file: File private let context: LinterContext public var styleViolations: [StyleViolation] { if self.linters.count == 1 { // Terminal case return context.enabledRules.flatMap { - $0.validateFile(self.file) + $0.validateFile(self.context.file) } } @@ -29,38 +28,33 @@ public struct Linter { private var linters: [Linter] { let linterContextBegin = "// swift-lint:begin-context" let linterContextEnd = "// swift-lint:end-context" - - + var contextDepth = 0 var (contextStartingLineNumber, contextEndingLineNumber) = (0, 0) var currentLineNumber = 0 - var currentContext: LinterContext? + var inContext = false typealias LinterRegion = (start: Int, end: Int) - var regions: [(LinterRegion, LinterContext)] = flatten(file.contents.lines().map { (line: Line) -> (LinterRegion, LinterContext)? in - let inContext = currentContext != nil + var regions: [LinterRegion] = flatten(self.context.file.contents.lines().map { (line: Line) -> (LinterRegion)? in if line.content == linterContextBegin { if inContext { // starting a new context, so we need the region for the code BEFORE this - return ((contextEndingLineNumber, currentLineNumber - 1), currentContext!) + return (contextEndingLineNumber, currentLineNumber - 1) } - currentContext = LinterContext(enabledRules: self.context.enabledRules, disabledRules: self.context.disabledRules) - + inContext = true contextDepth += 1 contextStartingLineNumber = currentLineNumber + 1 } else if line.content == linterContextEnd && inContext { contextDepth -= 1 - if contextDepth == 0 { - currentContext = nil - } + inContext = contextDepth == 0 contextEndingLineNumber = currentLineNumber - 1 - if !inContext { - return ((contextStartingLineNumber, contextEndingLineNumber), self.context) + return (contextStartingLineNumber, contextEndingLineNumber) } + } currentLineNumber += 1 return nil @@ -69,13 +63,12 @@ public struct Linter { // regions now contains all the regions except // potentially the last, if there is code after the last linterContextEnd // let's add that region to regions + let lastRegion: LinterRegion = (contextEndingLineNumber, max(self.context.file.contents.lines().count - 1, 0)) + regions.append(lastRegion) - let tuple: (LinterRegion, LinterContext) = ((contextEndingLineNumber, max(file.contents.lines().count - 1, 0)), self.context) - regions.append(tuple) - - let linters = regions.map { (tuple: (LinterRegion, LinterContext)) -> Linter in - let file = File(contents: (self.file.contents as NSString).substringWithRange(NSMakeRange(tuple.0.start, tuple.0.end - tuple.0.start))) - return Linter(file: file, context: tuple.1) + let linters = regions.map { (region: LinterRegion) -> Linter in + let file = File(contents: (self.context.file.contents as NSString).substringWithRange(NSMakeRange(region.start, region.end - region.start))) + return Linter(file: file) } return linters @@ -86,7 +79,7 @@ public struct Linter { :param: file File to lint. */ - public init(file: File, context: LinterContext = LinterContext()) { - (self.file, self.context) = (file, context) + public init(file: File) { + self.context = LinterContext(file: file) } } diff --git a/Source/SwiftLintFramework/LinterContext.swift b/Source/SwiftLintFramework/LinterContext.swift index 354e21c6af..3fa3f1b51f 100644 --- a/Source/SwiftLintFramework/LinterContext.swift +++ b/Source/SwiftLintFramework/LinterContext.swift @@ -7,8 +7,10 @@ // import Foundation +import SourceKittenFramework public struct LinterContext { + let file: File var enabledRules: [Rule] = [LineLengthRule(), LeadingWhitespaceRule(), TrailingWhitespaceRule(), @@ -24,12 +26,8 @@ public struct LinterContext { NestingRule()] var disabledRules: [Rule] = [] - public init(enabledRules: [Rule], disabledRules: [Rule]) { - (self.enabledRules, self.disabledRules) = (enabledRules, disabledRules) - } - - public init() { - + public init(file: File) { + self.file = file } func ruleWith(identifier: String, enabled: Bool) -> Rule? {