Skip to content
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
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
* Improve performance of `ColonRule`.
[Norio Nomura](https://github.com/norio-nomura)

* Add `FunctionParameterCountRule`.
[Denis Lebedev](https://github.com/garnett)

##### Bug Fixes

* None.
Expand Down
1 change: 1 addition & 0 deletions Source/SwiftLintFramework/Models/MasterRuleList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public let masterRuleList = RuleList( rules: ClosingBraceRule.self,
NestingRule.self,
OpeningBraceRule.self,
OperatorFunctionWhitespaceRule.self,
FunctionParameterCountRule.self,
ReturnArrowWhitespaceRule.self,
StatementPositionRule.self,
TodoRule.self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public struct CyclomaticComplexityRule: ASTRule, ConfigProviderRule {
let offset = Int(dictionary["key.offset"] as? Int64 ?? 0)
return [StyleViolation(ruleDescription: self.dynamicType.description,
severity: parameter.severity,
location: Location(file: file, characterOffset: offset),
location: Location(file: file, byteOffset: offset),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 nice catch!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

reason: "Function should have complexity \(config.warning) or less: " +
"currently complexity equals \(complexity)")]
}
Expand Down
104 changes: 104 additions & 0 deletions Source/SwiftLintFramework/Rules/FunctionParameterCountRule.swift
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
]
}
4 changes: 4 additions & 0 deletions Source/SwiftLintFrameworkTests/RulesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ class RulesTests: XCTestCase {
verifyRule(OperatorFunctionWhitespaceRule.description)
}

func testFunctionParameterCountRule() {
verifyRule(FunctionParameterCountRule.description)
}

func testReturnArrowWhitespace() {
verifyRule(ReturnArrowWhitespaceRule.description)
}
Expand Down
5 changes: 4 additions & 1 deletion SwiftLint.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
1F11B3CF1C252F23002E8FA8 /* ClosingBraceRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F11B3CE1C252F23002E8FA8 /* ClosingBraceRule.swift */; };
24E17F721B14BB3F008195BE /* File+Cache.swift in Sources */ = {isa = PBXBuildFile; fileRef = 24E17F701B1481FF008195BE /* File+Cache.swift */; };
2E02005F1C54BF680024D09D /* CyclomaticComplexityRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2E02005E1C54BF680024D09D /* CyclomaticComplexityRule.swift */; };
2E5761AA1C573B83003271AF /* FunctionParameterCountRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2E5761A91C573B83003271AF /* FunctionParameterCountRule.swift */; };
3B0B14541C505D6300BE82F7 /* SeverityConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3B0B14531C505D6300BE82F7 /* SeverityConfig.swift */; };
3B1150CA1C31FC3F00D83B1E /* Yaml+SwiftLint.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3B1150C91C31FC3F00D83B1E /* Yaml+SwiftLint.swift */; };
3B12C9C11C3209CB000B423F /* test.yml in Resources */ = {isa = PBXBuildFile; fileRef = 3B12C9BF1C3209AC000B423F /* test.yml */; };
Expand Down Expand Up @@ -164,6 +165,7 @@
1F11B3CE1C252F23002E8FA8 /* ClosingBraceRule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ClosingBraceRule.swift; sourceTree = "<group>"; };
24E17F701B1481FF008195BE /* File+Cache.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "File+Cache.swift"; sourceTree = "<group>"; };
2E02005E1C54BF680024D09D /* CyclomaticComplexityRule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CyclomaticComplexityRule.swift; sourceTree = "<group>"; };
2E5761A91C573B83003271AF /* FunctionParameterCountRule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FunctionParameterCountRule.swift; sourceTree = "<group>"; };
3B0B14531C505D6300BE82F7 /* SeverityConfig.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SeverityConfig.swift; sourceTree = "<group>"; };
3B1150C91C31FC3F00D83B1E /* Yaml+SwiftLint.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Yaml+SwiftLint.swift"; sourceTree = "<group>"; };
3B12C9BF1C3209AC000B423F /* test.yml */ = {isa = PBXFileReference; lastKnownFileType = text; path = test.yml; sourceTree = "<group>"; };
Expand All @@ -177,7 +179,6 @@
3BB47D821C514E8100AE6A10 /* RegexConfig.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RegexConfig.swift; sourceTree = "<group>"; };
3BB47D841C51D80000AE6A10 /* NSRegularExpression+SwiftLint.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "NSRegularExpression+SwiftLint.swift"; sourceTree = "<group>"; };
3BB47D861C51DE6E00AE6A10 /* CustomRulesTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CustomRulesTests.swift; sourceTree = "<group>"; };
3BCC04C51C4EFA52006073C3 /* RuleConfig.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RuleConfig.swift; sourceTree = "<group>"; };
3BCC04CC1C4F5694006073C3 /* ConfigurationError.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ConfigurationError.swift; sourceTree = "<group>"; };
3BCC04CF1C4F56D3006073C3 /* SeverityLevelsConfig.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SeverityLevelsConfig.swift; sourceTree = "<group>"; };
3BCC04D01C4F56D3006073C3 /* NameConfig.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NameConfig.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -568,6 +569,7 @@
E81CDE701C00FEAA00B430F6 /* ValidDocsRule.swift */,
E88DEA931B099C0900A66CB0 /* VariableNameRule.swift */,
2E02005E1C54BF680024D09D /* CyclomaticComplexityRule.swift */,
2E5761A91C573B83003271AF /* FunctionParameterCountRule.swift */,
);
path = Rules;
sourceTree = "<group>";
Expand Down Expand Up @@ -844,6 +846,7 @@
3BCC04D11C4F56D3006073C3 /* SeverityLevelsConfig.swift in Sources */,
E86396C51BADAC15002C9E88 /* XcodeReporter.swift in Sources */,
3B1DF0121C5148140011BCED /* CustomRules.swift in Sources */,
2E5761AA1C573B83003271AF /* FunctionParameterCountRule.swift in Sources */,
E86396C91BADB2B9002C9E88 /* JSONReporter.swift in Sources */,
E881985A1BEA96EA00333A11 /* OperatorFunctionWhitespaceRule.swift in Sources */,
3BCC04D21C4F56D3006073C3 /* NameConfig.swift in Sources */,
Expand Down