Skip to content

Commit

Permalink
Code Review Updates
Browse files Browse the repository at this point in the history
- renamed "standard" to "default"
- Updated formatting per code review
- added note to changelog
  • Loading branch information
Michael Skiba committed Jun 10, 2016
1 parent d0247a3 commit b2ad5fc
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 30 deletions.
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
## 0.12.0

##### Breaking

##### Enhancements

* Added `statement_mode` configuration to the `statement_position` rule. The
`default` mode keeps the current SwiftLint behavior of keeping `else` and
`catch` statements on the same line as the closing brace before them. The
`uncuddled_else`configuration requires the `else` and `catch` to be on a new
line with the same leading whitespace as the brace.
[Mike Skiba](https://github.com/ateliercw)
[#651](https://github.com/realm/SwiftLint/issues/651)

##### Bug Fixes

## 0.11.0: Laundromat Format

##### Breaking
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,12 @@
import Foundation

public enum StatmentModeConfiguration: String {
case Standard = "standard", UncuddledElse = "uncuddled_else"
case Default = "default", UncuddledElse = "uncuddled_else"

init(value: AnyObject) throws {
guard let string = (value as? String)?.lowercaseString else {
throw ConfigurationError.UnknownConfiguration
}
if string == StatmentModeConfiguration.Standard.rawValue.lowercaseString {
self = .Standard
} else if string == StatmentModeConfiguration.UncuddledElse.rawValue.lowercaseString {
self = .UncuddledElse
if let string = (value as? String)?.lowercaseString,
value = StatmentModeConfiguration(rawValue: string) {
self = value
} else {
throw ConfigurationError.UnknownConfiguration
}
Expand Down
42 changes: 20 additions & 22 deletions Source/SwiftLintFramework/Rules/StatementPositionRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import SourceKittenFramework

public struct StatementPositionRule: CorrectableRule, ConfigurationProviderRule {

public var configuration = StatmentConfiguration(statementMode: .Standard,
public var configuration = StatmentConfiguration(statementMode: .Default,
severity: SeverityConfiguration(.Warning))

public init() {}
Expand Down Expand Up @@ -45,8 +45,8 @@ public struct StatementPositionRule: CorrectableRule, ConfigurationProviderRule
public static let uncuddledDescription = RuleDescription(
identifier: "statement_position",
name: "Statment Position",
description: "Else and catch should on the next line, with equal indentation to the " +
"previous declaration.",
description: "Else and catch should be on the next line, with equal indentation to the " +
"previous declaration.",
nonTriggeringExamples: [
" }\n else if {",
" }\n else {",
Expand All @@ -73,17 +73,17 @@ public struct StatementPositionRule: CorrectableRule, ConfigurationProviderRule

public func validateFile(file: File) -> [StyleViolation] {
switch configuration.statementMode {
case .Standard:
return standardValidateFile(file)
case .Default:
return defaultValidateFile(file)
case .UncuddledElse:
return uncuddledValidateFile(file)
}
}

public func correctFile(file: File) -> [Correction] {
switch configuration.statementMode {
case .Standard:
return standardCorrectFile(file)
case .Default:
return defaultCorrectFile(file)
case .UncuddledElse:
return uncuddledCorrectFile(file)
}
Expand All @@ -92,35 +92,35 @@ public struct StatementPositionRule: CorrectableRule, ConfigurationProviderRule

}

// Standard Behaviors
// Default Behaviors
private extension StatementPositionRule {

// match literal '}'
// followed by 1) nothing, 2) two+ whitespace/newlines or 3) newlines or tabs
// followed by 'else' or 'catch' literals
static let standardPattern = "\\}(?:[\\s\\n\\r]{2,}|[\\n\\t\\r]+)?\\b(else|catch)\\b"
static let defaultPattern = "\\}(?:[\\s\\n\\r]{2,}|[\\n\\t\\r]+)?\\b(else|catch)\\b"

func standardValidateFile(file: File) -> [StyleViolation] {
return standardViolationRangesInFile(file,
withPattern: self.dynamicType.standardPattern).flatMap { range in
func defaultValidateFile(file: File) -> [StyleViolation] {
return defaultViolationRangesInFile(file,
withPattern: self.dynamicType.defaultPattern).flatMap { range in
return StyleViolation(ruleDescription: self.dynamicType.description,
severity: configuration.severity.severity,
location: Location(file: file, characterOffset: range.location))
}
}

func standardViolationRangesInFile(file: File, withPattern pattern: String) -> [NSRange] {
func defaultViolationRangesInFile(file: File, withPattern pattern: String) -> [NSRange] {
return file.matchPattern(pattern).filter { range, syntaxKinds in
return syntaxKinds.startsWith([.Keyword])
}.flatMap { $0.0 }
}.flatMap { $0.0 }
}

func standardCorrectFile(file: File) -> [Correction] {
let matches = standardViolationRangesInFile(file,
withPattern: self.dynamicType.standardPattern)
func defaultCorrectFile(file: File) -> [Correction] {
let matches = defaultViolationRangesInFile(file,
withPattern: self.dynamicType.defaultPattern)
guard !matches.isEmpty else { return [] }

let regularExpression = regex(self.dynamicType.standardPattern)
let regularExpression = regex(self.dynamicType.defaultPattern)
let description = self.dynamicType.description
var corrections = [Correction]()
var contents = file.contents
Expand All @@ -138,7 +138,7 @@ private extension StatementPositionRule {
}

//Uncuddled Behaviors
extension StatementPositionRule {
private extension StatementPositionRule {
func uncuddledValidateFile(file: File) -> [StyleViolation] {
return uncuddledViolationRangesInFile(file).flatMap { range in
return StyleViolation(ruleDescription: self.dynamicType.uncuddledDescription,
Expand All @@ -147,8 +147,6 @@ extension StatementPositionRule {
}
}

// MARK: - Private

// match literal '}'
// preceded by whitespace (or nothing)
// followed by 1) nothing, 2) two+ whitespace/newlines or 3) newlines or tabs
Expand Down Expand Up @@ -184,7 +182,7 @@ extension StatementPositionRule {
let range = match.range
guard let matchRange = contents.NSRangeToByteRange(start: range.location,
length: range.length) else {
return false
return false
}
let tokens = syntaxMap.tokensIn(matchRange).flatMap { SyntaxKind(rawValue: $0.type) }
return tokens == [.Keyword]
Expand Down

0 comments on commit b2ad5fc

Please sign in to comment.