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 Oct 22, 2024
1 parent b486648 commit f724f3a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,18 @@ 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_single_line_parameters")
private(set) var maxNumberOfSingleLineParameters: Int?

func validate() throws {
if let maxNumberOfSingleLineParameters, !allowsSingleLine && maxNumberOfSingleLineParameters > 1 {
throw Issue.invalidConfiguration(
ruleID: Parent.identifier,
message: """
Invalid configuration: 'allows_single_line' and 'max_number_of_single_line_parameters' are mutually exclusive.
They cannot both be active with conflicting values.
"""
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ private extension MultilineParametersRule {
numberOfParameters += 1
}

if let maxNumberOfSingleLineParameters = configuration.maxNumberOfSingleLineParameters,
configuration.allowsSingleLine,
numberOfParameters > maxNumberOfSingleLineParameters {
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_single_line_parameters": 3]),
Example("""
func foo(param1: Int,
param2: Bool,
param3: [String]) { }
""", configuration: ["max_number_of_single_line_parameters": 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_single_line_parameters": 2]),
Example("""
func ↓foo(param1: Int,
param2: Bool, param3: [String]) { }
""",
configuration: ["max_number_of_single_line_parameters": 3]),
Example("""
func ↓foo(param1: Int, param2: Bool) { }
""",
configuration: [
"max_number_of_single_line_parameters": 3,
"allows_single_line": false,
]),
]
}

0 comments on commit f724f3a

Please sign in to comment.