Skip to content

Commit

Permalink
Move the file storage to LinterContext
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron Daub authored and Aaron Daub committed May 20, 2015
1 parent 4d849a7 commit 4ca141c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 29 deletions.
39 changes: 16 additions & 23 deletions Source/SwiftLintFramework/Linter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)
}
}
10 changes: 4 additions & 6 deletions Source/SwiftLintFramework/LinterContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
//

import Foundation
import SourceKittenFramework

public struct LinterContext {
let file: File
var enabledRules: [Rule] = [LineLengthRule(),
LeadingWhitespaceRule(),
TrailingWhitespaceRule(),
Expand All @@ -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? {
Expand Down

0 comments on commit 4ca141c

Please sign in to comment.