Skip to content

Commit

Permalink
Add max number if params allowed
Browse files Browse the repository at this point in the history
  • Loading branch information
kimdv committed Sep 17, 2024
1 parent e9386ae commit 064dade
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ struct MultilineParametersConfiguration: SeverityBasedRuleConfiguration {
private(set) var severityConfiguration = SeverityConfiguration<Parent>(.warning)
@ConfigurationElement(key: "allows_single_line")
private(set) var allowsSingleLine = true
@ConfigurationElement(key: "max_number_of_params")
private(set) var maxNumberOfParams: Int?
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ private extension MultilineParametersRule {
numberOfParameters += 1
}

if let maxNumberOfParams = configuration.maxNumberOfParams,
configuration.allowsSingleLine,
numberOfParameters > maxNumberOfParams {
return true
}

guard linesWithParameters.count > (configuration.allowsSingleLine ? 1 : 0),
numberOfParameters != linesWithParameters.count else {
return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,13 @@ internal struct MultilineParametersRuleExamples {
) { }
}
""", configuration: ["allows_single_line": false]),
Example("func foo(param1: Int, param2: Bool, param3: [String]) { }",
configuration: ["max_number_of_params": 3]),
Example("""
func foo(param1: Int,
param2: Bool,
param3: [String]) { }
""", configuration: ["max_number_of_params": 3]),
]

static let triggeringExamples: [Example] = [
Expand Down Expand Up @@ -336,5 +343,19 @@ internal struct MultilineParametersRuleExamples {
configuration: ["allows_single_line": false]),
Example("func ↓foo(param1: Int, param2: Bool, param3: [String]) { }",
configuration: ["allows_single_line": false]),
Example("func ↓foo(param1: Int, param2: Bool, param3: [String]) { }",
configuration: ["max_number_of_params": 2]),
Example("""
func ↓foo(param1: Int,
param2: Bool, param3: [String]) { }
""",
configuration: ["max_number_of_params": 3]),
Example("""
func ↓foo(param1: Int, param2: Bool) { }
""",
configuration: [
"max_number_of_params": 3,
"allows_single_line": false,
]),
]
}

0 comments on commit 064dade

Please sign in to comment.