mirror of
https://github.com/realm/SwiftLint.git
synced 2026-05-07 20:12:49 +00:00
40bd97038a
Almost all rules based on SwiftSyntax can be set up now by just adding `@SwiftSyntaxRule` to the rule struct.
55 lines
2.0 KiB
Swift
55 lines
2.0 KiB
Swift
import SwiftSyntax
|
|
|
|
@SwiftSyntaxRule
|
|
struct MultilineParametersRule: OptInRule {
|
|
var configuration = MultilineParametersConfiguration()
|
|
|
|
static let description = RuleDescription(
|
|
identifier: "multiline_parameters",
|
|
name: "Multiline Parameters",
|
|
description: "Functions and methods parameters should be either on the same line, or one per line",
|
|
kind: .style,
|
|
nonTriggeringExamples: MultilineParametersRuleExamples.nonTriggeringExamples,
|
|
triggeringExamples: MultilineParametersRuleExamples.triggeringExamples
|
|
)
|
|
}
|
|
|
|
private extension MultilineParametersRule {
|
|
final class Visitor: ViolationsSyntaxVisitor<ConfigurationType> {
|
|
override func visitPost(_ node: FunctionDeclSyntax) {
|
|
if containsViolation(for: node.signature) {
|
|
violations.append(node.name.positionAfterSkippingLeadingTrivia)
|
|
}
|
|
}
|
|
|
|
override func visitPost(_ node: InitializerDeclSyntax) {
|
|
if containsViolation(for: node.signature) {
|
|
violations.append(node.initKeyword.positionAfterSkippingLeadingTrivia)
|
|
}
|
|
}
|
|
|
|
private func containsViolation(for signature: FunctionSignatureSyntax) -> Bool {
|
|
let parameterPositions = signature.parameterClause.parameters.map(\.positionAfterSkippingLeadingTrivia)
|
|
guard parameterPositions.isNotEmpty else {
|
|
return false
|
|
}
|
|
|
|
var numberOfParameters = 0
|
|
var linesWithParameters = Set<Int>()
|
|
|
|
for position in parameterPositions {
|
|
let line = locationConverter.location(for: position).line
|
|
linesWithParameters.insert(line)
|
|
numberOfParameters += 1
|
|
}
|
|
|
|
guard linesWithParameters.count > (configuration.allowsSingleLine ? 1 : 0),
|
|
numberOfParameters != linesWithParameters.count else {
|
|
return false
|
|
}
|
|
|
|
return true
|
|
}
|
|
}
|
|
}
|