|
| 1 | +import SwiftLintCore |
| 2 | +import SwiftSyntax |
| 3 | + |
| 4 | +@SwiftSyntaxRule(optIn: true) |
| 5 | +struct MultilineCallArgumentsRule: Rule { |
| 6 | + var configuration = MultilineCallArgumentsConfiguration() |
| 7 | + |
| 8 | + static let description = RuleDescription( |
| 9 | + identifier: "multiline_call_arguments", |
| 10 | + name: "Multiline Call Arguments", |
| 11 | + description: """ |
| 12 | + Arguments in function and method calls should be either on the same line \ |
| 13 | + or one per line when the call spans multiple lines |
| 14 | + """, |
| 15 | + kind: .style, |
| 16 | + nonTriggeringExamples: [ |
| 17 | + Example(""" |
| 18 | + foo( |
| 19 | + param1: "param1", |
| 20 | + param2: false, |
| 21 | + param3: [] |
| 22 | + ) |
| 23 | + """, configuration: ["max_number_of_single_line_parameters": 2]), |
| 24 | + Example(""" |
| 25 | + foo(param1: 1, |
| 26 | + param2: false, |
| 27 | + param3: []) |
| 28 | + """, configuration: ["max_number_of_single_line_parameters": 1]), |
| 29 | + Example("foo(param1: 1, param2: false)", |
| 30 | + configuration: ["max_number_of_single_line_parameters": 2]), |
| 31 | + Example("Enum.foo(param1: 1, param2: false)", |
| 32 | + configuration: ["max_number_of_single_line_parameters": 2]), |
| 33 | + Example(""" |
| 34 | + foo(param1: 1) |
| 35 | + """, |
| 36 | + configuration: [ |
| 37 | + "allows_single_line": false |
| 38 | + ]), |
| 39 | + Example(""" |
| 40 | + Enum.foo(param1: 1) |
| 41 | + """, |
| 42 | + configuration: [ |
| 43 | + "allows_single_line": false |
| 44 | + ]), |
| 45 | + Example(""" |
| 46 | + Enum.foo(param1: 1, param2: 2, param3: 3) |
| 47 | + """, |
| 48 | + configuration: [ |
| 49 | + "allows_single_line": true |
| 50 | + ]), |
| 51 | + Example(""" |
| 52 | + foo( |
| 53 | + param1: 1, |
| 54 | + param2: 2, |
| 55 | + param3: 3 |
| 56 | + ) |
| 57 | + """, |
| 58 | + configuration: [ |
| 59 | + "allows_single_line": false |
| 60 | + ]), |
| 61 | + ], |
| 62 | + triggeringExamples: [ |
| 63 | + Example("↓foo(param1: 1, param2: false, param3: [])", |
| 64 | + configuration: [ |
| 65 | + "max_number_of_single_line_parameters": 2 |
| 66 | + ]), |
| 67 | + Example("↓Enum.foo(param1: 1, param2: false, param3: [])", |
| 68 | + configuration: [ |
| 69 | + "max_number_of_single_line_parameters": 2 |
| 70 | + ]), |
| 71 | + Example(""" |
| 72 | + ↓foo(param1: 1, param2: false, |
| 73 | + param3: []) |
| 74 | + """, configuration: [ |
| 75 | + "max_number_of_single_line_parameters": 3 |
| 76 | + ]), |
| 77 | + Example(""" |
| 78 | + ↓Enum.foo(param1: 1, param2: false, |
| 79 | + param3: []) |
| 80 | + """, configuration: [ |
| 81 | + "max_number_of_single_line_parameters": 3 |
| 82 | + ]), |
| 83 | + Example(""" |
| 84 | + ↓foo(param1: 1, param2: false) |
| 85 | + """, |
| 86 | + configuration: [ |
| 87 | + "allows_single_line": false |
| 88 | + ]), |
| 89 | + Example(""" |
| 90 | + ↓Enum.foo(param1: 1, param2: false) |
| 91 | + """, |
| 92 | + configuration: [ |
| 93 | + "allows_single_line": false |
| 94 | + ]), |
| 95 | + ] |
| 96 | + ) |
| 97 | +} |
| 98 | + |
| 99 | +private extension MultilineCallArgumentsRule { |
| 100 | + final class Visitor: ViolationsSyntaxVisitor<ConfigurationType> { |
| 101 | + override func visitPost(_ node: FunctionCallExprSyntax) { |
| 102 | + guard node.arguments.isNotEmpty else { return } |
| 103 | + guard node.trailingClosure == nil else { return } |
| 104 | + |
| 105 | + if containsViolation(for: node.arguments) { |
| 106 | + let anchor = node.calledExpression.positionAfterSkippingLeadingTrivia |
| 107 | + violations.append(anchor) |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + private func containsViolation(for arguments: LabeledExprListSyntax) -> Bool { |
| 112 | + let argumentPositions = arguments.map(\.positionAfterSkippingLeadingTrivia) |
| 113 | + return containsViolation(parameterPositions: argumentPositions) |
| 114 | + } |
| 115 | + |
| 116 | + private func containsViolation(parameterPositions: [AbsolutePosition]) -> Bool { |
| 117 | + guard parameterPositions.isNotEmpty else { |
| 118 | + return false |
| 119 | + } |
| 120 | + |
| 121 | + var numberOfParameters = 0 |
| 122 | + var linesWithParameters: Set<Int> = [] |
| 123 | + var hasMultipleParametersOnSameLine = false |
| 124 | + |
| 125 | + for position in parameterPositions { |
| 126 | + let line = locationConverter.location(for: position).line |
| 127 | + |
| 128 | + if !linesWithParameters.insert(line).inserted { |
| 129 | + hasMultipleParametersOnSameLine = true |
| 130 | + } |
| 131 | + |
| 132 | + numberOfParameters += 1 |
| 133 | + } |
| 134 | + |
| 135 | + if linesWithParameters.count == 1 { |
| 136 | + guard configuration.allowsSingleLine else { |
| 137 | + return numberOfParameters > 1 |
| 138 | + } |
| 139 | + |
| 140 | + if let maxNumberOfSingleLineParameters = configuration.maxNumberOfSingleLineParameters { |
| 141 | + return numberOfParameters > maxNumberOfSingleLineParameters |
| 142 | + } |
| 143 | + |
| 144 | + return false |
| 145 | + } |
| 146 | + |
| 147 | + return hasMultipleParametersOnSameLine |
| 148 | + } |
| 149 | + } |
| 150 | +} |
0 commit comments