From f0804fa51c14dcffe2ce576bbcbe52b28c8bffe6 Mon Sep 17 00:00:00 2001 From: JP Simard Date: Wed, 7 Dec 2016 16:24:54 -0800 Subject: [PATCH] fix some false positives in detecting for initializers in function_parameter_count --- .../Rules/FunctionParameterCountRule.swift | 29 ++++++++++++------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/Source/SwiftLintFramework/Rules/FunctionParameterCountRule.swift b/Source/SwiftLintFramework/Rules/FunctionParameterCountRule.swift index 768e16581..77867fb68 100644 --- a/Source/SwiftLintFramework/Rules/FunctionParameterCountRule.swift +++ b/Source/SwiftLintFramework/Rules/FunctionParameterCountRule.swift @@ -20,6 +20,7 @@ public struct FunctionParameterCountRule: ASTRule, ConfigurationProviderRule { description: "Number of function parameters should be low.", nonTriggeringExamples: [ "init(a: Int, b: Int, c: Int, d: Int, e: Int, f: Int) {}", + "init (a: Int, b: Int, c: Int, d: Int, e: Int, f: Int) {}", "`init`(a: Int, b: Int, c: Int, d: Int, e: Int, f: Int) {}", "init?(a: Int, b: Int, c: Int, d: Int, e: Int, f: Int) {}", "func f2(p1: Int, p2: Int) { }", @@ -29,6 +30,7 @@ public struct FunctionParameterCountRule: ASTRule, ConfigurationProviderRule { ], triggeringExamples: [ "func f(a: Int, b: Int, c: Int, d: Int, e: Int, f: Int) {}", + "func initialValue(a: Int, b: Int, c: Int, d: Int, e: Int, f: Int) {}", "func f(a: Int, b: Int, c: Int, d: Int, e: Int, f: Int = 2, g: Int) {}", "struct Foo {\n" + "init(a: Int, b: Int, c: Int, d: Int, e: Int, f: Int) {}\n" + @@ -44,16 +46,17 @@ public struct FunctionParameterCountRule: ASTRule, ConfigurationProviderRule { let nameOffset = Int(dictionary["key.nameoffset"] as? Int64 ?? 0) let length = Int(dictionary["key.namelength"] as? Int64 ?? 0) - let substructure = dictionary["key.substructure"] as? [SourceKitRepresentable] ?? [] if functionIsInitializer(file, offset: nameOffset, length: length) { return [] } + let substructure = dictionary["key.substructure"] as? [SourceKitRepresentable] ?? [] + let minThreshold = configuration.params.map({ $0.value }).min(by: <) - let allParameterCount = - allFunctionParameterCount(substructure, offset: nameOffset, length: length) + let allParameterCount = allFunctionParameterCount(substructure, offset: nameOffset, + length: length) if allParameterCount < minThreshold! { return [] } @@ -74,7 +77,7 @@ public struct FunctionParameterCountRule: ASTRule, ConfigurationProviderRule { } fileprivate func allFunctionParameterCount(_ structure: [SourceKitRepresentable], - offset: Int, length: Int) -> Int { + offset: Int, length: Int) -> Int { var parameterCount = 0 for substructure in structure { guard let subDict = substructure as? [String: SourceKitRepresentable], @@ -95,18 +98,22 @@ public struct FunctionParameterCountRule: ASTRule, ConfigurationProviderRule { } fileprivate func defaultFunctionParameterCount(_ file: File, offset: Int, length: Int) -> Int { - let equalCharacter = Character("=") - return (file.contents as NSString) - .substringWithByteRange(start: offset, length: length)? - .characters.filter { $0 == equalCharacter }.count ?? 0 + return file.contents.substringWithByteRange(start: offset, length: length)? + .characters.filter { $0 == "=" }.count ?? 0 } fileprivate func functionIsInitializer(_ file: File, offset: Int, length: Int) -> Bool { - if let function = (file.contents as NSString) - .substringWithByteRange(start: offset, length: length), function.hasPrefix("init") { + guard let name = file.contents.substringWithByteRange(start: offset, length: length), + name.hasPrefix("init"), + let funcName = name.components(separatedBy: "(").first else { + return false + } + if funcName == "init" { // fast path return true } - return false + let nonAlphas = CharacterSet.alphanumerics.inverted + let alphaNumericName = funcName.components(separatedBy: nonAlphas).joined() + return alphaNumericName == "init" } fileprivate let functionKinds: [SwiftDeclarationKind] = [