Skip to content

Commit

Permalink
Merge pull request #521 from realm/nn-optimize-function-parameter-cou…
Browse files Browse the repository at this point in the history
…nt-rule

Improve performance of `FunctionParameterCountRule`
  • Loading branch information
jpsim committed Feb 10, 2016
2 parents 3fa233f + 03b237e commit dc97106
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
14 changes: 11 additions & 3 deletions Source/SwiftLintFramework/Rules/FunctionParameterCountRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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] = [
Expand Down

0 comments on commit dc97106

Please sign in to comment.