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 c858669
Show file tree
Hide file tree
Showing 3 changed files with 34 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,12 @@ 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]),
]
}

0 comments on commit c858669

Please sign in to comment.