Skip to content

Commit

Permalink
Add new option max_number_of_single_line_parameters to `multiline_…
Browse files Browse the repository at this point in the history
…parameters` rule (#5781)

Co-authored-by: Danny Mösch <danny.moesch@icloud.com>
  • Loading branch information
kimdv and SimplyDanny authored Nov 24, 2024
1 parent c784adc commit 2a723d0
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 0 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@
[SimplyDanny](https://github.com/SimplyDanny)
[#5774](https://github.com/realm/SwiftLint/issues/5774)

* Add new option `max_number_of_single_line_parameters` that allows only the specified maximum
number of parameters to be on one line when `allows_single_line = true`. If the limit is
exceeded, the rule will still trigger. Confusing option combinations like `allows_single_line = false`
together with `max_number_of_single_line_parameters > 1` will be reported.
[kimdv](https://github.com/kimdv)
[SimplyDanny](https://github.com/SimplyDanny)
[#5781](https://github.com/realm/SwiftLint/issues/5781)

* The `redundant_type_annotation` rule gains a new option,
`ignore_properties`, that skips enforcement on members in a
type declaration (like a `struct`). This helps the rule coexist with
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,29 @@ 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 {
guard let maxNumberOfSingleLineParameters else {
return
}
guard maxNumberOfSingleLineParameters >= 1 else {
Issue.inconsistentConfiguration(
ruleID: Parent.identifier,
message: "Option '\($maxNumberOfSingleLineParameters.key)' should be >= 1."
).print()
return
}

if maxNumberOfSingleLineParameters > 1, !allowsSingleLine {
Issue.inconsistentConfiguration(
ruleID: Parent.identifier,
message: """
Option '\($maxNumberOfSingleLineParameters.key)' has no effect when \
'\($allowsSingleLine.key)' is false.
"""
).print()
}
}
}
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]),
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
@testable import SwiftLintBuiltInRules
@testable import SwiftLintCore
import XCTest

final class MultilineParametersConfigurationTests: SwiftLintTestCase {
func testInvalidMaxNumberOfSingleLineParameters() throws {
for maxNumberOfSingleLineParameters in [0, -1] {
var config = MultilineParametersConfiguration()

XCTAssertEqual(
try Issue.captureConsole {
try config.apply(
configuration: ["max_number_of_single_line_parameters": maxNumberOfSingleLineParameters]
)
},
"""
warning: Inconsistent configuration for 'multiline_parameters' rule: Option \
'max_number_of_single_line_parameters' should be >= 1.
"""
)
}
}

func testInvalidMaxNumberOfSingleLineParametersWithSingleLineEnabled() throws {
var config = MultilineParametersConfiguration()

XCTAssertEqual(
try Issue.captureConsole {
try config.apply(
configuration: ["max_number_of_single_line_parameters": 2, "allows_single_line": false]
)
},
"""
warning: Inconsistent configuration for 'multiline_parameters' rule: Option \
'max_number_of_single_line_parameters' has no effect when 'allows_single_line' is false.
"""
)
}
}

0 comments on commit 2a723d0

Please sign in to comment.