-
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
Disable/re-enable rules from within source code comments. Fixes #4. #111
Changes from 1 commit
c699d3d
c10830e
71d9d57
fbc253a
61d46c6
7d85fe3
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 |
---|---|---|
|
@@ -9,9 +9,51 @@ | |
import SourceKittenFramework | ||
import SwiftXPC | ||
|
||
typealias Line = (index: Int, content: String) | ||
public typealias Line = (index: Int, content: String) | ||
|
||
public typealias Region = (startLine: Int, endLine: Int, disabledRules: [String]) | ||
|
||
public enum CommandAction: String { | ||
case Enable = "enable" | ||
case Disable = "disable" | ||
} | ||
|
||
public typealias Command = (CommandAction, String, Int) | ||
|
||
extension File { | ||
public func regions() -> [Region] { | ||
let nsStringContents = contents as NSString | ||
let commands = matchPattern("swiftlint:(enable|disable):force_cast") | ||
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. shouldn't 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. the rule capturing was already being done by |
||
.flatMap { range, syntaxKinds -> Command? in | ||
let scanner = NSScanner(string: nsStringContents.substringWithRange(range)) | ||
scanner.scanString("swiftlint:", intoString: nil) | ||
var actionString: NSString? = nil | ||
scanner.scanUpToString(":", intoString: &actionString) | ||
let start = range.location | ||
if let actionString = actionString as String?, | ||
action = CommandAction(rawValue: actionString), | ||
lineRange = nsStringContents.lineRangeWithByteRange(start: start, length: 0) { | ||
scanner.scanString(":", intoString: nil) | ||
let ruleStart = scanner.string.startIndex.advancedBy(scanner.scanLocation) | ||
let rule = scanner.string.substringFromIndex(ruleStart) | ||
return (action, rule, lineRange.start) | ||
} | ||
return nil | ||
} | ||
let totalNumberOfLines = contents.lines().count | ||
var regions: [Region] = [(1, commands.first?.2 ?? totalNumberOfLines, [])] | ||
var disabledRules = Set<String>() | ||
let commandPairs = zip(commands, Array(commands.dropFirst().map({Optional($0)})) + [nil]) | ||
for (command, nextCommand) in commandPairs { | ||
switch command.0 { | ||
case .Disable: disabledRules.insert(command.1) | ||
case .Enable: disabledRules.remove(command.1) | ||
} | ||
regions.append((command.2, nextCommand?.2 ?? totalNumberOfLines, Array(disabledRules))) | ||
} | ||
return regions | ||
} | ||
|
||
public func matchPattern(pattern: String, | ||
withSyntaxKinds syntaxKinds: [SyntaxKind]) -> [NSRange] { | ||
return matchPattern(pattern).filter { _, kindsInRange in | ||
|
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.
i'd prefer
// swiftlint:disable force_unwrap
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.
that would make it more similar to rubocop, and I have no main reason to keep that separator a colon... so ok!