diff --git a/Source/SwiftLintFramework/Reporters/JUnitReporter.swift b/Source/SwiftLintFramework/Reporters/JUnitReporter.swift index d99051b1f..a2205af8a 100644 --- a/Source/SwiftLintFramework/Reporters/JUnitReporter.swift +++ b/Source/SwiftLintFramework/Reporters/JUnitReporter.swift @@ -18,7 +18,7 @@ public struct JUnitReporter: Reporter { public static func generateReport(_ violations: [StyleViolation]) -> String { return "\n" + - violations.map({ violation in + violations.map({ violation -> String in let fileName = (violation.location.file ?? "").escapedForXML() let severity = violation.severity.rawValue + ":\n" let message = severity + "Line:" + String(violation.location.line ?? 0) + " " diff --git a/Source/SwiftLintFramework/Rules/ColonRule.swift b/Source/SwiftLintFramework/Rules/ColonRule.swift index fbf81f731..1008f0db1 100644 --- a/Source/SwiftLintFramework/Rules/ColonRule.swift +++ b/Source/SwiftLintFramework/Rules/ColonRule.swift @@ -203,7 +203,7 @@ public struct ColonRule: CorrectableRule, ConfigurationProviderRule { private typealias RangeWithKind = (range: NSRange, kind: ColonKind) private func correctionRanges(in file: File) -> [RangeWithKind] { - let violations = typeColonViolationRanges(in: file, matching: pattern).map { + let violations: [RangeWithKind] = typeColonViolationRanges(in: file, matching: pattern).map { (range: $0, kind: ColonKind.type) } let dictionary = file.structure.dictionary diff --git a/Source/SwiftLintFramework/Rules/ControlStatementRule.swift b/Source/SwiftLintFramework/Rules/ControlStatementRule.swift index d022a0caa..88a20a60b 100644 --- a/Source/SwiftLintFramework/Rules/ControlStatementRule.swift +++ b/Source/SwiftLintFramework/Rules/ControlStatementRule.swift @@ -58,21 +58,23 @@ public struct ControlStatementRule: ConfigurationProviderRule { public func validate(file: File) -> [StyleViolation] { let statements = ["if", "for", "guard", "switch", "while"] - return statements.flatMap { statementKind -> [StyleViolation] in - let pattern = statementKind == "guard" - ? "\(statementKind)\\s*\\([^,{]*\\)\\s*else\\s*\\{" - : "\(statementKind)\\s*\\([^,{]*\\)\\s*\\{" - return file.match(pattern: pattern).flatMap { match, syntaxKinds in - let matchString = file.contents.substring(from: match.location, length: match.length) - if isFalsePositive(matchString, syntaxKind: syntaxKinds.first) { - return nil - } - return StyleViolation(ruleDescription: type(of: self).description, - severity: configuration.severity, - location: Location(file: file, characterOffset: match.location)) - } + let statementPatterns: [String] = statements.map { statement -> String in + let isGuard = statement == "guard" + let elsePattern = isGuard ? "else\\s*" : "" + return "\(statement)\\s*\\([^,{]*\\)\\s*\(elsePattern)\\{" + } + return statementPatterns.flatMap { pattern -> [StyleViolation] in + return file.match(pattern: pattern) + .filter { match, syntaxKinds -> Bool in + let matchString = file.contents.substring(from: match.location, length: match.length) + return !isFalsePositive(matchString, syntaxKind: syntaxKinds.first) + } + .map { match, _ -> StyleViolation in + return StyleViolation(ruleDescription: type(of: self).description, + severity: configuration.severity, + location: Location(file: file, characterOffset: match.location)) + } } - } fileprivate func isFalsePositive(_ content: String, syntaxKind: SyntaxKind?) -> Bool { diff --git a/Source/SwiftLintFramework/Rules/GenericTypeNameRule.swift b/Source/SwiftLintFramework/Rules/GenericTypeNameRule.swift index 941b4b7de..523d36d03 100644 --- a/Source/SwiftLintFramework/Rules/GenericTypeNameRule.swift +++ b/Source/SwiftLintFramework/Rules/GenericTypeNameRule.swift @@ -47,8 +47,8 @@ public struct GenericTypeNameRule: ASTRule, ConfigurationProviderRule { "typealias StringDictionary<↓T_Foo> = Dictionary\n", "typealias BackwardTriple = (T3, T2_Bar, T1)\n", "typealias DictionaryOfStrings<↓T_Foo: Hashable> = Dictionary\n" - ] + ["class", "struct", "enum"].flatMap { type in - [ + ] + ["class", "struct", "enum"].flatMap { type -> [String] in + return [ "\(type) Foo<↓T_Foo> {}\n", "\(type) Foo {}\n", "\(type) Foo<↓T_Foo, ↓U_Foo> {}\n", diff --git a/Source/SwiftLintFramework/Rules/NestingRule.swift b/Source/SwiftLintFramework/Rules/NestingRule.swift index f2e857e3b..4d4b98b30 100644 --- a/Source/SwiftLintFramework/Rules/NestingRule.swift +++ b/Source/SwiftLintFramework/Rules/NestingRule.swift @@ -21,15 +21,15 @@ public struct NestingRule: ASTRule, ConfigurationProviderRule { identifier: "nesting", name: "Nesting", description: "Types should be nested at most 1 level deep, " + - "and statements should be nested at most 5 levels deep.", + "and statements should be nested at most 5 levels deep.", kind: .metrics, - nonTriggeringExamples: ["class", "struct", "enum"].flatMap { kind in + nonTriggeringExamples: ["class", "struct", "enum"].flatMap { kind -> [String] in ["\(kind) Class0 { \(kind) Class1 {} }\n", "func func0() {\nfunc func1() {\nfunc func2() {\nfunc func3() {\nfunc func4() { " + "func func5() {\n}\n}\n}\n}\n}\n}\n"] } + ["enum Enum0 { enum Enum1 { case Case } }"], - triggeringExamples: ["class", "struct", "enum"].map { kind in - "\(kind) A { \(kind) B { ↓\(kind) C {} } }\n" + triggeringExamples: ["class", "struct", "enum"].map { kind -> String in + return "\(kind) A { \(kind) B { ↓\(kind) C {} } }\n" } + [ "func func0() {\nfunc func1() {\nfunc func2() {\nfunc func3() {\nfunc func4() { " + "func func5() {\n↓func func6() {\n}\n}\n}\n}\n}\n}\n}\n" diff --git a/Source/SwiftLintFramework/Rules/NumberSeparatorRule.swift b/Source/SwiftLintFramework/Rules/NumberSeparatorRule.swift index beb705f90..9d5638bde 100644 --- a/Source/SwiftLintFramework/Rules/NumberSeparatorRule.swift +++ b/Source/SwiftLintFramework/Rules/NumberSeparatorRule.swift @@ -34,7 +34,7 @@ public struct NumberSeparatorRule: OptInRule, CorrectableRule, ConfigurationProv private func violatingRanges(in file: File) -> [(NSRange, String)] { let numberTokens = file.syntaxMap.tokens.filter { SyntaxKind(rawValue: $0.type) == .number } - return numberTokens.flatMap { token in + return numberTokens.flatMap { (token: SyntaxToken) -> (NSRange, String)? in guard let content = contentFrom(file: file, token: token), isDecimal(number: content) else { return nil diff --git a/Source/SwiftLintFramework/Rules/VerticalWhitespaceRule.swift b/Source/SwiftLintFramework/Rules/VerticalWhitespaceRule.swift index 533aa277c..4f2d03f3f 100644 --- a/Source/SwiftLintFramework/Rules/VerticalWhitespaceRule.swift +++ b/Source/SwiftLintFramework/Rules/VerticalWhitespaceRule.swift @@ -101,7 +101,8 @@ public struct VerticalWhitespaceRule: CorrectableRule, ConfigurationProviderRule var previousIndex = 0 for (index, line) in lines.enumerated() { - if lines[previousIndex].index + 1 == line.index { + let previousLine: Line = lines[previousIndex] + if previousLine.index + 1 == line.index { lineSection.append(line) } else if !lineSection.isEmpty { blankLinesSections.append(lineSection)