mirror of
https://github.com/realm/SwiftLint.git
synced 2026-05-07 20:12:49 +00:00
Fix configuration handling in multiline_parameters for stricter validation (#6148)
This commit is contained in:
@@ -59,6 +59,10 @@
|
||||
[kaseken](https://github.com/kaseken)
|
||||
[#6063](https://github.com/realm/SwiftLint/issues/6063)
|
||||
|
||||
* Improve `multiline_parameters` rule to correctly support
|
||||
`max_number_of_single_line_parameters` and detect mixed formatting.
|
||||
[GandaLF2006](https://github.com/GandaLF2006)
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* Improved error reporting when SwiftLint exits, because of an invalid configuration file
|
||||
|
||||
@@ -35,26 +35,32 @@ private extension MultilineParametersRule {
|
||||
}
|
||||
|
||||
var numberOfParameters = 0
|
||||
var linesWithParameters = Set<Int>()
|
||||
var linesWithParameters: Set<Int> = []
|
||||
var hasMultipleParametersOnSameLine = false
|
||||
|
||||
for position in parameterPositions {
|
||||
let line = locationConverter.location(for: position).line
|
||||
linesWithParameters.insert(line)
|
||||
|
||||
if !linesWithParameters.insert(line).inserted {
|
||||
hasMultipleParametersOnSameLine = true
|
||||
}
|
||||
|
||||
numberOfParameters += 1
|
||||
}
|
||||
|
||||
if let maxNumberOfSingleLineParameters = configuration.maxNumberOfSingleLineParameters,
|
||||
configuration.allowsSingleLine,
|
||||
numberOfParameters > maxNumberOfSingleLineParameters {
|
||||
return true
|
||||
}
|
||||
if linesWithParameters.count == 1 {
|
||||
guard configuration.allowsSingleLine else {
|
||||
return numberOfParameters > 1
|
||||
}
|
||||
|
||||
if let maxNumberOfSingleLineParameters = configuration.maxNumberOfSingleLineParameters {
|
||||
return numberOfParameters > maxNumberOfSingleLineParameters
|
||||
}
|
||||
|
||||
guard linesWithParameters.count > (configuration.allowsSingleLine ? 1 : 0),
|
||||
numberOfParameters != linesWithParameters.count else {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
return hasMultipleParametersOnSameLine
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -199,11 +199,20 @@ 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) { }",
|
||||
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,
|
||||
param3: [String]
|
||||
) { }
|
||||
""", configuration: ["max_number_of_single_line_parameters": 2]),
|
||||
]
|
||||
|
||||
static let triggeringExamples: [Example] = [
|
||||
@@ -348,7 +357,9 @@ internal struct MultilineParametersRuleExamples {
|
||||
Example("""
|
||||
func ↓foo(param1: Int,
|
||||
param2: Bool, param3: [String]) { }
|
||||
""",
|
||||
configuration: ["max_number_of_single_line_parameters": 3]),
|
||||
""", configuration: ["max_number_of_single_line_parameters": 3]),
|
||||
Example("""
|
||||
func ↓foo(param1: Int, param2: Bool, param3: [String]) { }
|
||||
""", configuration: ["max_number_of_single_line_parameters": 2]),
|
||||
]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user