-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Limit parameters count #447
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7c428a6
Implement ParametersListLengthRule
delebedev 18be9e6
Use byteOffset for Styleviolation construction
delebedev 9697859
Use substringWithByteRange for extracting substring
delebedev 41a31ac
Update CHANGELOG
delebedev f47a1a4
Rename ParametersListLengthRule -> FunctionParameterCountRule
delebedev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
Source/SwiftLintFramework/Rules/FunctionParameterCountRule.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
// | ||
// FunctionParameterCountRule.swift | ||
// SwiftLint | ||
// | ||
// Created by Denis Lebedev on 26/01/2016. | ||
// Copyright © 2016 Realm. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import SourceKittenFramework | ||
|
||
public struct FunctionParameterCountRule: ASTRule, ConfigProviderRule { | ||
public var config = SeverityLevelsConfig(warning: 5, error: 8) | ||
|
||
public init() {} | ||
|
||
public static let description = RuleDescription( | ||
identifier: "function_parameter_count", | ||
name: "Function Paramete rCount", | ||
description: "Count of function parameters should be low.", | ||
nonTriggeringExamples: [ | ||
"func f2(p1: Int, p2: Int) { }", | ||
"func f(a: Int, b: Int, c: Int, d: Int, x: Int = 42) {}", | ||
"func f(a: [Int], b: Int, c: Int, d: Int, f: Int) -> [Int] {\n" + | ||
"let s = a.flatMap { $0 as? [String: Int] } ?? []}}" | ||
], | ||
triggeringExamples: [ | ||
"func f(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) {}", | ||
] | ||
) | ||
|
||
public func validateFile(file: File, kind: SwiftDeclarationKind, | ||
dictionary: [String: SourceKitRepresentable]) -> [StyleViolation] { | ||
if !functionKinds.contains(kind) { | ||
return [] | ||
} | ||
|
||
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] ?? [] | ||
|
||
let allParams = allFuncParameters(substructure, offset: nameOffset, length: length) | ||
let defaultParams = defaultFuncParameters(file, offset: nameOffset, length: length) | ||
|
||
let parametersCount = allParams - defaultParams | ||
|
||
for parameter in config.params where parametersCount > parameter.value { | ||
let offset = Int(dictionary["key.offset"] as? Int64 ?? 0) | ||
return [StyleViolation(ruleDescription: self.dynamicType.description, | ||
severity: parameter.severity, | ||
location: Location(file: file, byteOffset: offset), | ||
reason: "Function should have \(config.warning) or less parameters: " + | ||
"currently it has \(parametersCount)")] | ||
} | ||
|
||
return [] | ||
} | ||
|
||
private func allFuncParameters(structure: [SourceKitRepresentable], | ||
offset: Int, length: Int) -> Int { | ||
|
||
var count = 0 | ||
for e in structure { | ||
guard let subDict = e as? [String: SourceKitRepresentable], | ||
key = subDict["key.kind"] as? String, | ||
paramOffset = subDict["key.offset"] as? Int64 else { | ||
continue | ||
} | ||
|
||
guard offset..<offset+length ~= Int(paramOffset) else { | ||
return count | ||
} | ||
|
||
if SwiftDeclarationKind(rawValue: key) == .VarParameter { | ||
count += 1 | ||
} | ||
} | ||
return count | ||
} | ||
|
||
private func defaultFuncParameters(file: File, offset: Int, length: Int) -> Int { | ||
return (file.contents as NSString) | ||
.substringWithByteRange(start: offset, length: length)? | ||
.characters.filter { $0 == "=" }.count ?? 0 | ||
} | ||
|
||
private let functionKinds: [SwiftDeclarationKind] = [ | ||
.FunctionAccessorAddress, | ||
.FunctionAccessorDidset, | ||
.FunctionAccessorGetter, | ||
.FunctionAccessorMutableaddress, | ||
.FunctionAccessorSetter, | ||
.FunctionAccessorWillset, | ||
.FunctionConstructor, | ||
.FunctionDestructor, | ||
.FunctionFree, | ||
.FunctionMethodClass, | ||
.FunctionMethodInstance, | ||
.FunctionMethodStatic, | ||
.FunctionOperator, | ||
.FunctionSubscript | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 nice catch!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍