Skip to content

Commit

Permalink
Merge pull request #2037 from marcelofabri/swift4.1-violations
Browse files Browse the repository at this point in the history
Fix violations introduced when linting with Swift 4.1
  • Loading branch information
marcelofabri authored Feb 4, 2018
2 parents d4346d2 + 03b4d6b commit 9bd193c
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 28 deletions.
3 changes: 2 additions & 1 deletion Source/SwiftLintFramework/Extensions/File+SwiftLint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,9 @@ extension File {
return (count > limit, count)
}

private typealias RangePatternTemplate = (NSRange, String, String)

internal func correct<R: Rule>(legacyRule: R, patterns: [String: String]) -> [Correction] {
typealias RangePatternTemplate = (NSRange, String, String)
let matches: [RangePatternTemplate]
matches = patterns.flatMap({ pattern, template -> [RangePatternTemplate] in
return match(pattern: pattern).filter { range, kinds in
Expand Down
9 changes: 6 additions & 3 deletions Source/SwiftLintFramework/Extensions/String+SwiftLint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,13 @@ extension String {
limitedBy: utf16.endIndex) ?? utf16.endIndex
let to16 = utf16.index(from16, offsetBy: nsrange.length,
limitedBy: utf16.endIndex) ?? utf16.endIndex
if let from = Index(from16, within: self), let to = Index(to16, within: self) {
return from..<to

guard let fromIndex = Index(from16, within: self),
let toIndex = Index(to16, within: self) else {
return nil
}
return nil

return fromIndex..<toIndex
}

public func absolutePathStandardized() -> String {
Expand Down
4 changes: 2 additions & 2 deletions Source/SwiftLintFramework/Models/Location.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ public struct Location: CustomStringConvertible, Comparable {

private func < <T: Comparable>(lhs: T?, rhs: T?) -> Bool {
switch (lhs, rhs) {
case let (l?, r?):
return l < r
case let (lhs?, rhs?):
return lhs < rhs
case (nil, _?):
return true
default:
Expand Down
4 changes: 2 additions & 2 deletions Source/SwiftLintFramework/Models/YamlParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ internal enum YamlParserError: Error, Equatable {

internal func == (lhs: YamlParserError, rhs: YamlParserError) -> Bool {
switch (lhs, rhs) {
case let (.yamlParsing(x), .yamlParsing(y)):
return x == y
case let (.yamlParsing(lhs), .yamlParsing(rhs)):
return lhs == rhs
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,9 @@ public struct CyclomaticComplexityRule: ASTRule, ConfigurationProviderRule {
let bodyOffset = dictionary.bodyOffset ?? 0
let bodyLength = dictionary.bodyLength ?? 0

let c = file.contents.bridge()
.substringWithByteRange(start: bodyOffset, length: bodyLength) ?? ""
let contents = file.contents.bridge().substringWithByteRange(start: bodyOffset, length: bodyLength) ?? ""

let fallthroughCount = c.components(separatedBy: "fallthrough").count - 1
let fallthroughCount = contents.components(separatedBy: "fallthrough").count - 1
return complexity - fallthroughCount
}

Expand Down
4 changes: 2 additions & 2 deletions Source/swiftlint/Commands/LintCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ struct LintCommand: CommandProtocol {
let currentViolations: [StyleViolation]
if options.benchmark {
let start = Date()
let (_currentViolations, currentRuleTimes) = linter.styleViolationsAndRuleTimes
currentViolations = LintCommand.applyLeniency(options: options, violations: _currentViolations)
let (violationsBeforeLeniency, currentRuleTimes) = linter.styleViolationsAndRuleTimes
currentViolations = LintCommand.applyLeniency(options: options, violations: violationsBeforeLeniency)
visitorMutationQueue.sync {
fileBenchmark.record(file: linter.file, from: start)
currentRuleTimes.forEach { ruleBenchmark.record(id: $0, time: $1) }
Expand Down
4 changes: 3 additions & 1 deletion Tests/SwiftLintFrameworkTests/ConfigurationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ class ConfigurationTests: XCTestCase {
"whitelist_rules": whitelist
]
let combinedRulesConfigDict = enabledRulesConfigDict.reduce(disabledRulesConfigDict) {
var d = $0; d[$1.0] = $1.1; return d
var dict = $0
dict[$1.0] = $1.1
return dict
}
var configuration = Configuration(dict: enabledRulesConfigDict)
XCTAssertNil(configuration)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ class CyclomaticComplexityRuleTests: XCTestCase {
private lazy var complexSwitchExample: String = {
var example = "func switcheroo() {\n"
example += " switch foo {\n"
for i in (0...30) {
example += " case \(i): print(\"\(i)\")\n"
for index in (0...30) {
example += " case \(index): print(\"\(index)\")\n"
}
example += " }\n"
example += "}\n"
Expand All @@ -25,14 +25,14 @@ class CyclomaticComplexityRuleTests: XCTestCase {
private lazy var complexIfExample: String = {
let nest = 22
var example = "func nestThoseIfs() {\n"
for i in (0...nest) {
let indent = String(repeating: " ", count: i + 1)
for index in (0...nest) {
let indent = String(repeating: " ", count: index + 1)
example += indent + "if false != true {\n"
example += indent + " print \"\\(i)\"\n"
}

for i in (0...nest).reversed() {
let indent = String(repeating: " ", count: i + 1)
for index in (0...nest).reversed() {
let indent = String(repeating: " ", count: index + 1)
example += indent + "}\n"
}
example += "}\n"
Expand Down
16 changes: 8 additions & 8 deletions Tests/SwiftLintFrameworkTests/Yaml+SwiftLintTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ class YamlSwiftLintTests: XCTestCase {
XCTAssertTrue(array1[2] as? Double == 1.0 && array2[2] as? Double == 1.0)
XCTAssertTrue(array1[3] as? String == "string" && array2[3] as? String == "string")

let dict1_1 = (array1[4] as? [Swift.String: Any])!
let dict2_2 = (array2[4] as? [Swift.String: Any])!
XCTAssertTrue(dict1_1["bool"] as? Bool == true && dict2_2["bool"] as? Bool == true)
XCTAssertTrue(dict1_1["int"] as? Int == 1 && dict2_2["int"] as? Int == 1)
XCTAssertTrue(dict1_1["double"] as? Double == 1.0 &&
dict2_2["double"] as? Double == 1.0)
XCTAssertTrue(dict1_1["string"] as? String == "string" &&
dict2_2["string"] as? String == "string")
let dictFromArray1 = (array1[4] as? [Swift.String: Any])!
let dictFromArray2 = (array2[4] as? [Swift.String: Any])!
XCTAssertTrue(dictFromArray1["bool"] as? Bool == true && dictFromArray2["bool"] as? Bool == true)
XCTAssertTrue(dictFromArray1["int"] as? Int == 1 && dictFromArray2["int"] as? Int == 1)
XCTAssertTrue(dictFromArray1["double"] as? Double == 1.0 &&
dictFromArray2["double"] as? Double == 1.0)
XCTAssertTrue(dictFromArray1["string"] as? String == "string" &&
dictFromArray2["string"] as? String == "string")
} catch {
XCTFail(error.localizedDescription)
}
Expand Down

0 comments on commit 9bd193c

Please sign in to comment.