diff --git a/CHANGELOG.md b/CHANGELOG.md index bf144ec092..334eb1fb93 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,6 +44,9 @@ [JP Simard](https://github.com/jpsim) [#120](https://github.com/realm/SwiftLint/issues/120) +* Improve performance of `FunctionParameterCountRule`. + [Norio Nomura](https://github.com/norio-nomura) + ##### Bug Fixes * Fix case sensitivity of keywords for `valid_docs`. diff --git a/Source/SwiftLintFramework/Rules/FunctionParameterCountRule.swift b/Source/SwiftLintFramework/Rules/FunctionParameterCountRule.swift index e5513ef22b..73387368e8 100644 --- a/Source/SwiftLintFramework/Rules/FunctionParameterCountRule.swift +++ b/Source/SwiftLintFramework/Rules/FunctionParameterCountRule.swift @@ -40,8 +40,15 @@ public struct FunctionParameterCountRule: ASTRule, ConfigProviderRule { let length = Int(dictionary["key.namelength"] as? Int64 ?? 0) let substructure = dictionary["key.substructure"] as? [SourceKitRepresentable] ?? [] - let parameterCount = - allFunctionParameterCount(substructure, offset: nameOffset, length: length) - + let minThreshold = config.params.map({ $0.value }).minElement(<) + + let allParameterCount = + allFunctionParameterCount(substructure, offset: nameOffset, length: length) + if allParameterCount < minThreshold { + return [] + } + + let parameterCount = allParameterCount - defaultFunctionParameterCount(file, offset: nameOffset, length: length) for parameter in config.params where parameterCount > parameter.value { @@ -78,9 +85,10 @@ public struct FunctionParameterCountRule: ASTRule, ConfigProviderRule { } private 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 == "=" }.count ?? 0 + .characters.filter { $0 == equalCharacter }.count ?? 0 } private let functionKinds: [SwiftDeclarationKind] = [