SwiftLint icon indicating copy to clipboard operation
SwiftLint copied to clipboard

Multiline arguments violation with multiple trailing closures

Open jakubkiermasz-zd opened this issue 3 years ago • 8 comments

New Issue Checklist

Describe the bug

For below code

talkProvider.getAvailability(forUser: userId) { [weak self] response in
    self?.handleSuccess(response)
} onError: { [weak self] _ in
    self?.handleError()
}

I receive this warning: Multiline Arguments Violation: Arguments should be either on the same line, or one per line. (multiline_arguments)

Environment

  • SwiftLint version (run swiftlint version to be sure)? 0.43.1
  • Installation method used (Homebrew, CocoaPods, building from source, etc)? Pods
  • Are you using nested configurations? nope
  • Which Xcode version are you using (check xcodebuild -version)? Xcode 12.4 Build version 12D4e
  • Do you have a sample that shows the issue? Yes, it is. Done linting! Found 1 violation, 0 serious in 1 file.

jakubkiermasz-zd avatar Apr 19 '21 13:04 jakubkiermasz-zd

To resolve this warning I did following changes to function declaration

Existing function declaration:

func getAvailability(forUser: Int, _ onSuccess: @escaping (String) -> Void, onError: (String) -> Void) { }

Updated function declaration

func getAvailability(forUser: Int, onSuccess: @escaping (String) -> Void, onError: (String) -> Void) { }

Usage of updated function:

getAvailability(forUser: 10, onSuccess: { (_) in

}, onError: { (_) in

})

Sonal-Kachare avatar Apr 28 '21 11:04 Sonal-Kachare

@Sonal-Kachare Thank you for your answer! Of course, the code you've proposed is correct, but unfortunately, it doesn't use the trailing closure feature, which is powerful and recommended in Swift 5.3+.

jakubkiermasz-zd avatar Apr 29 '21 06:04 jakubkiermasz-zd

When you use a SwiftUI TextField, Xcode will generate this code for you:

TextField("Title", text: $text) { isEditing in
    // ...
} onCommit: {
    // ...
}

So this is definitely the recommended syntax and should not trigger a multiline arguments violation.

holgerde avatar May 07 '21 06:05 holgerde

@marcelofabri could you take a look at this, please :)?

jakubkiermasz-zd avatar May 07 '21 09:05 jakubkiermasz-zd

I run into a similar issue with multiple trailing closures but for the vertical_parameter_alignment_on_call rule:

PreviewView(
    previewLayout: .fitWidth(360)
) {
    SomeView()
} update: {
    $0.update(data: data)
}

Which creates the following warning:

Vertical Parameter Alignment On Call Violation: Function parameters should be aligned vertically if they're in multiple lines in a method call. (vertical_parameter_alignment_on_call)

dnadoba avatar May 14 '21 12:05 dnadoba

Any news on the status of this issue? I'm still getting this warning in version 0.47.1.

gentges avatar May 18 '22 14:05 gentges

Also seeing this in 0.47.1.

Custom signature:

init(
	_ title: String,
	@ViewBuilder leftButtonBuilder: () -> LeftButton,
	@ViewBuilder rightButtonBuilder: () -> RightButton
)

Failing Invocation (multiline_arguments):

TitleBar("Profile") {
	backButton
} rightButtonBuilder: {
	editButton
}

Also tried...

Fails multiline_arguments_brackets and vertical_parameter_alignment_on_call:

TitleBar("Profile")
{
	backButton
} rightButtonBuilder: {
	editButton
}

Works, but not ideal:

TitleBar(
	"Profile",
	leftButtonBuilder: {
		backButton
	}, rightButtonBuilder: {
		editButton
	}
)

edelabar avatar May 27 '22 17:05 edelabar

It seems like a workaround would be config it differently:

multiline_arguments:
  only_enforce_after_first_closure_on_first_line: true

Please refer to https://realm.github.io/SwiftLint/multiline_arguments.html

yo1995 avatar May 31 '22 21:05 yo1995

This should've been fixed by https://github.com/realm/SwiftLint/pull/4750

marcelofabri avatar Nov 07 '23 09:11 marcelofabri